-
-
Notifications
You must be signed in to change notification settings - Fork 701
/
Copy pathindex.test.ts
187 lines (166 loc) · 6.62 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
183
184
185
186
187
import { stream } from '../../helper/streaming'
import { Hono } from '../../hono'
import { compress } from '.'
describe('Compress Middleware', () => {
const app = new Hono()
// Apply compress middleware to all routes
app.use('*', compress())
// Test routes
app.get('/small', (c) => {
c.header('Content-Type', 'text/plain')
c.header('Content-Length', '5')
return c.text('small')
})
app.get('/large', (c) => {
c.header('Content-Type', 'text/plain')
c.header('Content-Length', '1024')
return c.text('a'.repeat(1024))
})
app.get('/small-json', (c) => {
c.header('Content-Type', 'application/json')
c.header('Content-Length', '26')
return c.json({ message: 'Hello, World!' })
})
app.get('/large-json', (c) => {
c.header('Content-Type', 'application/json')
c.header('Content-Length', '1024')
return c.json({ data: 'a'.repeat(1024), message: 'Large JSON' })
})
app.get('/no-transform', (c) => {
c.header('Content-Type', 'text/plain')
c.header('Content-Length', '1024')
c.header('Cache-Control', 'no-transform')
return c.text('a'.repeat(1024))
})
app.get('/jpeg-image', (c) => {
c.header('Content-Type', 'image/jpeg')
c.header('Content-Length', '1024')
return c.body(new Uint8Array(1024)) // Simulated JPEG data
})
app.get('/already-compressed', (c) => {
c.header('Content-Type', 'application/octet-stream')
c.header('Content-Encoding', 'br')
c.header('Content-Length', '1024')
return c.body(new Uint8Array(1024)) // Simulated compressed data
})
app.get('/stream', (c) =>
stream(c, async (stream) => {
c.header('Content-Type', 'text/plain')
// 60000 bytes
for (let i = 0; i < 10000; i++) {
await stream.write('chunk ')
}
})
)
app.get('/already-compressed-stream', (c) =>
stream(c, async (stream) => {
c.header('Content-Type', 'text/plain')
c.header('Content-Encoding', 'br')
// 60000 bytes
for (let i = 0; i < 10000; i++) {
await stream.write(new Uint8Array([0, 1, 2, 3, 4, 5])) // Simulated compressed data
}
})
)
app.notFound((c) => c.text('Custom NotFound', 404))
const testCompression = async (
path: string,
acceptEncoding: string,
expectedEncoding: string | null
) => {
const req = new Request(`http://localhost${path}`, {
method: 'GET',
headers: new Headers({ 'Accept-Encoding': acceptEncoding }),
})
const res = await app.request(req)
expect(res.headers.get('Content-Encoding')).toBe(expectedEncoding)
return res
}
describe('Compression Behavior', () => {
it('should compress large responses with gzip', async () => {
const res = await testCompression('/large', 'gzip', 'gzip')
expect(res.headers.get('Content-Length')).toBeNull()
expect((await res.arrayBuffer()).byteLength).toBeLessThan(1024)
})
it('should compress large responses with deflate', async () => {
const res = await testCompression('/large', 'deflate', 'deflate')
expect((await res.arrayBuffer()).byteLength).toBeLessThan(1024)
})
it('should prioritize gzip over deflate when both are accepted', async () => {
await testCompression('/large', 'gzip, deflate', 'gzip')
})
it('should not compress small responses', async () => {
const res = await testCompression('/small', 'gzip, deflate', null)
expect(res.headers.get('Content-Length')).toBe('5')
})
it('should not compress when no Accept-Encoding is provided', async () => {
await testCompression('/large', '', null)
})
it('should not compress images', async () => {
const res = await testCompression('/jpeg-image', 'gzip', null)
expect(res.headers.get('Content-Type')).toBe('image/jpeg')
expect(res.headers.get('Content-Length')).toBe('1024')
})
it('should not compress already compressed responses', async () => {
const res = await testCompression('/already-compressed', 'gzip', 'br')
expect(res.headers.get('Content-Length')).toBe('1024')
})
it('should remove Content-Length when compressing', async () => {
const res = await testCompression('/large', 'gzip', 'gzip')
expect(res.headers.get('Content-Length')).toBeNull()
})
it('should not remove Content-Length when not compressing', async () => {
const res = await testCompression('/jpeg-image', 'gzip', null)
expect(res.headers.get('Content-Length')).toBeDefined()
})
})
describe('JSON Handling', () => {
it('should not compress small JSON responses', async () => {
const res = await testCompression('/small-json', 'gzip', null)
expect(res.headers.get('Content-Length')).toBe('26')
})
it('should compress large JSON responses', async () => {
const res = await testCompression('/large-json', 'gzip', 'gzip')
expect(res.headers.get('Content-Length')).toBeNull()
const decompressed = await decompressResponse(res)
const json = JSON.parse(decompressed)
expect(json.data.length).toBe(1024)
expect(json.message).toBe('Large JSON')
})
})
describe('Streaming Responses', () => {
it('should compress streaming responses written in multiple chunks', async () => {
const res = await testCompression('/stream', 'gzip', 'gzip')
const decompressed = await decompressResponse(res)
expect(decompressed.length).toBe(60000)
})
it('should not compress already compressed streaming responses', async () => {
const res = await testCompression('/already-compressed-stream', 'gzip', 'br')
expect((await res.arrayBuffer()).byteLength).toBe(60000)
})
})
describe('Edge Cases', () => {
it('should not compress responses with Cache-Control: no-transform', async () => {
await testCompression('/no-transform', 'gzip', null)
})
it('should handle HEAD requests without compression', async () => {
const req = new Request('http://localhost/large', {
method: 'HEAD',
headers: new Headers({ 'Accept-Encoding': 'gzip' }),
})
const res = await app.request(req)
expect(res.headers.get('Content-Encoding')).toBeNull()
})
it('should compress custom 404 Not Found responses', async () => {
const res = await testCompression('/not-found', 'gzip', 'gzip')
expect(res.status).toBe(404)
const decompressed = await decompressResponse(res)
expect(decompressed).toBe('Custom NotFound')
})
})
})
async function decompressResponse(res: Response): Promise<string> {
const decompressedStream = res.body!.pipeThrough(new DecompressionStream('gzip'))
const decompressedResponse = new Response(decompressedStream)
return await decompressedResponse.text()
}