handler.test.ts
This file
Path
src/adapter/aws-lambda/handler.test.ts
275 lines 路 9.1 KiB 路 TypeScript
Last changed by
Merge pull request #4340 from honojs/nextYusuke Wadabefore adoption3a5248f
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276import type { LambdaEvent } from './handler'import { getProcessor, isContentEncodingBinary, defaultIsContentTypeBinary } from './handler'
describe('isContentTypeBinary', () => { it('Should determine whether it is binary', () => { expect(defaultIsContentTypeBinary('image/png')).toBe(true) expect(defaultIsContentTypeBinary('font/woff2')).toBe(true) expect(defaultIsContentTypeBinary('image/svg+xml')).toBe(false) expect(defaultIsContentTypeBinary('image/svg+xml; charset=UTF-8')).toBe(false) expect(defaultIsContentTypeBinary('text/plain')).toBe(false) expect(defaultIsContentTypeBinary('text/plain; charset=UTF-8')).toBe(false) expect(defaultIsContentTypeBinary('text/css')).toBe(false) expect(defaultIsContentTypeBinary('text/javascript')).toBe(false) expect(defaultIsContentTypeBinary('application/json')).toBe(false) expect(defaultIsContentTypeBinary('application/ld+json')).toBe(false) expect(defaultIsContentTypeBinary('application/json')).toBe(false) })})
describe('isContentEncodingBinary', () => { it('Should determine whether it is compressed', () => { expect(isContentEncodingBinary('gzip')).toBe(true) expect(isContentEncodingBinary('compress')).toBe(true) expect(isContentEncodingBinary('deflate')).toBe(true) expect(isContentEncodingBinary('br')).toBe(true) expect(isContentEncodingBinary('deflate, gzip')).toBe(true) expect(isContentEncodingBinary('')).toBe(false) expect(isContentEncodingBinary('unknown')).toBe(false) })})
describe('EventProcessor.createResult with contentTypsAsBinary', () => { const event: LambdaEvent = { version: '1.0', resource: '/my/path', path: '/my/path', httpMethod: 'GET', headers: {}, multiValueHeaders: {}, queryStringParameters: {}, requestContext: { accountId: '123456789012', apiId: 'id', authorizer: { claims: null, scopes: null }, domainName: 'id.execute-api.us-east-1.amazonaws.com', domainPrefix: 'id', extendedRequestId: 'request-id', httpMethod: 'GET', identity: { sourceIp: '192.0.2.1', userAgent: 'user-agent', clientCert: { clientCertPem: 'CERT_CONTENT', subjectDN: 'www.example.com', issuerDN: 'Example issuer', serialNumber: 'a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1', validity: { notBefore: 'May 28 12:30:02 2019 GMT', notAfter: 'Aug 5 09:36:04 2021 GMT', }, }, }, path: '/my/path', protocol: 'HTTP/1.1', requestId: 'id=', requestTime: '04/Mar/2020:19:15:17 +0000', requestTimeEpoch: 1583349317135, resourcePath: '/my/path', stage: '$default', }, pathParameters: {}, stageVariables: {}, body: null, isBase64Encoded: false, }
it('Should encode as base64 when content-type is in contentTypesAsBinary array', async () => { const processor = getProcessor(event) const response = new Response('test content', { headers: { 'content-type': 'application/custom' }, })
const result = await processor.createResult(event, response, { isContentTypeBinary: (contentType: string) => contentType === 'application/custom', })
expect(result.isBase64Encoded).toBe(true) expect(result.body).toBe('dGVzdCBjb250ZW50') })
it('Should not encode as base64 when content-type is not in isContentTypeBinary array', async () => { const processor = getProcessor(event) const response = new Response('test content', { headers: { 'content-type': 'application/json' }, })
const result = await processor.createResult(event, response, { isContentTypeBinary: (contentType: string) => contentType === 'application/custom', })
expect(result.isBase64Encoded).toBe(false) expect(result.body).toBe('test content') })
it('Should use defaultIsContentTypeBinary when isContentTypeBinary is undefined with binary content', async () => { const processor = getProcessor(event) const response = new Response('test image content', { headers: { 'content-type': 'image/png' }, })
// Pass undefined for isContentTypeBinary to test default behavior const result = await processor.createResult(event, response, { isContentTypeBinary: undefined, })
expect(result.isBase64Encoded).toBe(true) expect(result.body).toBe('dGVzdCBpbWFnZSBjb250ZW50') })
it('Should use defaultIsContentTypeBinary when isContentTypeBinary is undefined with non-binary content', async () => { const processor = getProcessor(event) const response = new Response('test text content', { headers: { 'content-type': 'text/plain' }, })
// Pass undefined for isContentTypeBinary to test default behavior const result = await processor.createResult(event, response, { isContentTypeBinary: undefined, })
expect(result.isBase64Encoded).toBe(false) expect(result.body).toBe('test text content') })})
describe('EventProcessor.createRequest', () => { it('Should return valid Request object from version 1.0 API Gateway event', () => { const event: LambdaEvent = { version: '1.0', resource: '/my/path', path: '/my/path', httpMethod: 'GET', headers: { 'content-type': 'application/json', header1: 'value1', header2: 'value1', }, multiValueHeaders: { header1: ['value1'], header2: ['value1', 'value2', 'value3'], }, // This value doesn't match multi value's content. // We want to assert handler is using the multi value's content when both are available. queryStringParameters: { parameter2: 'value', }, multiValueQueryStringParameters: { parameter1: ['value1', 'value2'], parameter2: ['value'], }, requestContext: { accountId: '123456789012', apiId: 'id', authorizer: { claims: null, scopes: null, }, domainName: 'id.execute-api.us-east-1.amazonaws.com', domainPrefix: 'id', extendedRequestId: 'request-id', httpMethod: 'GET', identity: { sourceIp: '192.0.2.1', userAgent: 'user-agent', clientCert: { clientCertPem: 'CERT_CONTENT', subjectDN: 'www.example.com', issuerDN: 'Example issuer', serialNumber: 'a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1', validity: { notBefore: 'May 28 12:30:02 2019 GMT', notAfter: 'Aug 5 09:36:04 2021 GMT', }, }, }, path: '/my/path', protocol: 'HTTP/1.1', requestId: 'id=', requestTime: '04/Mar/2020:19:15:17 +0000', requestTimeEpoch: 1583349317135, resourcePath: '/my/path', stage: '$default', }, pathParameters: {}, stageVariables: {}, body: null, isBase64Encoded: false, }
const processor = getProcessor(event) const request = processor.createRequest(event)
expect(request.method).toEqual('GET') expect(request.url).toEqual( 'https://id.execute-api.us-east-1.amazonaws.com/my/path?parameter1=value1¶meter1=value2¶meter2=value' ) expect(Object.fromEntries(request.headers)).toEqual({ 'content-type': 'application/json', header1: 'value1', header2: 'value1, value2, value3', }) })
it('Should return valid Request object from version 2.0 API Gateway event', () => { const event: LambdaEvent = { version: '2.0', routeKey: '$default', rawPath: '/my/path', rawQueryString: 'parameter1=value1¶meter1=value2¶meter2=value', cookies: ['cookie1', 'cookie2'], headers: { 'content-type': 'application/json', header1: 'value1', header2: 'value1,value2', }, queryStringParameters: { parameter1: 'value1,value2', parameter2: 'value', }, requestContext: { accountId: '123456789012', apiId: 'api-id', authentication: null, authorizer: {}, domainName: 'id.execute-api.us-east-1.amazonaws.com', domainPrefix: 'id', http: { method: 'POST', path: '/my/path', protocol: 'HTTP/1.1', sourceIp: '192.0.2.1', userAgent: 'agent', }, requestId: 'id', routeKey: '$default', stage: '$default', time: '12/Mar/2020:19:03:58 +0000', timeEpoch: 1583348638390, }, body: 'Hello from Lambda', pathParameters: { parameter1: 'value1', }, isBase64Encoded: false, stageVariables: { stageVariable1: 'value1', stageVariable2: 'value2', }, }
const processor = getProcessor(event) const request = processor.createRequest(event)
expect(request.method).toEqual('POST') expect(request.url).toEqual( 'https://id.execute-api.us-east-1.amazonaws.com/my/path?parameter1=value1¶meter1=value2¶meter2=value' ) expect(Object.fromEntries(request.headers)).toEqual({ 'content-type': 'application/json', cookie: 'cookie1; cookie2', header1: 'value1', header2: 'value1,value2', }) })})