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
The walk stopped after 200 commits: lines older than 61bee9f point at it.
refactor(hono-base): remove unneeded processes and variables (#3649)EdamAmexbefore adoption61bee9f
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'refactor(hono-base): remove unneeded processes and variables (#3649)EdamAmexbefore adoption61bee9f
lines 3–5why, one step back34describe('isContentTypeBinary', () => {5 it('Should determine whether it is binary', () => {Merge pull request #4340 from honojs/nextYusuke Wadabefore adoption3a5248f
lines 6–16why, one step back6 expect(defaultIsContentTypeBinary('image/png')).toBe(true)7 expect(defaultIsContentTypeBinary('font/woff2')).toBe(true)8 expect(defaultIsContentTypeBinary('image/svg+xml')).toBe(false)9 expect(defaultIsContentTypeBinary('image/svg+xml; charset=UTF-8')).toBe(false)10 expect(defaultIsContentTypeBinary('text/plain')).toBe(false)11 expect(defaultIsContentTypeBinary('text/plain; charset=UTF-8')).toBe(false)12 expect(defaultIsContentTypeBinary('text/css')).toBe(false)13 expect(defaultIsContentTypeBinary('text/javascript')).toBe(false)14 expect(defaultIsContentTypeBinary('application/json')).toBe(false)15 expect(defaultIsContentTypeBinary('application/ld+json')).toBe(false)16 expect(defaultIsContentTypeBinary('application/json')).toBe(false)refactor(hono-base): remove unneeded processes and variables (#3649)EdamAmexbefore adoption61bee9f
lines 17–28why, one step back17 })18})1920describe('isContentEncodingBinary', () => {21 it('Should determine whether it is compressed', () => {22 expect(isContentEncodingBinary('gzip')).toBe(true)23 expect(isContentEncodingBinary('compress')).toBe(true)24 expect(isContentEncodingBinary('deflate')).toBe(true)25 expect(isContentEncodingBinary('br')).toBe(true)26 expect(isContentEncodingBinary('deflate, gzip')).toBe(true)27 expect(isContentEncodingBinary('')).toBe(false)28 expect(isContentEncodingBinary('unknown')).toBe(false)Merge pull request #4340 from honojs/nextYusuke Wadabefore adoption3a5248f
lines 29–132why, one step back29 })30})3132describe('EventProcessor.createResult with contentTypsAsBinary', () => {33 const event: LambdaEvent = {34 version: '1.0',35 resource: '/my/path',36 path: '/my/path',37 httpMethod: 'GET',38 headers: {},39 multiValueHeaders: {},40 queryStringParameters: {},41 requestContext: {42 accountId: '123456789012',43 apiId: 'id',44 authorizer: { claims: null, scopes: null },45 domainName: 'id.execute-api.us-east-1.amazonaws.com',46 domainPrefix: 'id',47 extendedRequestId: 'request-id',48 httpMethod: 'GET',49 identity: {50 sourceIp: '192.0.2.1',51 userAgent: 'user-agent',52 clientCert: {53 clientCertPem: 'CERT_CONTENT',54 subjectDN: 'www.example.com',55 issuerDN: 'Example issuer',56 serialNumber: 'a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1',57 validity: {58 notBefore: 'May 28 12:30:02 2019 GMT',59 notAfter: 'Aug 5 09:36:04 2021 GMT',60 },61 },62 },63 path: '/my/path',64 protocol: 'HTTP/1.1',65 requestId: 'id=',66 requestTime: '04/Mar/2020:19:15:17 +0000',67 requestTimeEpoch: 1583349317135,68 resourcePath: '/my/path',69 stage: '$default',70 },71 pathParameters: {},72 stageVariables: {},73 body: null,74 isBase64Encoded: false,75 }7677 it('Should encode as base64 when content-type is in contentTypesAsBinary array', async () => {78 const processor = getProcessor(event)79 const response = new Response('test content', {80 headers: { 'content-type': 'application/custom' },81 })8283 const result = await processor.createResult(event, response, {84 isContentTypeBinary: (contentType: string) => contentType === 'application/custom',85 })8687 expect(result.isBase64Encoded).toBe(true)88 expect(result.body).toBe('dGVzdCBjb250ZW50')89 })9091 it('Should not encode as base64 when content-type is not in isContentTypeBinary array', async () => {92 const processor = getProcessor(event)93 const response = new Response('test content', {94 headers: { 'content-type': 'application/json' },95 })9697 const result = await processor.createResult(event, response, {98 isContentTypeBinary: (contentType: string) => contentType === 'application/custom',99 })100101 expect(result.isBase64Encoded).toBe(false)102 expect(result.body).toBe('test content')103 })104105 it('Should use defaultIsContentTypeBinary when isContentTypeBinary is undefined with binary content', async () => {106 const processor = getProcessor(event)107 const response = new Response('test image content', {108 headers: { 'content-type': 'image/png' },109 })110111 // Pass undefined for isContentTypeBinary to test default behavior112 const result = await processor.createResult(event, response, {113 isContentTypeBinary: undefined,114 })115116 expect(result.isBase64Encoded).toBe(true)117 expect(result.body).toBe('dGVzdCBpbWFnZSBjb250ZW50')118 })119120 it('Should use defaultIsContentTypeBinary when isContentTypeBinary is undefined with non-binary content', async () => {121 const processor = getProcessor(event)122 const response = new Response('test text content', {123 headers: { 'content-type': 'text/plain' },124 })125126 // Pass undefined for isContentTypeBinary to test default behavior127 const result = await processor.createResult(event, response, {128 isContentTypeBinary: undefined,129 })130131 expect(result.isBase64Encoded).toBe(false)132 expect(result.body).toBe('test text content')refactor(hono-base): remove unneeded processes and variables (#3649)EdamAmexbefore adoption61bee9f
lines 133–151why, one step back133 })134})135136describe('EventProcessor.createRequest', () => {137 it('Should return valid Request object from version 1.0 API Gateway event', () => {138 const event: LambdaEvent = {139 version: '1.0',140 resource: '/my/path',141 path: '/my/path',142 httpMethod: 'GET',143 headers: {144 'content-type': 'application/json',145 header1: 'value1',146 header2: 'value1',147 },148 multiValueHeaders: {149 header1: ['value1'],150 header2: ['value1', 'value2', 'value3'],151 },fix(aws-lambda): Fix query string handling for v1 (#3717)Hollis Wubefore adoption4af04a6
lines 152–153why, one step back152 // This value doesn't match multi value's content.153 // We want to assert handler is using the multi value's content when both are available.refactor(hono-base): remove unneeded processes and variables (#3649)EdamAmexbefore adoption61bee9f
lines 154–204why, one step back154 queryStringParameters: {155 parameter2: 'value',156 },157 multiValueQueryStringParameters: {158 parameter1: ['value1', 'value2'],159 parameter2: ['value'],160 },161 requestContext: {162 accountId: '123456789012',163 apiId: 'id',164 authorizer: {165 claims: null,166 scopes: null,167 },168 domainName: 'id.execute-api.us-east-1.amazonaws.com',169 domainPrefix: 'id',170 extendedRequestId: 'request-id',171 httpMethod: 'GET',172 identity: {173 sourceIp: '192.0.2.1',174 userAgent: 'user-agent',175 clientCert: {176 clientCertPem: 'CERT_CONTENT',177 subjectDN: 'www.example.com',178 issuerDN: 'Example issuer',179 serialNumber: 'a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1',180 validity: {181 notBefore: 'May 28 12:30:02 2019 GMT',182 notAfter: 'Aug 5 09:36:04 2021 GMT',183 },184 },185 },186 path: '/my/path',187 protocol: 'HTTP/1.1',188 requestId: 'id=',189 requestTime: '04/Mar/2020:19:15:17 +0000',190 requestTimeEpoch: 1583349317135,191 resourcePath: '/my/path',192 stage: '$default',193 },194 pathParameters: {},195 stageVariables: {},196 body: null,197 isBase64Encoded: false,198 }199200 const processor = getProcessor(event)201 const request = processor.createRequest(event)202203 expect(request.method).toEqual('GET')204 expect(request.url).toEqual(fix(aws-lambda): Fix query string handling for v1 (#3717)Hollis Wubefore adoption4af04a6
line 205why, one step back205 'https://id.execute-api.us-east-1.amazonaws.com/my/path?parameter1=value1¶meter1=value2¶meter2=value'refactor(hono-base): remove unneeded processes and variables (#3649)EdamAmexbefore adoption61bee9f
lines 206–275why, one step back206 )207 expect(Object.fromEntries(request.headers)).toEqual({208 'content-type': 'application/json',209 header1: 'value1',210 header2: 'value1, value2, value3',211 })212 })213214 it('Should return valid Request object from version 2.0 API Gateway event', () => {215 const event: LambdaEvent = {216 version: '2.0',217 routeKey: '$default',218 rawPath: '/my/path',219 rawQueryString: 'parameter1=value1¶meter1=value2¶meter2=value',220 cookies: ['cookie1', 'cookie2'],221 headers: {222 'content-type': 'application/json',223 header1: 'value1',224 header2: 'value1,value2',225 },226 queryStringParameters: {227 parameter1: 'value1,value2',228 parameter2: 'value',229 },230 requestContext: {231 accountId: '123456789012',232 apiId: 'api-id',233 authentication: null,234 authorizer: {},235 domainName: 'id.execute-api.us-east-1.amazonaws.com',236 domainPrefix: 'id',237 http: {238 method: 'POST',239 path: '/my/path',240 protocol: 'HTTP/1.1',241 sourceIp: '192.0.2.1',242 userAgent: 'agent',243 },244 requestId: 'id',245 routeKey: '$default',246 stage: '$default',247 time: '12/Mar/2020:19:03:58 +0000',248 timeEpoch: 1583348638390,249 },250 body: 'Hello from Lambda',251 pathParameters: {252 parameter1: 'value1',253 },254 isBase64Encoded: false,255 stageVariables: {256 stageVariable1: 'value1',257 stageVariable2: 'value2',258 },259 }260261 const processor = getProcessor(event)262 const request = processor.createRequest(event)263264 expect(request.method).toEqual('POST')265 expect(request.url).toEqual(266 'https://id.execute-api.us-east-1.amazonaws.com/my/path?parameter1=value1¶meter1=value2¶meter2=value'267 )268 expect(Object.fromEntries(request.headers)).toEqual({269 'content-type': 'application/json',270 cookie: 'cookie1; cookie2',271 header1: 'value1',272 header2: 'value1,value2',273 })274 })275})