index.test.ts
This file
Path
runtime_tests/lambda/index.test.ts
285 lines 路 8.2 KiB 路 TypeScript
Last changed by
fix(adaptor): Access to custom request context on AWS Lambda Adaptor (#1305)watanybefore adoptioneefdb27
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286import type { ApiGatewayRequestContext, LambdaFunctionUrlRequestContext,} from '../../src/adapter/aws-lambda/custom-context'import { handle } from '../../src/adapter/aws-lambda/handler'import { Hono } from '../../src/hono'import { basicAuth } from '../../src/middleware/basic-auth'
type Bindings = { requestContext: ApiGatewayRequestContext | LambdaFunctionUrlRequestContext}
describe('AWS Lambda Adapter for Hono', () => { const app = new Hono<{ Bindings: Bindings }>()
app.get('/', (c) => { return c.text('Hello Lambda!') })
app.get('/binary', (c) => { return c.body('Fake Image', 200, { 'Content-Type': 'image/png', }) })
app.post('/post', async (c) => { const body = (await c.req.parseBody()) as { message: string } return c.text(body.message) })
app.post('/post/binary', async (c) => { const body = await c.req.blob() return c.text(`${body.size} bytes`) })
const username = 'hono-user-a' const password = 'hono-password-a' app.use('/auth/*', basicAuth({ username, password })) app.get('/auth/abc', (c) => c.text('Good Night Lambda!'))
app.get('/custom-context/apigw', (c) => { const lambdaContext = c.env.requestContext return c.json(lambdaContext) })
app.get('/custom-context/lambda', (c) => { const lambdaContext = c.env.requestContext return c.json(lambdaContext) })
const handler = handle(app)
const testApiGatewayRequestContext = { accountId: '123456789012', apiId: 'id', authorizer: { claims: null, scopes: null, }, domainName: 'example.com', domainPrefix: 'id', extendedRequestId: 'request-id', httpMethod: 'GET', identity: { sourceIp: 'IP', userAgent: 'user-agent', }, path: '/my/path', protocol: 'HTTP/1.1', requestId: 'id=', requestTime: '04/Mar/2020:19:15:17 +0000', requestTimeEpoch: 1583349317135, resourcePath: '/', stage: '$default', customProperty: 'customValue', }
const testLambdaFunctionUrlRequestContext = { accountId: '123456789012', apiId: 'urlid', authentication: null, authorizer: { iam: { accessKey: 'AKIA...', accountId: '111122223333', callerId: 'AIDA...', cognitoIdentity: null, principalOrgId: null, userArn: 'arn:aws:iam::111122223333:user/example-user', userId: 'AIDA...', }, }, domainName: 'example.com', domainPrefix: '<url-id>', http: { method: 'POST', path: '/my/path', protocol: 'HTTP/1.1', sourceIp: '123.123.123.123', userAgent: 'agent', }, requestId: 'id', routeKey: '$default', stage: '$default', time: '12/Mar/2020:19:03:58 +0000', timeEpoch: 1583348638390, customProperty: 'customValue', }
it('Should handle a GET request and return a 200 response', async () => { const event = { httpMethod: 'GET', headers: { 'content-type': 'text/plain' }, path: '/', body: null, isBase64Encoded: false, requestContext: testApiGatewayRequestContext, }
const response = await handler(event) expect(response.statusCode).toBe(200) expect(response.body).toBe('Hello Lambda!') expect(response.headers['content-type']).toMatch(/^text\/plain/) expect(response.isBase64Encoded).toBe(false) })
it('Should handle a GET request and return a 200 response with binary', async () => { const event = { httpMethod: 'GET', headers: {}, path: '/binary', body: null, isBase64Encoded: false, requestContext: testApiGatewayRequestContext, }
const response = await handler(event) expect(response.statusCode).toBe(200) expect(response.body).toBe('RmFrZSBJbWFnZQ==') expect(response.headers['content-type']).toMatch(/^image\/png/) expect(response.isBase64Encoded).toBe(true) })
it('Should handle a GET request and return a 200 response (LambdaFunctionUrlEvent)', async () => { const event = { headers: { 'content-type': 'text/plain' }, rawPath: '/', rawQueryString: '', body: null, isBase64Encoded: false, requestContext: testLambdaFunctionUrlRequestContext, }
testLambdaFunctionUrlRequestContext.http.method = 'GET'
const response = await handler(event) expect(response.statusCode).toBe(200) expect(response.body).toBe('Hello Lambda!') expect(response.headers['content-type']).toMatch(/^text\/plain/) expect(response.isBase64Encoded).toBe(false) })
it('Should handle a GET request and return a 404 response', async () => { const event = { httpMethod: 'GET', headers: { 'content-type': 'text/plain' }, path: '/nothing', body: null, isBase64Encoded: false, requestContext: testApiGatewayRequestContext, }
const response = await handler(event) expect(response.statusCode).toBe(404) })
it('Should handle a POST request and return a 200 response', async () => { const searchParam = new URLSearchParams() searchParam.append('message', 'Good Morning Lambda!') const event = { httpMethod: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, path: '/post', body: Buffer.from(searchParam.toString()).toString('base64'), isBase64Encoded: true, requestContext: testApiGatewayRequestContext, }
const response = await handler(event) expect(response.statusCode).toBe(200) expect(response.body).toBe('Good Morning Lambda!') })
it('Should handle a POST request and return a 200 response (LambdaFunctionUrlEvent)', async () => { const searchParam = new URLSearchParams() searchParam.append('message', 'Good Morning Lambda!') const event = { headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, rawPath: '/post', rawQueryString: '', body: Buffer.from(searchParam.toString()).toString('base64'), isBase64Encoded: true, requestContext: testLambdaFunctionUrlRequestContext, }
testLambdaFunctionUrlRequestContext.http.method = 'POST'
const response = await handler(event) expect(response.statusCode).toBe(200) expect(response.body).toBe('Good Morning Lambda!') })
it('Should handle a POST request with binary and return a 200 response', async () => { const array = new Uint8Array([0xc0, 0xff, 0xee]) const buffer = Buffer.from(array) const event = { httpMethod: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, path: '/post/binary', body: buffer.toString('base64'), isBase64Encoded: true, requestContext: testApiGatewayRequestContext, }
const response = await handler(event) expect(response.statusCode).toBe(200) expect(response.body).toBe('3 bytes') })
it('Should handle a request and return a 401 response with Basic auth', async () => { const event = { httpMethod: 'GET', headers: { 'Content-Type': 'plain/text', }, path: '/auth/abc', body: null, isBase64Encoded: true, requestContext: testApiGatewayRequestContext, }
const response = await handler(event) expect(response.statusCode).toBe(401) })
it('Should handle a request and return a 200 response with Basic auth', async () => { const credential = 'aG9uby11c2VyLWE6aG9uby1wYXNzd29yZC1h' const event = { httpMethod: 'GET', headers: { 'Content-Type': 'plain/text', Authorization: `Basic ${credential}`, }, path: '/auth/abc', body: null, isBase64Encoded: true, requestContext: testApiGatewayRequestContext, }
const response = await handler(event) expect(response.statusCode).toBe(200) expect(response.body).toBe('Good Night Lambda!') })
it('Should handle a GET request and return custom context', async () => { const event = { httpMethod: 'GET', headers: { 'content-type': 'application/json' }, path: '/custom-context/apigw', body: null, isBase64Encoded: false, requestContext: testApiGatewayRequestContext, }
const response = await handler(event) expect(response.statusCode).toBe(200) expect(JSON.parse(response.body).customProperty).toEqual('customValue') })})