Skip to content

Commit

Permalink
Write tests in runtime_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Apr 11, 2024
1 parent 389da2c commit d51f022
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 70 deletions.
101 changes: 101 additions & 0 deletions runtime_tests/lambda/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ describe('AWS Lambda Adapter for Hono', () => {
return c.text('Valid Cookies')
})

app.post('/headers', (c) => {
if (c.req.header('foo')?.includes('bar')) {
return c.json({ message: 'ok' })
}
return c.json({ message: 'fail' }, 400)
})

const handler = handle(app)

const testApiGatewayRequestContext = {
Expand Down Expand Up @@ -491,6 +498,100 @@ describe('AWS Lambda Adapter for Hono', () => {
])
})

describe('headers', () => {
describe('single-value headers', () => {
it('Should extract single-value headers and return 200 (ALBProxyEvent)', async () => {
const event = {
body: '{}',
httpMethod: 'POST',
isBase64Encoded: false,
path: '/headers',
headers: {
host: 'localhost',
foo: 'bar',
},
requestContext: testALBRequestContext,
}
const apiGatewayResponseV2 = await handler(event)
expect(apiGatewayResponseV2.statusCode).toBe(200)
})

it('Should extract single-value headers and return 200 (APIGatewayProxyEvent)', async () => {
const apigatewayProxyEvent = {
version: '1.0',
resource: '/headers',
httpMethod: 'POST',
headers: {
host: 'localhost',
foo: 'bar',
},
path: '/headers',
body: null,
isBase64Encoded: false,
requestContext: testApiGatewayRequestContext,
}
const apiGatewayResponseV2 = await handler(apigatewayProxyEvent)
expect(apiGatewayResponseV2.statusCode).toBe(200)
})

it('Should extract single-value headers and return 200 (APIGatewayProxyEventV2)', async () => {
const apigatewayProxyV2Event = {
version: '2.0',
routeKey: '$default',
headers: {
host: 'localhost',
foo: 'bar',
},
rawPath: '/headers',
rawQueryString: '',
requestContext: testApiGatewayRequestContextV2,
resource: '/headers',
body: null,
isBase64Encoded: false,
}
const apiGatewayResponseV2 = await handler(apigatewayProxyV2Event)
expect(apiGatewayResponseV2.statusCode).toBe(200)
})
})

describe('multi-value headers', () => {
it('Should extract multi-value headers and return 200 (ALBProxyEvent)', async () => {
const event = {
body: '{}',
httpMethod: 'POST',
isBase64Encoded: false,
path: '/headers',
multiValueHeaders: {
host: ['localhost'],
foo: ['bar'],
},
requestContext: testALBRequestContext,
}
const apiGatewayResponseV2 = await handler(event)
expect(apiGatewayResponseV2.statusCode).toBe(200)
})

it('Should extract multi-value headers and return 200 (APIGatewayProxyEvent)', async () => {
const apigatewayProxyEvent = {
version: '1.0',
resource: '/headers',
httpMethod: 'POST',
headers: {},
multiValueHeaders: {
host: ['localhost'],
foo: ['bar'],
},
path: '/headers',
body: null,
isBase64Encoded: false,
requestContext: testApiGatewayRequestContext,
}
const apiGatewayResponseV2 = await handler(apigatewayProxyEvent)
expect(apiGatewayResponseV2.statusCode).toBe(200)
})
})
})

it('Should handle a POST request and return a 200 response if cookies match (APIGatewayProxyEvent V1 and V2)', async () => {
const apiGatewayEvent = {
version: '1.0',
Expand Down
70 changes: 1 addition & 69 deletions src/adapter/aws-lambda/handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Hono } from '../../hono'
import { isContentTypeBinary, isContentEncodingBinary, handle } from './handler'
import type { ALBProxyEvent } from './handler'
import { isContentTypeBinary, isContentEncodingBinary } from './handler'

describe('isContentTypeBinary', () => {
it('Should determine whether it is binary', () => {
Expand Down Expand Up @@ -29,69 +27,3 @@ describe('isContentEncodingBinary', () => {
expect(isContentEncodingBinary('unknown')).toBe(false)
})
})

describe('handle', () => {
const dummyLambdaContext = {
awsRequestId: '',
callbackWaitsForEmptyEventLoop: false,
functionName: '',
functionVersion: '',
invokedFunctionArn: '',
logGroupName: '',
logStreamName: '',
memoryLimitInMB: '',
getRemainingTimeInMillis(): number {
return 0
},
}

describe('ALB', () => {
const app = new Hono().post('/', (c) => {
if (c.req.header('foo')?.includes('bar')) {
return c.json({ message: 'ok' })
}
return c.json({ message: 'fail' }, 400)
})
const handler = handle(app)

it('Should accept single value headers', async () => {
const event: ALBProxyEvent = {
body: '{}',
httpMethod: 'POST',
isBase64Encoded: false,
path: '/',
headers: {
host: 'localhost',
foo: 'bar',
},
requestContext: {
elb: {
targetGroupArn: '',
},
},
}
const response = await handler(event, dummyLambdaContext)
expect(response?.['statusCode']).toEqual(200)
})

it('Should accept multi value headers', async () => {
const event: ALBProxyEvent = {
body: '{}',
httpMethod: 'POST',
isBase64Encoded: false,
path: '/',
multiValueHeaders: {
host: ['localhost'],
foo: ['bar', 'buz'],
},
requestContext: {
elb: {
targetGroupArn: '',
},
},
}
const response = await handler(event, dummyLambdaContext)
expect(response?.['statusCode']).toEqual(200)
})
})
})
3 changes: 2 additions & 1 deletion src/adapter/aws-lambda/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ const createRequest = (event: LambdaEvent) => {
headers.set(k, v)
}
}
} else if (event.multiValueHeaders) {
}
if (event.multiValueHeaders) {
for (const [k, values] of Object.entries(event.multiValueHeaders)) {
if (values) {
values.forEach((v) => headers.append(k, v))
Expand Down

0 comments on commit d51f022

Please sign in to comment.