From 5ff81fb318a741f7d58f83ae4e550717c4d3bbbc Mon Sep 17 00:00:00 2001 From: Danilo Campana Fuchs Date: Mon, 18 May 2020 16:02:25 -0300 Subject: [PATCH 1/3] fix: Resource path should contain wildcards --- src/events/http/createAuthScheme.js | 2 +- src/events/http/lambda-events/LambdaProxyIntegrationEvent.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/events/http/createAuthScheme.js b/src/events/http/createAuthScheme.js index 61be9efa8..317356593 100644 --- a/src/events/http/createAuthScheme.js +++ b/src/events/http/createAuthScheme.js @@ -44,7 +44,7 @@ export default function createAuthScheme(authorizerOptions, provider, lambda) { const accountId = 'random-account-id' const apiId = 'random-api-id' const httpMethod = request.method.toUpperCase() - const resourcePath = request.path.replace( + const resourcePath = request.route.path.replace( new RegExp(`^/${provider.stage}`), '', ) diff --git a/src/events/http/lambda-events/LambdaProxyIntegrationEvent.js b/src/events/http/lambda-events/LambdaProxyIntegrationEvent.js index e1314785d..94162143a 100644 --- a/src/events/http/lambda-events/LambdaProxyIntegrationEvent.js +++ b/src/events/http/lambda-events/LambdaProxyIntegrationEvent.js @@ -186,7 +186,7 @@ export default class LambdaProxyIntegrationEvent { resourcePath: route.path, stage: this.#stage, }, - resource: this.#path, + resource: route.path, stageVariables: this.#stageVariables, } } From 578ecc907aee2f9309440e104233a6b067e45ad2 Mon Sep 17 00:00:00 2001 From: Danilo Campana Fuchs Date: Wed, 3 Jun 2020 16:55:01 -0300 Subject: [PATCH 2/3] feat: Add tests for resource variable --- tests/integration/handler/handler.js | 7 +++++++ tests/integration/handler/handlerPayload.test.js | 7 +++++++ tests/integration/handler/serverless.yml | 7 +++++++ 3 files changed, 21 insertions(+) diff --git a/tests/integration/handler/handler.js b/tests/integration/handler/handler.js index 20a4b90ff..38073567c 100644 --- a/tests/integration/handler/handler.js +++ b/tests/integration/handler/handler.js @@ -202,3 +202,10 @@ exports.TestPathVariable = (event, context, callback) => { body: stringify(event.path), }) } + +exports.TestResourceVariable = (event, context, callback) => { + callback(null, { + statusCode: 200, + body: stringify(event.resource), + }) +} diff --git a/tests/integration/handler/handlerPayload.test.js b/tests/integration/handler/handlerPayload.test.js index a46c2d7cd..c9c7f666a 100644 --- a/tests/integration/handler/handlerPayload.test.js +++ b/tests/integration/handler/handlerPayload.test.js @@ -301,6 +301,13 @@ describe('handler payload tests with prepend off', () => { path: '/test-path-variable-handler', status: 200, }, + + { + description: 'event.resource should not contain wildcards', + expected: '/{id}/test-resource-variable-handler', + path: '/1/test-resource-variable-handler', + status: 200, + }, ].forEach(({ description, expected, path, status }) => { test(description, async () => { const url = joinUrl(TEST_BASE_URL, path) diff --git a/tests/integration/handler/serverless.yml b/tests/integration/handler/serverless.yml index 4660d7fa9..36aa123b5 100644 --- a/tests/integration/handler/serverless.yml +++ b/tests/integration/handler/serverless.yml @@ -155,3 +155,10 @@ functions: method: get path: test-path-variable-handler handler: handler.TestPathVariable + + TestResourceVariable: + events: + - http: + method: get + path: /{id}/test-resource-variable-handler + handler: handler.TestResourceVariable From efc4866f583bb84b0a43c559d24e10604087cc8a Mon Sep 17 00:00:00 2001 From: Danilo Campana Fuchs Date: Wed, 3 Jun 2020 17:06:41 -0300 Subject: [PATCH 3/3] fix: Add missing resource and path variables in auth event Addresses https://github.com/dherault/serverless-offline/issues/917#issuecomment-638424551 --- src/events/http/createAuthScheme.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/events/http/createAuthScheme.js b/src/events/http/createAuthScheme.js index 317356593..524978e93 100644 --- a/src/events/http/createAuthScheme.js +++ b/src/events/http/createAuthScheme.js @@ -59,8 +59,10 @@ export default function createAuthScheme(authorizerOptions, provider, lambda) { requestId: 'random-request-id', resourceId: 'random-resource-id', resourcePath, + path: request.path, stage: provider.stage, }, + resource: resourcePath, } // Create event Object for authFunction