handler.test.ts
This file
Path
src/adapter/aws-lambda/handler.test.ts
316 lines · 10.1 KiB · TypeScript
Last changed by
fix(aws-lambda): serve microsoft office files as binary in lambda handler (#4469)Matthias Feistbefore adoption253ec28
History of this fileTake it
The walk stopped after 200 commits: lines older than b2affb8 point at it.
4.6.20Yusuke Wadabefore adoptionb2affb8
line 1why, one step back1import type { LambdaEvent } from './handler'Merge pull request #4340 from honojs/nextYusuke Wadabefore adoption3a5248f
line 2why, one step back2import { getProcessor, isContentEncodingBinary, defaultIsContentTypeBinary } from './handler'4.6.20Yusuke Wadabefore adoptionb2affb8
line 3why, one step back3fix(aws-lambda): preserve percent-encoded values in query strings (#4372)Yusuke Wadabefore adoptiona268569
lines 4–82why, one step back4// Base event objects to reduce duplication5const baseV1Event: LambdaEvent = {6 version: '1.0',7 resource: '/my/path',8 path: '/my/path',9 httpMethod: 'GET',10 headers: {},11 multiValueHeaders: {},12 queryStringParameters: {},13 requestContext: {14 accountId: '123456789012',15 apiId: 'id',16 authorizer: { claims: null, scopes: null },17 domainName: 'id.execute-api.us-east-1.amazonaws.com',18 domainPrefix: 'id',19 extendedRequestId: 'request-id',20 httpMethod: 'GET',21 identity: {22 sourceIp: '192.0.2.1',23 userAgent: 'user-agent',24 clientCert: {25 clientCertPem: 'CERT_CONTENT',26 subjectDN: 'www.example.com',27 issuerDN: 'Example issuer',28 serialNumber: 'a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1',29 validity: {30 notBefore: 'May 28 12:30:02 2019 GMT',31 notAfter: 'Aug 5 09:36:04 2021 GMT',32 },33 },34 },35 path: '/my/path',36 protocol: 'HTTP/1.1',37 requestId: 'id=',38 requestTime: '04/Mar/2020:19:15:17 +0000',39 requestTimeEpoch: 1583349317135,40 resourcePath: '/my/path',41 stage: '$default',42 },43 pathParameters: {},44 stageVariables: {},45 body: null,46 isBase64Encoded: false,47}4849const baseV2Event: LambdaEvent = {50 version: '2.0',51 routeKey: '$default',52 rawPath: '/my/path',53 rawQueryString: '',54 cookies: [],55 headers: {},56 queryStringParameters: {},57 requestContext: {58 accountId: '123456789012',59 apiId: 'api-id',60 authentication: null,61 authorizer: {},62 domainName: 'id.execute-api.us-east-1.amazonaws.com',63 domainPrefix: 'id',64 http: {65 method: 'POST',66 path: '/my/path',67 protocol: 'HTTP/1.1',68 sourceIp: '192.0.2.1',69 userAgent: 'agent',70 },71 requestId: 'id',72 routeKey: '$default',73 stage: '$default',74 time: '12/Mar/2020:19:03:58 +0000',75 timeEpoch: 1583348638390,76 },77 body: null,78 pathParameters: {},79 isBase64Encoded: false,80 stageVariables: {},81}824.6.20Yusuke Wadabefore adoptionb2affb8
line 83why, one step back83describe('isContentTypeBinary', () => {fix(aws-lambda): serve microsoft office files as binary in lambda handler (#4469)Matthias Feistbefore adoption253ec28
lines 84–102why, one step back84 it.each([85 ['image/png', true],86 ['font/woff2', true],87 ['image/svg+xml', false],88 ['image/svg+xml; charset=UTF-8', false],89 ['text/plain', false],90 ['text/plain; charset=UTF-8', false],91 ['text/css', false],92 ['text/javascript', false],93 ['application/json', false],94 ['application/ld+json', false],95 ['application/json', false],96 ['application/vnd.openxmlformats-officedocument.wordprocessingml.document', true],97 ['application/msword', true],98 ['application/epub+zip', true],99 ['application/ld+json', false],100 ['application/vnd.oasis.opendocument.text', true],101 ])('Should determine whether %s it is binary', (mimeType: string, expected: boolean) => {102 expect(defaultIsContentTypeBinary(mimeType)).toBe(expected)4.6.20Yusuke Wadabefore adoptionb2affb8
lines 103–114why, one step back103 })104})105106describe('isContentEncodingBinary', () => {107 it('Should determine whether it is compressed', () => {108 expect(isContentEncodingBinary('gzip')).toBe(true)109 expect(isContentEncodingBinary('compress')).toBe(true)110 expect(isContentEncodingBinary('deflate')).toBe(true)111 expect(isContentEncodingBinary('br')).toBe(true)112 expect(isContentEncodingBinary('deflate, gzip')).toBe(true)113 expect(isContentEncodingBinary('')).toBe(false)114 expect(isContentEncodingBinary('unknown')).toBe(false)Merge pull request #4340 from honojs/nextYusuke Wadabefore adoption3a5248f
lines 115–117why, one step back115 })116})117fix(aws-lambda): preserve percent-encoded values in query strings (#4372)Yusuke Wadabefore adoptiona268569
lines 118–119why, one step back118describe('EventProcessor.createResult with contentTypesAsBinary', () => {119 const event = baseV1EventMerge pull request #4340 from honojs/nextYusuke Wadabefore adoption3a5248f
lines 120–176why, one step back120121 it('Should encode as base64 when content-type is in contentTypesAsBinary array', async () => {122 const processor = getProcessor(event)123 const response = new Response('test content', {124 headers: { 'content-type': 'application/custom' },125 })126127 const result = await processor.createResult(event, response, {128 isContentTypeBinary: (contentType: string) => contentType === 'application/custom',129 })130131 expect(result.isBase64Encoded).toBe(true)132 expect(result.body).toBe('dGVzdCBjb250ZW50')133 })134135 it('Should not encode as base64 when content-type is not in isContentTypeBinary array', async () => {136 const processor = getProcessor(event)137 const response = new Response('test content', {138 headers: { 'content-type': 'application/json' },139 })140141 const result = await processor.createResult(event, response, {142 isContentTypeBinary: (contentType: string) => contentType === 'application/custom',143 })144145 expect(result.isBase64Encoded).toBe(false)146 expect(result.body).toBe('test content')147 })148149 it('Should use defaultIsContentTypeBinary when isContentTypeBinary is undefined with binary content', async () => {150 const processor = getProcessor(event)151 const response = new Response('test image content', {152 headers: { 'content-type': 'image/png' },153 })154155 // Pass undefined for isContentTypeBinary to test default behavior156 const result = await processor.createResult(event, response, {157 isContentTypeBinary: undefined,158 })159160 expect(result.isBase64Encoded).toBe(true)161 expect(result.body).toBe('dGVzdCBpbWFnZSBjb250ZW50')162 })163164 it('Should use defaultIsContentTypeBinary when isContentTypeBinary is undefined with non-binary content', async () => {165 const processor = getProcessor(event)166 const response = new Response('test text content', {167 headers: { 'content-type': 'text/plain' },168 })169170 // Pass undefined for isContentTypeBinary to test default behavior171 const result = await processor.createResult(event, response, {172 isContentTypeBinary: undefined,173 })174175 expect(result.isBase64Encoded).toBe(false)176 expect(result.body).toBe('test text content')4.6.20Yusuke Wadabefore adoptionb2affb8
lines 177–180why, one step back177 })178})179180describe('EventProcessor.createRequest', () => {fix(aws-lambda): preserve percent-encoded values in query strings (#4372)Yusuke Wadabefore adoptiona268569
lines 181–222why, one step back181 it('Should preserve percent-encoded values in query string for version 1.0', () => {182 const event: LambdaEvent = {183 ...baseV1Event,184 // API Gateway provides decoded values185 multiValueQueryStringParameters: {186 path: ['/book/{bookId}/'], // Originally %7BbookId%7D187 name: ['John Doe'], // Originally John%20Doe188 tag: ['日本語'], // Originally %E6%97%A5%E6%9C%AC%E8%AA%9E189 },190 }191192 const processor = getProcessor(event)193 const request = processor.createRequest(event)194195 // URL should contain properly encoded values196 expect(request.url).toEqual(197 'https://id.execute-api.us-east-1.amazonaws.com/my/path?path=%2Fbook%2F%7BbookId%7D%2F&name=John%20Doe&tag=%E6%97%A5%E6%9C%AC%E8%AA%9E'198 )199 })200201 it('Should handle special characters correctly in queryStringParameters for version 1.0', () => {202 const event: LambdaEvent = {203 ...baseV1Event,204 queryStringParameters: {205 'key with spaces': 'value with spaces',206 'special!@#$%^&*()': 'chars!@#$%^&*()',207 equals: 'a=b=c',208 ampersand: 'a&b&c',209 },210 }211212 const processor = getProcessor(event)213 const request = processor.createRequest(event)214215 // Verify the URL is properly encoded216 const url = new URL(request.url)217 expect(url.searchParams.get('key with spaces')).toBe('value with spaces')218 expect(url.searchParams.get('special!@#$%^&*()')).toBe('chars!@#$%^&*()')219 expect(url.searchParams.get('equals')).toBe('a=b=c')220 expect(url.searchParams.get('ampersand')).toBe('a&b&c')221 })2224.6.20Yusuke Wadabefore adoptionb2affb8
lines 223–224why, one step back223 it('Should return valid Request object from version 1.0 API Gateway event', () => {224 const event: LambdaEvent = {fix(aws-lambda): preserve percent-encoded values in query strings (#4372)Yusuke Wadabefore adoptiona268569
line 225why, one step back225 ...baseV1Event,4.6.20Yusuke Wadabefore adoptionb2affb8
lines 226–249why, one step back226 headers: {227 'content-type': 'application/json',228 header1: 'value1',229 header2: 'value1',230 },231 multiValueHeaders: {232 header1: ['value1'],233 header2: ['value1', 'value2', 'value3'],234 },235 // This value doesn't match multi value's content.236 // We want to assert handler is using the multi value's content when both are available.237 queryStringParameters: {238 parameter2: 'value',239 },240 multiValueQueryStringParameters: {241 parameter1: ['value1', 'value2'],242 parameter2: ['value'],243 },244 }245246 const processor = getProcessor(event)247 const request = processor.createRequest(event)248249 expect(request.method).toEqual('GET')fix(aws-lambda): preserve percent-encoded values in query strings (#4372)Yusuke Wadabefore adoptiona268569
line 250why, one step back250 // Note: Values are now properly encoded4.6.20Yusuke Wadabefore adoptionb2affb8
lines 251–262why, one step back251 expect(request.url).toEqual(252 'https://id.execute-api.us-east-1.amazonaws.com/my/path?parameter1=value1¶meter1=value2¶meter2=value'253 )254 expect(Object.fromEntries(request.headers)).toEqual({255 'content-type': 'application/json',256 header1: 'value1',257 header2: 'value1, value2, value3',258 })259 })260261 it('Should return valid Request object from version 2.0 API Gateway event', () => {262 const event: LambdaEvent = {fix(aws-lambda): preserve percent-encoded values in query strings (#4372)Yusuke Wadabefore adoptiona268569
line 263why, one step back263 ...baseV2Event,4.6.20Yusuke Wadabefore adoptionb2affb8
lines 264–298why, one step back264 rawQueryString: 'parameter1=value1¶meter1=value2¶meter2=value',265 cookies: ['cookie1', 'cookie2'],266 headers: {267 'content-type': 'application/json',268 header1: 'value1',269 header2: 'value1,value2',270 },271 queryStringParameters: {272 parameter1: 'value1,value2',273 parameter2: 'value',274 },275 body: 'Hello from Lambda',276 pathParameters: {277 parameter1: 'value1',278 },279 stageVariables: {280 stageVariable1: 'value1',281 stageVariable2: 'value2',282 },283 }284285 const processor = getProcessor(event)286 const request = processor.createRequest(event)287288 expect(request.method).toEqual('POST')289 expect(request.url).toEqual(290 'https://id.execute-api.us-east-1.amazonaws.com/my/path?parameter1=value1¶meter1=value2¶meter2=value'291 )292 expect(Object.fromEntries(request.headers)).toEqual({293 'content-type': 'application/json',294 cookie: 'cookie1; cookie2',295 header1: 'value1',296 header2: 'value1,value2',297 })298 })fix(aws-lambda): sanitize non-ASCII header values to prevent ByteString errors (#4437)Yusuke Wadabefore adoption38f756d
lines 299–315why, one step back299300 describe('non-ASCII header value processing', () => {301 it('Should encode non-ASCII header values with encodeURIComponent', async () => {302 const event: LambdaEvent = {303 ...baseV1Event,304 headers: {305 'x-city': '炎', // Non-ASCII character306 },307 }308309 const processor = getProcessor(event)310 const request = processor.createRequest(event)311312 const xCity = request.headers.get('x-city') ?? ''313 expect(decodeURIComponent(xCity)).toBe('炎')314 })315 })4.6.20Yusuke Wadabefore adoptionb2affb8
line 316why, one step back316})