Skip to content

Commit

Permalink
test: refactor in prep for running tests in eventSource:framework matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-vendia committed Jan 29, 2021
1 parent bcb594a commit d4624c0
Show file tree
Hide file tree
Showing 11 changed files with 563 additions and 162 deletions.
90 changes: 53 additions & 37 deletions __tests__/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,44 @@ const path = require('path')
const fs = require('fs')
const serverlessExpress = require('../src/index')
const app = require('../examples/basic-starter-api-gateway-v1/src/app')
const { log, makeEvent, expectedRootResponse, makeResponse } = require('../jest-helpers')
const { log, makeEvent, expectedRootResponse, makeResponse, EACH_MATRIX } = require('../jest-helpers')

const serverlessExpressInstance = serverlessExpress({ app, log })

describe('integration tests', () => {
describe.each(EACH_MATRIX)('%s:%s: integration tests', (eventSourceName, frameworkName) => {
test('handler returns promise', () => {
const response = serverlessExpressInstance.handler(makeEvent({
const event = makeEvent({
eventSourceName,
path: '/',
httpMethod: 'GET'
}))
})
const response = serverlessExpressInstance.handler(event)
expect(response.then).toBeTruthy()
})

test('GET HTML (initial request)', async () => {
const response = await serverlessExpressInstance.handler(makeEvent({ path: '/', httpMethod: 'GET' }))

delete response.multiValueHeaders.date
expect(response.body.startsWith('<!DOCTYPE html>')).toBe(true)
const expectedResponse = expectedRootResponse()
delete response.body
delete expectedResponse.body
expect(response).toEqual(expectedResponse)
})

test('GET HTML (subsequent request)', async () => {
const response = await serverlessExpressInstance.handler(makeEvent({
test('GET HTML', async () => {
const event = makeEvent({
eventSourceName,
path: '/',
httpMethod: 'GET'
}))
delete response.multiValueHeaders.date
})
const response = await serverlessExpressInstance.handler(event)

expect(response.body.startsWith('<!DOCTYPE html>')).toBe(true)
const expectedResponse = expectedRootResponse()
delete response.body
delete expectedResponse.body
delete response.multiValueHeaders.date
expect(response).toEqual(expectedResponse)
})

test('GET JSON collection', async () => {
const response = await serverlessExpressInstance.handler(makeEvent({
const event = makeEvent({
eventSourceName,
path: '/users',
httpMethod: 'GET'
}))
})
const response = await serverlessExpressInstance.handler(event)

delete response.multiValueHeaders.date
expect(response).toEqual(makeResponse({
Expand All @@ -56,10 +52,12 @@ describe('integration tests', () => {
})

test('GET missing route', async () => {
const response = await serverlessExpressInstance.handler(makeEvent({
const event = makeEvent({
eventSourceName,
path: '/nothing-here',
httpMethod: 'GET'
}))
})
const response = await serverlessExpressInstance.handler(event)

delete response.multiValueHeaders.date
expect(response.body.startsWith('<!DOCTYPE html>')).toBe(true)
Expand All @@ -78,10 +76,12 @@ describe('integration tests', () => {
})

test('GET JSON single', async () => {
const response = await serverlessExpressInstance.handler(makeEvent({
const event = makeEvent({
eventSourceName,
path: '/users/1',
httpMethod: 'GET'
}))
})
const response = await serverlessExpressInstance.handler(event)

delete response.multiValueHeaders.date
expect(response).toEqual(makeResponse({
Expand All @@ -107,6 +107,7 @@ describe('integration tests', () => {
}

const event = makeEvent({
eventSourceName,
path: '/users/1',
httpMethod: 'GET'
})
Expand All @@ -116,6 +117,7 @@ describe('integration tests', () => {

test('GET JSON single (resolutionMode = PROMISE)', async () => {
const event = makeEvent({
eventSourceName,
path: '/users/1',
httpMethod: 'GET'
})
Expand All @@ -133,10 +135,12 @@ describe('integration tests', () => {
})

test('GET JSON single 404', async () => {
const response = await serverlessExpressInstance.handler(makeEvent({
const event = makeEvent({
eventSourceName,
path: '/users/3',
httpMethod: 'GET'
}))
})
const response = await serverlessExpressInstance.handler(event)
delete response.multiValueHeaders.date
expect(response).toEqual(makeResponse({
body: '{}',
Expand All @@ -150,6 +154,7 @@ describe('integration tests', () => {

test('success - image response', async () => {
const event = makeEvent({
eventSourceName,
path: '/sam',
httpMethod: 'GET'
})
Expand Down Expand Up @@ -179,6 +184,7 @@ describe('integration tests', () => {

test('POST JSON', async () => {
const event = makeEvent({
eventSourceName,
path: '/users',
httpMethod: 'POST',
body: `{"name": "${newName}"}`
Expand All @@ -197,10 +203,12 @@ describe('integration tests', () => {
})

test('GET JSON single (again; post-creation) 200', async () => {
const response = await serverlessExpressInstance.handler(makeEvent({
const event = makeEvent({
eventSourceName,
path: '/users/3',
httpMethod: 'GET'
}))
})
const response = await serverlessExpressInstance.handler(event)
delete response.multiValueHeaders.date
expect(response).toEqual(makeResponse({
body: `{"id":3,"name":"${newName}"}`,
Expand All @@ -213,10 +221,12 @@ describe('integration tests', () => {
})

test('DELETE JSON', async () => {
const response = await serverlessExpressInstance.handler(makeEvent({
const event = makeEvent({
eventSourceName,
path: '/users/1',
httpMethod: 'DELETE'
}))
})
const response = await serverlessExpressInstance.handler(event)

delete response.multiValueHeaders.date
expect(response).toEqual(makeResponse({
Expand All @@ -230,11 +240,13 @@ describe('integration tests', () => {
})

test('PUT JSON', async () => {
const response = await serverlessExpressInstance.handler(makeEvent({
const event = makeEvent({
eventSourceName,
path: '/users/2',
httpMethod: 'PUT',
body: '{"name": "Samuel"}'
}))
})
const response = await serverlessExpressInstance.handler(event)
delete response.multiValueHeaders.date
expect(response).toEqual(makeResponse({
body: '{"id":2,"name":"Samuel"}',
Expand All @@ -247,12 +259,14 @@ describe('integration tests', () => {
})

test('base64 encoded request', async () => {
const response = await serverlessExpressInstance.handler(makeEvent({
const event = makeEvent({
eventSourceName,
path: '/users/2',
httpMethod: 'PUT',
body: global.btoa('{"name": "Samuel"}'),
isBase64Encoded: true
}))
})
const response = await serverlessExpressInstance.handler(event)
delete response.multiValueHeaders.date
expect(response).toEqual(makeResponse({
body: '{"id":2,"name":"Samuel"}',
Expand Down Expand Up @@ -287,10 +301,12 @@ describe('integration tests', () => {
})

test('Multiple headers of the same name (set-cookie)', async () => {
const response = await serverlessExpressInstance.handler(makeEvent({
const event = makeEvent({
eventSourceName,
path: '/cookie',
httpMethod: 'GET'
}))
})
const response = await serverlessExpressInstance.handler(event)
delete response.multiValueHeaders.date

const expectedSetCookieHeaders = [
Expand Down
122 changes: 0 additions & 122 deletions jest-helpers.js

This file was deleted.

40 changes: 40 additions & 0 deletions jest-helpers/alb-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const clone = require('./clone')
const mergeDeep = require('./merge-deep')

const albEvent = {
'requestContext': {
'elb': {
'targetGroupArn': 'arn:aws:elasticloadbalancing:us-east-1:347971939225:targetgroup/aws-s-Targe-RJF5FKWHX6Y8/29425aed99131fd0'
}
},
'httpMethod': 'POST',
'path': '/users',
'multiValueQueryStringParameters': {},
'multiValueHeaders': {
'accept': ['*/*'],
'accept-encoding': ['gzip, deflate'],
'accept-language': ['en-US,en;q=0.9'],
'cache-control': ['no-cache'],
'connection': ['keep-alive'],
'content-length': ['26'],
'content-type': ['application/json'],
'host': ['aws-ser-alb-p9y7dvwm0r42-2135869912.us-east-1.elb.amazonaws.com'],
'origin': ['http://aws-ser-alb-p9y7dvwm0r42-2135869912.us-east-1.elb.amazonaws.com'],
'pragma': ['no-cache'],
'referer': ['http://aws-ser-alb-p9y7dvwm0r42-2135869912.us-east-1.elb.amazonaws.com/'],
'user-agent': ['Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36'],
'x-amzn-trace-id': ['Root=1-5cdf3407-76d73870a87c746001f27090'],
'x-forwarded-for': ['72.21.198.66'],
'x-forwarded-port': ['80'],
'x-forwarded-proto': ['http']
},
'body': '{"name":"postmaaaateeeee"}',
'isBase64Encoded': false
}

module.exports = function makeAlbEvent (values = {}) {
const baseEvent = clone(albEvent)
const mergedEvent = mergeDeep(baseEvent, values)

return mergedEvent
}
Loading

0 comments on commit d4624c0

Please sign in to comment.