mirror of
https://github.com/serverless/serverless.git
synced 2026-01-25 15:07:39 +00:00
Add separate request template for x-www-form-urlencoded content type
This commit is contained in:
parent
262fabd75a
commit
495c5d8eea
@ -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" : [
|
||||
|
||||
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user