handler.test.ts
This file
Path
src/adapter/lambda-edge/handler.test.ts
155 lines · 4.4 KiB · TypeScript
Last changed by
fix(lambda-edge): avoid callback handler deprecation on NODEJS_24_X (#4782)t0waxxbefore adoption53b66ae
History of this fileTake it
The walk stopped after 200 commits: lines older than 793ffda point at it.
chore: enable `skipLibCheck` to resolve TypeScript compilation issues (#4342)Yusuke Wadabefore adoption793ffda
lines 1–4why, one step back1import { describe } from 'vitest'2import { setCookie } from '../../helper/cookie'3import { Hono } from '../../hono'4import { encodeBase64 } from '../../utils/encode'fix(lambda-edge): avoid callback handler deprecation on NODEJS_24_X (#4782)t0waxxbefore adoption53b66ae
line 5why, one step back5import type { Callback, CloudFrontEdgeEvent } from './handler'chore: enable `skipLibCheck` to resolve TypeScript compilation issues (#4342)Yusuke Wadabefore adoption793ffda
lines 6–29why, one step back6import { createBody, handle, isContentTypeBinary } from './handler'78describe('isContentTypeBinary', () => {9 it('Should determine whether it is binary', () => {10 expect(isContentTypeBinary('image/png')).toBe(true)11 expect(isContentTypeBinary('font/woff2')).toBe(true)12 expect(isContentTypeBinary('image/svg+xml')).toBe(false)13 expect(isContentTypeBinary('image/svg+xml; charset=UTF-8')).toBe(false)14 expect(isContentTypeBinary('text/plain')).toBe(false)15 expect(isContentTypeBinary('text/plain; charset=UTF-8')).toBe(false)16 expect(isContentTypeBinary('text/css')).toBe(false)17 expect(isContentTypeBinary('text/javascript')).toBe(false)18 expect(isContentTypeBinary('application/json')).toBe(false)19 expect(isContentTypeBinary('application/ld+json')).toBe(false)20 expect(isContentTypeBinary('application/json')).toBe(false)21 })22})2324describe('createBody', () => {25 it('Should the request be a GET or HEAD, the Request must not include a Body', () => {26 const encoder = new TextEncoder()27 const data = encoder.encode('test')28 const body = {29 action: 'read-only',chore: update `typescript` to `5.9.2` (#4362)Yusuke Wadabefore adoption3e5a6e8
line 30why, one step back30 data: encodeBase64(data.buffer),chore: enable `skipLibCheck` to resolve TypeScript compilation issues (#4342)Yusuke Wadabefore adoption793ffda
lines 31–89why, one step back31 encoding: 'base64',32 inputTruncated: false,33 }3435 expect(createBody('GET', body)).toEqual(undefined)36 expect(createBody('GET', body)).not.toEqual(data)37 expect(createBody('HEAD', body)).toEqual(undefined)38 expect(createBody('HEAD', body)).not.toEqual(data)39 expect(createBody('POST', body)).toEqual(data)40 expect(createBody('POST', body)).not.toEqual(undefined)41 })42})4344describe('handle', () => {45 const cloudFrontEdgeEvent: CloudFrontEdgeEvent = {46 Records: [47 {48 cf: {49 config: {50 distributionDomainName: 'd111111abcdef8.cloudfront.net',51 distributionId: 'EDFDVBD6EXAMPLE',52 eventType: 'viewer-request',53 requestId: '4TyzHTaYWb1GX1qTfsHhEqV6HUDd_BzoBZnwfnvQc_1oF26ClkoUSEQ==',54 },55 request: {56 clientIp: '1.2.3.4',57 headers: {58 host: [59 {60 key: 'Host',61 value: 'hono.dev',62 },63 ],64 accept: [65 {66 key: 'accept',67 value: '*/*',68 },69 ],70 },71 method: 'GET',72 querystring: '',73 uri: '/test-path',74 },75 },76 },77 ],78 }7980 it('Should support alternate domain names', async () => {81 const app = new Hono()82 app.get('/test-path', (c) => {83 return c.text(c.req.url)84 })85 const handler = handle(app)8687 const res = await handler(cloudFrontEdgeEvent)8889 expect(res.body).toBe('https://hono.dev/test-path')fix(lambda-edge): avoid callback handler deprecation on NODEJS_24_X (#4782)t0waxxbefore adoption53b66ae
lines 90–122why, one step back90 })9192 it('Should expose async handler arity compatible with NODEJS_24_X', () => {93 const app = new Hono()94 const handler = handle(app)9596 expect(handler.length).toBeLessThanOrEqual(2)97 })9899 it('Should preserve positional callback compatibility', async () => {100 type Env = { Bindings: { callback: Callback } }101 const app = new Hono<Env>()102 const callback = vi.fn()103104 app.get('/test-path', (c) => {105 c.env.callback?.(null, {106 status: '200',107 headers: {108 'x-test': [{ key: 'x-test', value: 'ok' }],109 },110 })111 return c.text('ok')112 })113114 const handler = handle(app)115 await handler(cloudFrontEdgeEvent, undefined, callback)116117 expect(callback).toHaveBeenCalledWith(null, {118 status: '200',119 headers: {120 'x-test': [{ key: 'x-test', value: 'ok' }],121 },122 })chore: enable `skipLibCheck` to resolve TypeScript compilation issues (#4342)Yusuke Wadabefore adoption793ffda
lines 123–155why, one step back123 })124125 it('Should support multiple cookies', async () => {126 const app = new Hono()127 app.get('/test-path', (c) => {128 setCookie(c, 'cookie1', 'value1')129 setCookie(c, 'cookie2', 'value2')130 return c.text('')131 })132 const handler = handle(app)133134 const res = await handler(cloudFrontEdgeEvent)135136 expect(res.headers).toEqual({137 'content-type': [138 {139 key: 'content-type',140 value: 'text/plain; charset=UTF-8',141 },142 ],143 'set-cookie': [144 {145 key: 'set-cookie',146 value: 'cookie1=value1; Path=/',147 },148 {149 key: 'set-cookie',150 value: 'cookie2=value2; Path=/',151 },152 ],153 })154 })155})