Skip to content

Commit

Permalink
fix: fix Lambda@Edge headers and body
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-vendia committed Feb 1, 2021
1 parent 09e375b commit aabd9d3
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 186 deletions.
11 changes: 6 additions & 5 deletions __tests__/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
test('GET HTML', async () => {
router.get('/', (req, res) => {
const currentInvoke = serverlessExpress.getCurrentInvoke()
const eventPath = currentInvoke.event.path || currentInvoke.event.rawPath
const eventPath = currentInvoke.event.path || currentInvoke.event.rawPath || currentInvoke.event.Records[0].cf.request.uri
res.render('index', {
path: eventPath
})
Expand Down Expand Up @@ -68,9 +68,9 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
test('GET JSON', async () => {
const jsonResponse = { data: { name: 'Brett' } }
router.get('/users', (req, res) => {
res.set('X-Custom-Header', 'test')
res.json(jsonResponse)
})

const event = makeEvent({
eventSourceName,
path: '/users',
Expand All @@ -82,13 +82,14 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
body: JSON.stringify(jsonResponse),
multiValueHeaders: {
'content-length': ['25'],
etag: ['W/"19-dkLV0OMoaMM+tzXUD50EB/AHHoI"']
etag: ['W/"19-dkLV0OMoaMM+tzXUD50EB/AHHoI"'],
'x-custom-header': ['test']
}
})
expect(response).toEqual(expectedResponse)
})

test('GET JSON (resolutionMode = CALLBACK)', (done) => {
test('resolutionMode = CALLBACK', (done) => {
const jsonResponse = { data: { name: 'Brett' } }
router.get('/users', (req, res) => {
res.json(jsonResponse)
Expand All @@ -115,7 +116,7 @@ describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, framewo
serverlessExpressInstanceWithCallbackResolutionMode.handler(event, {}, callback)
})

test('GET JSON (resolutionMode = CONTEXT)', (done) => {
test('resolutionMode = CONTEXT', (done) => {
const jsonResponse = { data: { name: 'Brett' } }
router.get('/users', (req, res) => {
res.json(jsonResponse)
Expand Down
6 changes: 3 additions & 3 deletions examples/lambda-edge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"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 && (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"
"build": "webpack-cli && rm ./dist/main.js.LICENSE.txt"
},
"license": "Apache-2.0",
"dependencies": {
"@vendia/serverless-express": "^4.0.0-rc.7",
"@vendia/serverless-express": "^4.0.0-rc.12",
"body-parser": "^1.19.0",
"compression": "^1.7.4",
"cors": "^2.8.5",
Expand All @@ -39,4 +39,4 @@
"webpack-cli": "^4.3.1",
"webpack-node-externals": "^2.5.2"
}
}
}
7 changes: 4 additions & 3 deletions examples/lambda-edge/sam-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Resources:
QueryString: true
ViewerProtocolPolicy: redirect-to-https
LambdaFunctionAssociations:
- EventType: viewer-request # TODO: test origin-request
# TODO: loadtest origin-request vs viewer-request
- EventType: origin-request
LambdaFunctionARN: !Ref ExpressLambdaFunction.Version
IncludeBody: true
AllowedMethods:
Expand All @@ -38,8 +39,8 @@ Resources:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./dist
Handler: lambda.handler
MemorySize: 1024
Handler: main.handler
MemorySize: 128
Runtime: nodejs12.x
Timeout: 5
Role: !GetAtt ExpressLambdaRole.Arn
Expand Down
1 change: 0 additions & 1 deletion examples/lambda-edge/scripts/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const callback = (e, v) => {
if (v) console.info(v)
process.exit(0)
}
console.log('lambdaFunction', lambdaFunction)
lambdaFunction.handler(apiGatewayEvent, context, callback)

process.stdin.resume()
Expand Down
1 change: 0 additions & 1 deletion examples/lambda-edge/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ router.get('/sam', (req, res) => {
})

router.get('/users', (req, res) => {
console.log('users', users)
res.json(users)
})

Expand Down
7 changes: 4 additions & 3 deletions examples/lambda-edge/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ module.exports = {
entry: './src/lambda.js',
target: 'node',
mode: 'production',
// devtool: 'source-map',
devtool: 'source-map',
output: {
path: path.join(__dirname, 'dist'),
filename: 'lambda.js',
filename: '[name].js',
// library: 'serverlessExpressEdge',
libraryTarget: 'commonjs2'
},
plugins: [
new CopyPlugin({
patterns: [
{ from: './src/views', to: 'views' }
{ from: './src/views', to: 'views' },
{ from: './src/sam-logo.png' },
]
})
]
Expand Down
4 changes: 2 additions & 2 deletions jest-helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const { makeLambdaEdgeEvent, makeLambdaEdgeResponse} = require('./lambda-edge-ev
const EVENT_SOURCE_NAMES = [
'alb',
'apiGatewayV1',
'apiGatewayV2'
// 'lambdaEdge'
'apiGatewayV2',
'lambdaEdge'
]

const FRAMEWORK_NAMES = [
Expand Down
Loading

0 comments on commit aabd9d3

Please sign in to comment.