-
-
Notifications
You must be signed in to change notification settings - Fork 652
/
Copy pathindex.test.ts
182 lines (162 loc) · 6.08 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { Hono } from '../../hono'
import { RETAINED_304_HEADERS, etag } from '.'
describe('Etag Middleware', () => {
it('Should return etag header', async () => {
const app = new Hono()
app.use('/etag/*', etag())
app.get('/etag/abc', (c) => {
return c.text('Hono is cool')
})
app.get('/etag/def', (c) => {
return c.json({ message: 'Hono is cool' })
})
let res = await app.request('http://localhost/etag/abc')
expect(res.headers.get('ETag')).not.toBeFalsy()
expect(res.headers.get('ETag')).toBe('"4e32298b1cb4edc595237405e5b696e105c2399a"')
res = await app.request('http://localhost/etag/def')
expect(res.headers.get('ETag')).not.toBeFalsy()
expect(res.headers.get('ETag')).toBe('"4515561204e8269cb4468d5b39288d8f2482dcfe"')
})
it('Should return etag header - binary', async () => {
const app = new Hono()
app.use('/etag/*', etag())
app.get('/etag', async (c) => {
return c.body(new Uint8Array(1))
})
const res = await app.request('http://localhost/etag')
expect(res.headers.get('ETag')).not.toBeFalsy()
const etagHeader = res.headers.get('ETag')
expect(etagHeader).toBe('"5ba93c9db0cff93f52b521d7420e43f6eda2784f"')
})
it('Should not be the same etag - arrayBuffer', async () => {
const app = new Hono()
app.use('/etag/*', etag())
app.get('/etag/ab1', (c) => {
return c.body(new ArrayBuffer(1))
})
app.get('/etag/ab2', (c) => {
return c.body(new ArrayBuffer(2))
})
let res = await app.request('http://localhost/etag/ab1')
const hash = res.headers.get('Etag')
res = await app.request('http://localhost/etag/ab2')
expect(res.headers.get('ETag')).not.toBe(hash)
})
it('Should not be the same etag - Uint8Array', async () => {
const app = new Hono()
app.use('/etag/*', etag())
app.get('/etag/ui1', (c) => {
return c.body(new Uint8Array([1, 2, 3]))
})
app.get('/etag/ui2', (c) => {
return c.body(new Uint8Array([1, 2, 3, 4]))
})
let res = await app.request('http://localhost/etag/ui1')
const hash = res.headers.get('Etag')
res = await app.request('http://localhost/etag/ui2')
expect(res.headers.get('ETag')).not.toBe(hash)
})
it('Should return etag header - weak', async () => {
const app = new Hono()
app.use('/etag/*', etag({ weak: true }))
app.get('/etag/abc', (c) => {
return c.text('Hono is cool')
})
const res = await app.request('http://localhost/etag/abc')
expect(res.headers.get('ETag')).not.toBeFalsy()
expect(res.headers.get('ETag')).toBe('W/"4e32298b1cb4edc595237405e5b696e105c2399a"')
})
it('Should handle conditional GETs', async () => {
const app = new Hono()
app.use('/etag/*', etag())
app.get('/etag/ghi', (c) =>
c.text('Hono is great', 200, {
'cache-control': 'public, max-age=120',
date: 'Mon, Feb 27 2023 12:08:36 GMT',
expires: 'Mon, Feb 27 2023 12:10:36 GMT',
server: 'Upstream 1.2',
vary: 'Accept-Language',
})
)
// unconditional GET
let res = await app.request('http://localhost/etag/ghi')
expect(res.status).toBe(200)
expect(res.headers.get('ETag')).not.toBeFalsy()
const etagHeaderValue = res.headers.get('ETag') || ''
// conditional GET with the wrong ETag:
res = await app.request('http://localhost/etag/ghi', {
headers: {
'If-None-Match': '"not the right etag"',
},
})
expect(res.status).toBe(200)
// conditional GET with matching ETag:
res = await app.request('http://localhost/etag/ghi', {
headers: {
'If-None-Match': etagHeaderValue,
},
})
expect(res.status).toBe(304)
expect(res.headers.get('Etag')).toBe(etagHeaderValue)
expect(await res.text()).toBe('')
expect(res.headers.get('cache-control')).toBe('public, max-age=120')
expect(res.headers.get('date')).toBe('Mon, Feb 27 2023 12:08:36 GMT')
expect(res.headers.get('expires')).toBe('Mon, Feb 27 2023 12:10:36 GMT')
expect(res.headers.get('server')).toBeFalsy()
expect(res.headers.get('vary')).toBe('Accept-Language')
// conditional GET with matching ETag among list:
res = await app.request('http://localhost/etag/ghi', {
headers: {
'If-None-Match': `"mismatch 1", ${etagHeaderValue}, "mismatch 2"`,
},
})
expect(res.status).toBe(304)
})
it('Should not return duplicate etag header values', async () => {
const app = new Hono()
app.use('/etag/*', etag())
app.use('/etag/*', etag())
app.get('/etag/abc', (c) => c.text('Hono is cool'))
const res = await app.request('http://localhost/etag/abc')
expect(res.headers.get('ETag')).not.toBeFalsy()
expect(res.headers.get('ETag')).toBe('"4e32298b1cb4edc595237405e5b696e105c2399a"')
})
it('Should not override ETag headers from upstream', async () => {
const app = new Hono()
app.use('/etag/*', etag())
app.get('/etag/predefined', (c) =>
c.text('This response has an ETag', 200, { ETag: '"f-0194-d"' })
)
const res = await app.request('http://localhost/etag/predefined')
expect(res.headers.get('ETag')).toBe('"f-0194-d"')
})
it('Should retain the default and the specified headers', async () => {
const cacheControl = 'public, max-age=120'
const message = 'Hello!'
const app = new Hono()
app.use(
'/etag/*',
etag({
retainedHeaders: ['x-message-retain', ...RETAINED_304_HEADERS],
})
)
app.get('/etag', (c) => {
return c.text('Hono is cool', 200, {
'cache-control': cacheControl,
'x-message-retain': message,
'x-message': message,
})
})
const res = await app.request('/etag', {
headers: {
'If-None-Match': '"4e32298b1cb4edc595237405e5b696e105c2399a"',
},
})
expect(res.status).toBe(304)
expect(res.headers.get('ETag')).not.toBeFalsy()
expect(res.headers.get('ETag')).toBe('"4e32298b1cb4edc595237405e5b696e105c2399a"')
expect(res.headers.get('Cache-Control')).toBe(cacheControl)
expect(res.headers.get('x-message-retain')).toBe(message)
expect(res.headers.get('x-message')).toBeFalsy()
})
})