Skip to content
Myr

index.test.ts

This file

Path

src/middleware/request-id/index.test.ts

168 lines · 5.9 KiB · TypeScript

Last changed by

chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2

History of this file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2

line 1why, one step back
1import { requestId } from '.'

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 2–168why, one step back
2import type { Context } from '../../context'
3import { Hono } from '../../hono'45const regexUUIDv4 = /([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})/67describe('Request ID Middleware', () => {8 const app = new Hono()9 app.use('*', requestId())10 app.get('/requestId', (c) => c.text(c.get('requestId') ?? 'No Request ID'))1112 it('Should return random request id', async () => {13 const res = await app.request('http://localhost/requestId')14 expect(res).not.toBeNull()15 expect(res.status).toBe(200)16 expect(res.headers.get('X-Request-Id')).toMatch(regexUUIDv4)17 expect(await res.text()).match(regexUUIDv4)18 })1920 it('Should return custom request id', async () => {21 const res = await app.request('http://localhost/requestId', {22 headers: {23 'X-Request-Id': 'hono-is-hot',24 },25 })26 expect(res).not.toBeNull()27 expect(res.status).toBe(200)28 expect(res.headers.get('X-Request-Id')).toBe('hono-is-hot')29 expect(await res.text()).toBe('hono-is-hot')30 })3132 it('Should return custom request id with all valid characters', async () => {33 const validRequestId = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_='34 const res = await app.request('http://localhost/requestId', {35 headers: {36 'X-Request-Id': validRequestId,37 },38 })39 expect(res).not.toBeNull()40 expect(res.status).toBe(200)41 expect(res.headers.get('X-Request-Id')).toBe(validRequestId)42 expect(await res.text()).toBe(validRequestId)43 })4445 it('Should return random request id without using request header', async () => {46 const res = await app.request('http://localhost/requestId', {47 headers: {48 'X-Request-Id': 'Hello!12345-@*^',49 },50 })51 expect(res).not.toBeNull()52 expect(res.status).toBe(200)53 expect(res.headers.get('X-Request-Id')).toMatch(regexUUIDv4)54 expect(await res.text()).toMatch(regexUUIDv4)55 })56})5758describe('Request ID Middleware with custom generator', () => {59 function generateWord() {60 return 'HonoIsWebFramework'61 }62 function generateDoubleRequestId(c: Context) {63 const honoId = c.req.header('Hono-Request-Id')64 const ohnoId = c.req.header('Ohno-Request-Id')65 if (honoId && ohnoId) {66 return honoId + ohnoId67 }68 return crypto.randomUUID()69 }70 const app = new Hono()71 app.use('/word', requestId({ generator: generateWord }))72 app.use('/doubleRequestId', requestId({ generator: generateDoubleRequestId }))73 app.get('/word', (c) => c.text(c.get('requestId') ?? 'No Request ID'))74 app.get('/doubleRequestId', (c) => c.text(c.get('requestId') ?? 'No Request ID'))75 it('Should return custom request id', async () => {76 const res = await app.request('http://localhost/word')77 expect(res).not.toBeNull()78 expect(res.status).toBe(200)79 expect(res.headers.get('X-Request-Id')).toBe('HonoIsWebFramework')80 expect(await res.text()).toBe('HonoIsWebFramework')81 })8283 it('Should return complex request id', async () => {84 const res = await app.request('http://localhost/doubleRequestId', {85 headers: {86 'Hono-Request-Id': 'Hello',87 'Ohno-Request-Id': 'World',88 },89 })90 expect(res).not.toBeNull()91 expect(res.status).toBe(200)92 expect(res.headers.get('X-Request-Id')).toBe('HelloWorld')93 expect(await res.text()).toBe('HelloWorld')94 })95})9697describe('Request ID Middleware with limit length', () => {98 const charactersOf255 = 'h'.repeat(255)99 const charactersOf256 = 'h'.repeat(256)100101 const app = new Hono()102 app.use('/requestId', requestId())103 app.use('/limit256', requestId({ limitLength: 256 }))104 app.get('/requestId', (c) => c.text(c.get('requestId') ?? 'No Request ID'))105 app.get('/limit256', (c) => c.text(c.get('requestId') ?? 'No Request ID'))106107 it('Should return custom request id', async () => {108 const res = await app.request('http://localhost/requestId', {109 headers: {110 'X-Request-Id': charactersOf255,111 },112 })113 expect(res).not.toBeNull()114 expect(res.status).toBe(200)115 expect(res.headers.get('X-Request-Id')).toBe(charactersOf255)116 expect(await res.text()).toBe(charactersOf255)117 })118 it('Should return random request id without using request header', async () => {119 const res = await app.request('http://localhost/requestId', {120 headers: {121 'X-Request-Id': charactersOf256,122 },123 })124 expect(res).not.toBeNull()125 expect(res.status).toBe(200)126 expect(res.headers.get('X-Request-Id')).toMatch(regexUUIDv4)127 expect(await res.text()).toMatch(regexUUIDv4)128 })129 it('Should return custom request id with 256 characters', async () => {130 const res = await app.request('http://localhost/limit256', {131 headers: {132 'X-Request-Id': charactersOf256,133 },134 })135 expect(res).not.toBeNull()136 expect(res.status).toBe(200)137 expect(res.headers.get('X-Request-Id')).toBe(charactersOf256)138 expect(await res.text()).toBe(charactersOf256)139 })140})141142describe('Request ID Middleware with custom header', () => {143 const app = new Hono()144 app.use('/requestId', requestId({ headerName: 'Hono-Request-Id' }))145 app.get('/emptyId', requestId({ headerName: '' }))146 app.get('/requestId', (c) => c.text(c.get('requestId') ?? 'No Request ID'))147 app.get('/emptyId', (c) => c.text(c.get('requestId') ?? 'No Request ID'))148149 it('Should return custom request id', async () => {150 const res = await app.request('http://localhost/requestId', {151 headers: {152 'Hono-Request-Id': 'hono-is-hot',153 },154 })155 expect(res).not.toBeNull()156 expect(res.status).toBe(200)157 expect(res.headers.get('Hono-Request-Id')).toBe('hono-is-hot')158 expect(await res.text()).toBe('hono-is-hot')159 })160161 it('Should not return request id', async () => {162 const res = await app.request('http://localhost/emptyId')163 expect(res).not.toBeNull()164 expect(res.status).toBe(200)165 expect(res.headers.get('X-Request-Id')).toBeNull()166 expect(await res.text()).toMatch(regexUUIDv4)167 })168})