forked from honojs/hono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
155 lines (142 loc) · 5.41 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import type { Context } from '../../context'
import { Hono } from '../../hono'
import { requestId } from '.'
const regexUUIDv4 = /([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})/
describe('Request ID Middleware', () => {
const app = new Hono()
app.use('*', requestId())
app.get('/requestId', (c) => c.text(c.get('requestId') ?? 'No Request ID'))
it('Should return random request id', async () => {
const res = await app.request('http://localhost/requestId')
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(res.headers.get('X-Request-Id')).toMatch(regexUUIDv4)
expect(await res.text()).match(regexUUIDv4)
})
it('Should return custom request id', async () => {
const res = await app.request('http://localhost/requestId', {
headers: {
'X-Request-Id': 'hono-is-hot',
},
})
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(res.headers.get('X-Request-Id')).toBe('hono-is-hot')
expect(await res.text()).toBe('hono-is-hot')
})
it('Should return random request id without using request header', async () => {
const res = await app.request('http://localhost/requestId', {
headers: {
'X-Request-Id': 'Hello!12345-@*^',
},
})
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(res.headers.get('X-Request-Id')).toMatch(regexUUIDv4)
expect(await res.text()).toMatch(regexUUIDv4)
})
})
describe('Request ID Middleware with custom generator', () => {
function generateWord() {
return 'HonoIsWebFramework'
}
function generateDoubleRequestId(c: Context) {
const honoId = c.req.header('Hono-Request-Id')
const ohnoId = c.req.header('Ohno-Request-Id')
if (honoId && ohnoId) {
return honoId + ohnoId
}
return crypto.randomUUID()
}
const app = new Hono()
app.use('/word', requestId({ generator: generateWord }))
app.use('/doubleRequestId', requestId({ generator: generateDoubleRequestId }))
app.get('/word', (c) => c.text(c.get('requestId') ?? 'No Request ID'))
app.get('/doubleRequestId', (c) => c.text(c.get('requestId') ?? 'No Request ID'))
it('Should return custom request id', async () => {
const res = await app.request('http://localhost/word')
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(res.headers.get('X-Request-Id')).toBe('HonoIsWebFramework')
expect(await res.text()).toBe('HonoIsWebFramework')
})
it('Should return complex request id', async () => {
const res = await app.request('http://localhost/doubleRequestId', {
headers: {
'Hono-Request-Id': 'Hello',
'Ohno-Request-Id': 'World',
},
})
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(res.headers.get('X-Request-Id')).toBe('HelloWorld')
expect(await res.text()).toBe('HelloWorld')
})
})
describe('Request ID Middleware with limit length', () => {
const charactersOf255 = 'h'.repeat(255)
const charactersOf256 = 'h'.repeat(256)
const app = new Hono()
app.use('/requestId', requestId())
app.use('/limit256', requestId({ limitLength: 256 }))
app.get('/requestId', (c) => c.text(c.get('requestId') ?? 'No Request ID'))
app.get('/limit256', (c) => c.text(c.get('requestId') ?? 'No Request ID'))
it('Should return custom request id', async () => {
const res = await app.request('http://localhost/requestId', {
headers: {
'X-Request-Id': charactersOf255,
},
})
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(res.headers.get('X-Request-Id')).toBe(charactersOf255)
expect(await res.text()).toBe(charactersOf255)
})
it('Should return random request id without using request header', async () => {
const res = await app.request('http://localhost/requestId', {
headers: {
'X-Request-Id': charactersOf256,
},
})
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(res.headers.get('X-Request-Id')).toMatch(regexUUIDv4)
expect(await res.text()).toMatch(regexUUIDv4)
})
it('Should return custom request id with 256 characters', async () => {
const res = await app.request('http://localhost/limit256', {
headers: {
'X-Request-Id': charactersOf256,
},
})
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(res.headers.get('X-Request-Id')).toBe(charactersOf256)
expect(await res.text()).toBe(charactersOf256)
})
})
describe('Request ID Middleware with custom header', () => {
const app = new Hono()
app.use('/requestId', requestId({ headerName: 'Hono-Request-Id' }))
app.get('/emptyId', requestId({ headerName: '' }))
app.get('/requestId', (c) => c.text(c.get('requestId') ?? 'No Request ID'))
app.get('/emptyId', (c) => c.text(c.get('requestId') ?? 'No Request ID'))
it('Should return custom request id', async () => {
const res = await app.request('http://localhost/requestId', {
headers: {
'Hono-Request-Id': 'hono-is-hot',
},
})
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(res.headers.get('Hono-Request-Id')).toBe('hono-is-hot')
expect(await res.text()).toBe('hono-is-hot')
})
it('Should not return request id', async () => {
const res = await app.request('http://localhost/emptyId')
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(res.headers.get('X-Request-Id')).toBeNull()
expect(await res.text()).toMatch(regexUUIDv4)
})
})