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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156import { describe } from 'vitest'import { setCookie } from '../../helper/cookie'import { Hono } from '../../hono'import { encodeBase64 } from '../../utils/encode'import type { Callback, CloudFrontEdgeEvent } from './handler'import { createBody, handle, isContentTypeBinary } from './handler'
describe('isContentTypeBinary', () => { it('Should determine whether it is binary', () => { expect(isContentTypeBinary('image/png')).toBe(true) expect(isContentTypeBinary('font/woff2')).toBe(true) expect(isContentTypeBinary('image/svg+xml')).toBe(false) expect(isContentTypeBinary('image/svg+xml; charset=UTF-8')).toBe(false) expect(isContentTypeBinary('text/plain')).toBe(false) expect(isContentTypeBinary('text/plain; charset=UTF-8')).toBe(false) expect(isContentTypeBinary('text/css')).toBe(false) expect(isContentTypeBinary('text/javascript')).toBe(false) expect(isContentTypeBinary('application/json')).toBe(false) expect(isContentTypeBinary('application/ld+json')).toBe(false) expect(isContentTypeBinary('application/json')).toBe(false) })})
describe('createBody', () => { it('Should the request be a GET or HEAD, the Request must not include a Body', () => { const encoder = new TextEncoder() const data = encoder.encode('test') const body = { action: 'read-only', data: encodeBase64(data.buffer), encoding: 'base64', inputTruncated: false, }
expect(createBody('GET', body)).toEqual(undefined) expect(createBody('GET', body)).not.toEqual(data) expect(createBody('HEAD', body)).toEqual(undefined) expect(createBody('HEAD', body)).not.toEqual(data) expect(createBody('POST', body)).toEqual(data) expect(createBody('POST', body)).not.toEqual(undefined) })})
describe('handle', () => { const cloudFrontEdgeEvent: CloudFrontEdgeEvent = { Records: [ { cf: { config: { distributionDomainName: 'd111111abcdef8.cloudfront.net', distributionId: 'EDFDVBD6EXAMPLE', eventType: 'viewer-request', requestId: '4TyzHTaYWb1GX1qTfsHhEqV6HUDd_BzoBZnwfnvQc_1oF26ClkoUSEQ==', }, request: { clientIp: '1.2.3.4', headers: { host: [ { key: 'Host', value: 'hono.dev', }, ], accept: [ { key: 'accept', value: '*/*', }, ], }, method: 'GET', querystring: '', uri: '/test-path', }, }, }, ], }
it('Should support alternate domain names', async () => { const app = new Hono() app.get('/test-path', (c) => { return c.text(c.req.url) }) const handler = handle(app)
const res = await handler(cloudFrontEdgeEvent)
expect(res.body).toBe('https://hono.dev/test-path') })
it('Should expose async handler arity compatible with NODEJS_24_X', () => { const app = new Hono() const handler = handle(app)
expect(handler.length).toBeLessThanOrEqual(2) })
it('Should preserve positional callback compatibility', async () => { type Env = { Bindings: { callback: Callback } } const app = new Hono<Env>() const callback = vi.fn()
app.get('/test-path', (c) => { c.env.callback?.(null, { status: '200', headers: { 'x-test': [{ key: 'x-test', value: 'ok' }], }, }) return c.text('ok') })
const handler = handle(app) await handler(cloudFrontEdgeEvent, undefined, callback)
expect(callback).toHaveBeenCalledWith(null, { status: '200', headers: { 'x-test': [{ key: 'x-test', value: 'ok' }], }, }) })
it('Should support multiple cookies', async () => { const app = new Hono() app.get('/test-path', (c) => { setCookie(c, 'cookie1', 'value1') setCookie(c, 'cookie2', 'value2') return c.text('') }) const handler = handle(app)
const res = await handler(cloudFrontEdgeEvent)
expect(res.headers).toEqual({ 'content-type': [ { key: 'content-type', value: 'text/plain; charset=UTF-8', }, ], 'set-cookie': [ { key: 'set-cookie', value: 'cookie1=value1; Path=/', }, { key: 'set-cookie', value: 'cookie2=value2; Path=/', }, ], }) })})