From 495c5d8eea6cb46fed43b9d778ab0be4452329a9 Mon Sep 17 00:00:00 2001 From: Philipp Muens Date: Fri, 26 Aug 2016 15:50:27 +0200 Subject: [PATCH] Add separate request template for x-www-form-urlencoded content type --- .../compile/events/apiGateway/lib/methods.js | 71 ++++++++++++++++--- .../events/apiGateway/tests/methods.js | 29 ++++++-- 2 files changed, 82 insertions(+), 18 deletions(-) diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/methods.js b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/methods.js index 1f34a74c1..1c352b35c 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/lib/methods.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/lib/methods.js @@ -30,14 +30,6 @@ module.exports = { } // add the default request and response templates to the CloudFormation "Mappings" section - - // request template - // provides - // { - // body, method, principalId, stage, headers, - // query, path, identity, stageVariables - // } = event - // as js objects const DEFAULT_JSON_REQUEST_TEMPLATE = ` #define( $loop ) { @@ -48,6 +40,7 @@ module.exports = { #end } #end + { "body": $input.json("$"), "method": "$context.httpMethod", @@ -71,11 +64,64 @@ module.exports = { } `; + const DEFAULT_FORM_URL_ENCODED_REQUEST_TEMPLATE = ` + #define( $body ) + { + #foreach( $token in $input.path('$').split('&') ) + #set( $keyVal = $token.split('=') ) + #set( $keyValSize = $keyVal.size() ) + #if( $keyValSize >= 1 ) + #set( $key = $util.urlDecode($keyVal[0]) ) + #if( $keyValSize >= 2 ) + #set( $val = $util.urlDecode($keyVal[1]) ) + #else + #set( $val = '' ) + #end + "$key": "$val"#if($foreach.hasNext),#end + #end + #end + } + #end + + #define( $loop ) + { + #foreach($key in $map.keySet()) + "$util.escapeJavaScript($key)": + "$util.escapeJavaScript($map.get($key))" + #if( $foreach.hasNext ) , #end + #end + } + #end + + { + "body": $body, + "method": "$context.httpMethod", + "principalId": "$context.authorizer.principalId", + "stage": "$context.stage", + + #set( $map = $input.params().header ) + "headers": $loop, + + #set( $map = $input.params().querystring ) + "query": $loop, + + #set( $map = $input.params().path ) + "path": $loop, + + #set( $map = $context.identity ) + "identity": $loop, + + #set( $map = $stageVariables ) + "stageVariables": $loop + } + `; + const apiGatewayMappings = ` { "ApiGateway": { - "RequestTemplate": { - "Value": ${JSON.stringify(DEFAULT_JSON_REQUEST_TEMPLATE)} + "RequestTemplates": { + "Json": ${JSON.stringify(DEFAULT_JSON_REQUEST_TEMPLATE)}, + "FormUrlEncoded": ${JSON.stringify(DEFAULT_FORM_URL_ENCODED_REQUEST_TEMPLATE)} } } } @@ -179,7 +225,10 @@ module.exports = { }, "RequestTemplates" : { "application/json" : { - "Fn::FindInMap": [ "ApiGateway", "RequestTemplate", "Value" ] + "Fn::FindInMap": [ "ApiGateway", "RequestTemplates", "Json" ] + }, + "application/x-www-form-urlencoded" : { + "Fn::FindInMap": [ "ApiGateway", "RequestTemplates", "FormUrlEncoded" ] } }, "IntegrationResponses" : [ diff --git a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js index 68188f344..ad2b10b27 100644 --- a/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js +++ b/lib/plugins/aws/deploy/compile/events/apiGateway/tests/methods.js @@ -299,17 +299,17 @@ describe('#compileMethods()', () => { .compileMethods().then(() => { expect( awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate - .Mappings.ApiGateway.RequestTemplate.Value - ).to.not.equal(undefined); + .Mappings.ApiGateway.RequestTemplates + ).to.not.deep.equal({}); }) ); - it('should reference the request template for the different content types', () => { - const findInMap = { + it('should reference the request templates for the different content types', () => { + const findInMapForJson = { 'Fn::FindInMap': [ 'ApiGateway', - 'RequestTemplate', - 'Value', + 'RequestTemplates', + 'Json', ], }; @@ -319,7 +319,22 @@ describe('#compileMethods()', () => { awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate .Resources.PostMethodApigEvent0.Properties.Integration .RequestTemplates['application/json'] - ).to.equal(JSON.stringify(findInMap)); + ).to.deep.equal(findInMapForJson); + + const findInMapForFormUrlEncoded = { + 'Fn::FindInMap': [ + 'ApiGateway', + 'RequestTemplates', + 'FormUrlEncoded', + ], + }; + + // application/x-www-form-urlencoded + expect( + awsCompileApigEvents.serverless.service.provider.compiledCloudFormationTemplate + .Resources.PostMethodApigEvent0.Properties.Integration + .RequestTemplates['application/x-www-form-urlencoded'] + ).to.deep.equal(findInMapForFormUrlEncoded); }); }); });