From a987feee3f8b3d03972b0756b22f9f363f65e739 Mon Sep 17 00:00:00 2001 From: Christian Hoffmann <352753+hffmnn@users.noreply.github.com> Date: Mon, 31 May 2021 08:29:39 +0200 Subject: [PATCH] fix: make headers an object instead of an array (#386) * fix: make headers an object instead of an array * fix: add unit test Co-authored-by: Christian Hoffmann <352753+hffmnn@users.noreply.github.co> --- __tests__/unit.api-gateway-v2.js | 21 +++++++++++++++++++++ src/event-sources/aws/api-gateway-v2.js | 2 +- src/event-sources/utils.test.ts | 4 ++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 __tests__/unit.api-gateway-v2.js diff --git a/__tests__/unit.api-gateway-v2.js b/__tests__/unit.api-gateway-v2.js new file mode 100644 index 00000000..0cafa7ac --- /dev/null +++ b/__tests__/unit.api-gateway-v2.js @@ -0,0 +1,21 @@ +const eventSources = require('../src/event-sources') +const testUtils = require('../src/event-sources/utils.test') + +const apiGatewayEventSource = eventSources.getEventSource({ + eventSourceName: 'AWS_API_GATEWAY_V2' +}) + +test('request has correct headers', () => { + const req = getReq() + // see https://github.com/vendia/serverless-express/issues/387 + expect(typeof req).toEqual('object') + expect(JSON.stringify(req.headers)).toEqual( + '{"cookie":"","host":"localhost:9000","user-agent":"curl/7.64.1","accept":"*/*","x-forwarded-proto":"http","x-forwarded-port":"9000"}' + ) +}) + +function getReq () { + const event = testUtils.sam_httpapi_event + const request = apiGatewayEventSource.getRequest({ event }) + return request +} diff --git a/src/event-sources/aws/api-gateway-v2.js b/src/event-sources/aws/api-gateway-v2.js index 0fb30640..56896c11 100644 --- a/src/event-sources/aws/api-gateway-v2.js +++ b/src/event-sources/aws/api-gateway-v2.js @@ -18,7 +18,7 @@ function getRequestValuesFromApiGatewayEvent ({ event }) { search: rawQueryString }) - const headers = [] + const headers = {} if (cookies) { headers.cookie = cookies.join('; ') diff --git a/src/event-sources/utils.test.ts b/src/event-sources/utils.test.ts index acfd475d..b37f742d 100644 --- a/src/event-sources/utils.test.ts +++ b/src/event-sources/utils.test.ts @@ -50,3 +50,7 @@ describe("getEventSourceNameBasedOnEvent", () => { expect(result).toEqual("AWS_API_GATEWAY_V2"); }); }); + +module.exports = { + sam_httpapi_event, +};