Skip to content

Commit

Permalink
test: add ALB tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-vendia committed Jan 31, 2021
1 parent 9ccd8a0 commit 09e375b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
19 changes: 18 additions & 1 deletion jest-helpers/alb-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ function makeAlbEvent (values = {}) {
return mergedEvent
}

function makeAlbResponse (values = {}) {
const baseResponse = {
body: '',
isBase64Encoded: false,
statusCode: 200,
multiValueHeaders: {
'content-type': ['application/json; charset=utf-8'],
'x-powered-by': ['Express']
}
}
const mergedResponse = mergeDeep(baseResponse, values)
delete mergedResponse.cookies

return mergedResponse
}

module.exports = {
makeAlbEvent
makeAlbEvent,
makeAlbResponse
}
14 changes: 7 additions & 7 deletions jest-helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const {makeApiGatewayV1Event, makeApiGatewayV1Response} = require('./api-gateway-v1-event')
const {makeApiGatewayV2Event, makeApiGatewayV2Response} = require('./api-gateway-v2-event')
const {makeAlbEvent} = require('./alb-event')
const {makeLambdaEdgeEvent} = require('./lambda-edge-event.js')
const { makeApiGatewayV1Event, makeApiGatewayV1Response } = require('./api-gateway-v1-event')
const { makeApiGatewayV2Event, makeApiGatewayV2Response } = require('./api-gateway-v2-event')
const { makeAlbEvent, makeAlbResponse } = require('./alb-event')
const { makeLambdaEdgeEvent, makeLambdaEdgeResponse} = require('./lambda-edge-event.js')

const EVENT_SOURCE_NAMES = [
// 'alb',
'alb',
'apiGatewayV1',
'apiGatewayV2'
// 'lambdaEdge'
Expand Down Expand Up @@ -55,13 +55,13 @@ function makeEvent ({ eventSourceName, ...rest }) {
function makeResponse ({ eventSourceName, ...rest }, { shouldConvertContentLengthToInt = false } = {}) {
switch (eventSourceName) {
case 'alb':
return makeAlbEvent(rest)
return makeAlbResponse(rest)
case 'apiGatewayV1':
return makeApiGatewayV1Response(rest)
case 'apiGatewayV2':
return makeApiGatewayV2Response(rest, {shouldConvertContentLengthToInt})
case 'lambdaEdge':
return makeLambdaEdgeEvent(rest)
return makeLambdaEdgeResponse(rest)
default:
throw new Error(`Unknown eventSourceName ${eventSourceName}`)
}
Expand Down
6 changes: 4 additions & 2 deletions src/event-sources/aws/lambda-edge.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Lambda@Edge fails if certain headers are returned
const RESPONSE_HEADERS_DENY_LIST = ['content-length']

function getRequestValuesFromLambdaEdgeEvent ({ event }) {
const cloudFormationRequest = event.Records[0].cf.request
const {
Expand Down Expand Up @@ -45,8 +48,7 @@ function getResponseToLambdaEdge ({
const headersMap = {}
Object.entries(headers).forEach(([headerKey, headerValue]) => {
const headerKeyLowerCase = headerKey.toLowerCase()
// Lambda@Edge fails if you include content-length
if (headerKeyLowerCase === 'content-length') return
if (RESPONSE_HEADERS_DENY_LIST.includes(headerKeyLowerCase)) return
if (!headersMap[headerKeyLowerCase]) headersMap[headerKeyLowerCase] = []

headersMap[headerKeyLowerCase].push({
Expand Down

0 comments on commit 09e375b

Please sign in to comment.