From 61ad807df53fc92a5d8736af93472368b2071c64 Mon Sep 17 00:00:00 2001 From: Frank Schmid Date: Tue, 21 Nov 2017 11:46:28 +0100 Subject: [PATCH] Support ANY method type for stage configuration 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 --- .gitignore | 1 + lib/stackops/apiGateway.js | 16 +++++++++-- test/stackops/apiGateway.test.js | 46 ++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 7bbe1a3..0fd21d3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /node_modules /coverage +/.nyc_output diff --git a/lib/stackops/apiGateway.js b/lib/stackops/apiGateway.js index 181d5ad..d497aa0 100644 --- a/lib/stackops/apiGateway.js +++ b/lib/stackops/apiGateway.js @@ -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}'`); @@ -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)); + }); } } }); diff --git a/test/stackops/apiGateway.test.js b/test/stackops/apiGateway.test.js index 4e5a859..3b9b450 100644 --- a/test/stackops/apiGateway.test.js +++ b/test/stackops/apiGateway.test.js @@ -223,6 +223,17 @@ describe('API Gateway', () => { } ] }, + functionC: { + handler: 'functionB.handler', + events: [ + { + http: { + method: 'ANY', + path: '/funcC' + } + }, + ] + }, } }; const expectedMethodSettings = [ @@ -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);