Skip to content

Commit

Permalink
Support ANY method type for stage configuration
Browse files Browse the repository at this point in the history
Updated tests

Use LF EOL again ....

Set all methods explicitly

Added unit test for ANY method type with stage configuration

Use `*` as method for ANY type endpoints
  • Loading branch information
Frank Schmid committed Nov 22, 2017
1 parent e0215d2 commit 61ad807
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules
/coverage
/.nyc_output
16 changes: 14 additions & 2 deletions lib/stackops/apiGateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ const internal = {
if (!_.isEmpty(eventStageConfig)) {
const methodType = _.toUpper(httpEvent.method);
const methodSetting = {};
const methods = methodType === 'ANY' ? [
'DELETE',
'GET',
'HEAD',
'OPTIONS',
'PATCH',
'POST',
'PUT'
]: [ methodType ];

_.forOwn(eventStageConfig, (value, key) => {
if (!_.has(stageMethodConfigMappings, key)) {
throw new this.serverless.classes.Error(`Invalid stage config '${key}' at method '${methodType} /${httpEvent.path}'`);
Expand All @@ -91,9 +101,11 @@ const internal = {
}
});
if (!_.isEmpty(methodSetting)) {
methodSetting.HttpMethod = methodType;
methodSetting.ResourcePath = '/' + _.replace('/' + _.trimStart(httpEvent.path, '/'), /\//g, '~1');
methodSettings.push(methodSetting);
_.forEach(methods, method => {
methodSetting.HttpMethod = method;
methodSettings.push(_.clone(methodSetting));
});
}
}
});
Expand Down
46 changes: 46 additions & 0 deletions test/stackops/apiGateway.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,17 @@ describe('API Gateway', () => {
}
]
},
functionC: {
handler: 'functionB.handler',
events: [
{
http: {
method: 'ANY',
path: '/funcC'
}
},
]
},
}
};
const expectedMethodSettings = [
Expand All @@ -246,6 +257,41 @@ describe('API Gateway', () => {
HttpMethod: 'UPDATE',
ResourcePath: '/~1funcB~1update'
},
{
LoggingLevel: 'INFO',
HttpMethod: 'DELETE',
ResourcePath: '/~1funcC'
},
{
LoggingLevel: 'INFO',
HttpMethod: 'GET',
ResourcePath: '/~1funcC'
},
{
LoggingLevel: 'INFO',
HttpMethod: 'HEAD',
ResourcePath: '/~1funcC'
},
{
LoggingLevel: 'INFO',
HttpMethod: 'OPTIONS',
ResourcePath: '/~1funcC'
},
{
LoggingLevel: 'INFO',
HttpMethod: 'PATCH',
ResourcePath: '/~1funcC'
},
{
LoggingLevel: 'INFO',
HttpMethod: 'POST',
ResourcePath: '/~1funcC'
},
{
LoggingLevel: 'INFO',
HttpMethod: 'PUT',
ResourcePath: '/~1funcC'
},
];

awsAlias.serverless.service = new awsAlias.serverless.classes.Service(awsAlias.serverless, service);
Expand Down

0 comments on commit 61ad807

Please sign in to comment.