Skip to content

Commit

Permalink
fix: fix API Gateway V2 header response
Browse files Browse the repository at this point in the history
closes #352
  • Loading branch information
brett-vendia committed Jan 27, 2021
1 parent 50dbee7 commit 90bad18
Show file tree
Hide file tree
Showing 25 changed files with 1,376 additions and 890 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ npm-debug.log
coverage
sam-template.packaged.yaml
vendia-serverless-express-*.tgz
dist/
dist/
examples/nextjs
65 changes: 3 additions & 62 deletions __tests__/integration.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,10 @@
const path = require('path')
const fs = require('fs')
const serverlessExpress = require('../src/index')
const apiGatewayEvent = require('../examples/basic-starter-api-gateway-v1/api-gateway-event.json')
const app = require('../examples/basic-starter-api-gateway-v1/src/app')

const log = {
info: () => null,
debug: () => null,
error: () => null
}
const { log, makeEvent, expectedRootResponse, makeResponse } = require('../jest-helpers')

const serverlessExpressInstance = serverlessExpress({ app, log })
const nodeMajorVersion = process.version.split('.')[0].split('v')[1]

function clone (json) {
return JSON.parse(JSON.stringify(json))
}

function makeEvent (eventOverrides = {}) {
const baseEvent = clone(apiGatewayEvent)
const multiValueHeaders = {
...baseEvent.multiValueHeaders,
...eventOverrides.multiValueHeaders
}
const root = {
...baseEvent,
...eventOverrides
}
root.multiValueHeaders = multiValueHeaders
root.pathParameters.proxy = eventOverrides.path && eventOverrides.path.replace(/^\//, '')
return root
}

function expectedRootResponse () {
return makeResponse({
multiValueHeaders: {
'content-length': ['4659'],
'content-type': ['text/html; charset=utf-8'],
etag: ['W/"1233-UcgIN3SD7YNLl2bLEKDbkMGu6io"']
}
})
}

function makeResponse (response) {
const baseResponse = {
body: '',
isBase64Encoded: false,
statusCode: 200
}
const baseHeaders = {
'access-control-allow-origin': ['*'],
'content-type': ['application/json; charset=utf-8'],
'x-powered-by': ['Express']
}
const multiValueHeaders = {
...baseHeaders,
...response.multiValueHeaders
}
const finalResponse = {
...baseResponse,
...response
}
finalResponse.multiValueHeaders = multiValueHeaders
return finalResponse
}

describe('integration tests', () => {
test('handler returns promise', () => {
Expand Down Expand Up @@ -352,10 +293,10 @@ describe('integration tests', () => {
}))
delete response.multiValueHeaders.date

const expectedSetCookieHeaders = nodeMajorVersion >= 10 ? [
const expectedSetCookieHeaders = [
'Foo=bar; Path=/',
'Fizz=buzz; Path=/'
] : ['Foo=bar; Path=/,Fizz=buzz; Path=/']
]
expect(response).toEqual(makeResponse({
body: '{}',
multiValueHeaders: {
Expand Down
17 changes: 2 additions & 15 deletions __tests__/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ const makeResolver = require('../src/make-resolver')
const ServerlessRequest = require('../src/request')
const ServerlessResponse = require('../src/response')
const expressFramework = require('../src/frameworks/express')
const log = {
info: () => null,
debug: () => null,
error: () => null
}
const { log, MockContext } = require('../jest-helpers')

const apiGatewayEventSource = eventSources.getEventFnsBasedOnEventSource({ eventSource: 'AWS_API_GATEWAY_V1' })

test('getPathWithQueryStringParams: no params', () => {
Expand Down Expand Up @@ -115,16 +112,6 @@ test('getEventFnsBasedOnEventSource: without headers', async (done) => {
done()
})

class MockContext {
constructor (resolve) {
this.resolve = resolve
}

succeed (successResponse) {
this.resolve(successResponse)
}
}

describe('respondToEventSourceWithError', () => {
test('responds with 500 status', () => {
return new Promise(
Expand Down
2 changes: 1 addition & 1 deletion examples/alb/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Resources:
Properties:
CodeUri: ./src
Handler: lambda.handler
MemorySize: 128
MemorySize: 1024
Runtime: nodejs12.x
Timeout: 30

Expand Down
4 changes: 2 additions & 2 deletions examples/basic-starter-api-gateway-v1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"deploy": "cross-var aws cloudformation deploy --template-file sam-template.packaged.yaml --stack-name $npm_package_config_cloudFormationStackName --capabilities CAPABILITY_IAM --region $npm_package_config_region --parameter-overrides DomainName=$npm_package_config_domain",
"package-deploy": "npm run build && npm run package && npm run deploy",
"delete-stack": "cross-var aws cloudformation delete-stack --stack-name $npm_package_config_cloudFormationStackName --region $npm_package_config_region",
"setup": "npm install && npm run build && (cross-var aws s3api get-bucket-location --bucket $npm_package_config_s3BucketName --region $npm_package_config_region || npm run create-bucket) && npm run package-deploy",
"setup": "npm install && (cross-var aws s3api get-bucket-location --bucket $npm_package_config_s3BucketName --region $npm_package_config_region || npm run create-bucket) && npm run package-deploy",
"build": "webpack-cli"
},
"license": "Apache-2.0",
Expand All @@ -39,4 +39,4 @@
"webpack": "^5.11.1",
"webpack-cli": "^4.3.1"
}
}
}
2 changes: 1 addition & 1 deletion examples/basic-starter-api-gateway-v1/sam-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Resources:
Properties:
CodeUri: ./dist
Handler: main.handler
MemorySize: 128
MemorySize: 1024
Runtime: nodejs12.x
Timeout: 30
Events:
Expand Down

0 comments on commit 90bad18

Please sign in to comment.