@fastify/compress 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/dependabot.yml +13 -0
- package/.github/stale.yml +21 -0
- package/.github/workflows/ci.yml +47 -0
- package/.taprc +6 -0
- package/LICENSE +21 -0
- package/README.md +310 -0
- package/index.d.ts +81 -0
- package/index.js +556 -0
- package/lib/utils.js +7 -0
- package/package.json +73 -0
- package/test/global-compress.test.js +3048 -0
- package/test/global-decompress.test.js +423 -0
- package/test/routes-compress.test.js +437 -0
- package/test/routes-decompress.test.js +305 -0
- package/test/types/index.test-d.ts +115 -0
- package/test/utils.test.js +27 -0
|
@@ -0,0 +1,3048 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('tap')
|
|
4
|
+
const { createReadStream, readFile, readFileSync } = require('fs')
|
|
5
|
+
const { Readable, Writable, PassThrough } = require('stream')
|
|
6
|
+
const zlib = require('zlib')
|
|
7
|
+
const AdmZip = require('adm-zip')
|
|
8
|
+
const JSONStream = require('jsonstream')
|
|
9
|
+
const Fastify = require('fastify')
|
|
10
|
+
const compressPlugin = require('../index')
|
|
11
|
+
|
|
12
|
+
test('When `global` is not set, it is `true` by default :', async (t) => {
|
|
13
|
+
t.test('it should compress Buffer data using brotli when `Accept-Encoding` request header is `br`', async (t) => {
|
|
14
|
+
t.plan(1)
|
|
15
|
+
|
|
16
|
+
const fastify = Fastify()
|
|
17
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
18
|
+
|
|
19
|
+
const buf = Buffer.from('hello world')
|
|
20
|
+
fastify.get('/', (request, reply) => {
|
|
21
|
+
reply.send(buf)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
const response = await fastify.inject({
|
|
25
|
+
url: '/',
|
|
26
|
+
method: 'GET',
|
|
27
|
+
headers: {
|
|
28
|
+
'accept-encoding': 'br'
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
const payload = zlib.brotliDecompressSync(response.rawPayload)
|
|
32
|
+
t.equal(payload.toString('utf-8'), buf.toString())
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
t.test('it should compress Buffer data using deflate when `Accept-Encoding` request header is `deflate`', async (t) => {
|
|
36
|
+
t.plan(1)
|
|
37
|
+
|
|
38
|
+
const fastify = Fastify()
|
|
39
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
40
|
+
|
|
41
|
+
const buf = Buffer.from('hello world')
|
|
42
|
+
fastify.get('/', (request, reply) => {
|
|
43
|
+
reply.send(buf)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
const response = await fastify.inject({
|
|
47
|
+
url: '/',
|
|
48
|
+
method: 'GET',
|
|
49
|
+
headers: {
|
|
50
|
+
'accept-encoding': 'deflate'
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
54
|
+
t.equal(payload.toString('utf-8'), buf.toString())
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
t.test('it should compress Buffer data using gzip when `Accept-Encoding` request header is `gzip`', async (t) => {
|
|
58
|
+
t.plan(1)
|
|
59
|
+
|
|
60
|
+
const fastify = Fastify()
|
|
61
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
62
|
+
|
|
63
|
+
const buf = Buffer.from('hello world')
|
|
64
|
+
fastify.get('/', (request, reply) => {
|
|
65
|
+
reply.send(buf)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
const response = await fastify.inject({
|
|
69
|
+
url: '/',
|
|
70
|
+
method: 'GET',
|
|
71
|
+
headers: {
|
|
72
|
+
'accept-encoding': 'gzip'
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
76
|
+
t.equal(payload.toString('utf-8'), buf.toString())
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
t.test('it should compress JSON data using brotli when `Accept-Encoding` request header is `br`', async (t) => {
|
|
80
|
+
t.plan(1)
|
|
81
|
+
|
|
82
|
+
const fastify = Fastify()
|
|
83
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
84
|
+
|
|
85
|
+
const json = { hello: 'world' }
|
|
86
|
+
|
|
87
|
+
fastify.get('/', (request, reply) => {
|
|
88
|
+
reply.send(json)
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
const response = await fastify.inject({
|
|
92
|
+
url: '/',
|
|
93
|
+
method: 'GET',
|
|
94
|
+
headers: {
|
|
95
|
+
'accept-encoding': 'br'
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
const payload = zlib.brotliDecompressSync(response.rawPayload)
|
|
99
|
+
t.equal(payload.toString('utf-8'), JSON.stringify(json))
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
t.test('it should compress JSON data using deflate when `Accept-Encoding` request header is `deflate`', async (t) => {
|
|
103
|
+
t.plan(1)
|
|
104
|
+
|
|
105
|
+
const fastify = Fastify()
|
|
106
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
107
|
+
|
|
108
|
+
const json = { hello: 'world' }
|
|
109
|
+
|
|
110
|
+
fastify.get('/', (request, reply) => {
|
|
111
|
+
reply.send(json)
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
const response = await fastify.inject({
|
|
115
|
+
url: '/',
|
|
116
|
+
method: 'GET',
|
|
117
|
+
headers: {
|
|
118
|
+
'accept-encoding': 'deflate'
|
|
119
|
+
}
|
|
120
|
+
})
|
|
121
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
122
|
+
t.equal(payload.toString('utf-8'), JSON.stringify(json))
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
t.test('it should compress JSON data using gzip when `Accept-Encoding` request header is `gzip`', async (t) => {
|
|
126
|
+
t.plan(1)
|
|
127
|
+
|
|
128
|
+
const fastify = Fastify()
|
|
129
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
130
|
+
|
|
131
|
+
const json = { hello: 'world' }
|
|
132
|
+
fastify.get('/', (request, reply) => {
|
|
133
|
+
reply.send(json)
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
const response = await fastify.inject({
|
|
137
|
+
url: '/',
|
|
138
|
+
method: 'GET',
|
|
139
|
+
headers: {
|
|
140
|
+
'accept-encoding': 'gzip'
|
|
141
|
+
}
|
|
142
|
+
})
|
|
143
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
144
|
+
t.equal(payload.toString('utf-8'), JSON.stringify(json))
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
t.test('it should compress string data using brotli when `Accept-Encoding` request header is `br', async (t) => {
|
|
148
|
+
t.plan(1)
|
|
149
|
+
|
|
150
|
+
const fastify = Fastify()
|
|
151
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
152
|
+
|
|
153
|
+
fastify.get('/', (request, reply) => {
|
|
154
|
+
reply
|
|
155
|
+
.type('text/plain')
|
|
156
|
+
.send('hello')
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
const response = await fastify.inject({
|
|
160
|
+
url: '/',
|
|
161
|
+
method: 'GET',
|
|
162
|
+
headers: {
|
|
163
|
+
'accept-encoding': 'br'
|
|
164
|
+
}
|
|
165
|
+
})
|
|
166
|
+
const payload = zlib.brotliDecompressSync(response.rawPayload)
|
|
167
|
+
t.equal(payload.toString('utf-8'), 'hello')
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
t.test('it should compress string data using deflate when `Accept-Encoding` request header is `deflate', async (t) => {
|
|
171
|
+
t.plan(1)
|
|
172
|
+
|
|
173
|
+
const fastify = Fastify()
|
|
174
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
175
|
+
|
|
176
|
+
fastify.get('/', (request, reply) => {
|
|
177
|
+
reply
|
|
178
|
+
.type('text/plain')
|
|
179
|
+
.compress('hello')
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
const response = await fastify.inject({
|
|
183
|
+
url: '/',
|
|
184
|
+
method: 'GET',
|
|
185
|
+
headers: {
|
|
186
|
+
'accept-encoding': 'deflate'
|
|
187
|
+
}
|
|
188
|
+
})
|
|
189
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
190
|
+
t.equal(payload.toString('utf-8'), 'hello')
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
t.test('it should compress string data using gzip when `Accept-Encoding` request header is `gzip', async (t) => {
|
|
194
|
+
t.plan(1)
|
|
195
|
+
|
|
196
|
+
const fastify = Fastify()
|
|
197
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
198
|
+
|
|
199
|
+
fastify.get('/', (request, reply) => {
|
|
200
|
+
reply
|
|
201
|
+
.type('text/plain')
|
|
202
|
+
.compress('hello')
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
const response = await fastify.inject({
|
|
206
|
+
url: '/',
|
|
207
|
+
method: 'GET',
|
|
208
|
+
headers: {
|
|
209
|
+
'accept-encoding': 'gzip'
|
|
210
|
+
}
|
|
211
|
+
})
|
|
212
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
213
|
+
t.equal(payload.toString('utf-8'), 'hello')
|
|
214
|
+
})
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
test('It should send compressed Stream data when `global` is `true` :', async (t) => {
|
|
218
|
+
t.test('using brotli when `Accept-Encoding` request header is `br`', async (t) => {
|
|
219
|
+
t.plan(3)
|
|
220
|
+
|
|
221
|
+
const fastify = Fastify()
|
|
222
|
+
await fastify.register(compressPlugin, { global: true })
|
|
223
|
+
|
|
224
|
+
fastify.get('/', (request, reply) => {
|
|
225
|
+
reply
|
|
226
|
+
.type('text/plain')
|
|
227
|
+
.compress(createReadStream('./package.json'))
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
const response = await fastify.inject({
|
|
231
|
+
url: '/',
|
|
232
|
+
method: 'GET',
|
|
233
|
+
headers: {
|
|
234
|
+
'accept-encoding': 'br'
|
|
235
|
+
}
|
|
236
|
+
})
|
|
237
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
238
|
+
const payload = zlib.brotliDecompressSync(response.rawPayload)
|
|
239
|
+
t.equal(response.headers.vary, 'accept-encoding')
|
|
240
|
+
t.equal(response.headers['content-encoding'], 'br')
|
|
241
|
+
t.equal(payload.toString('utf-8'), file)
|
|
242
|
+
})
|
|
243
|
+
|
|
244
|
+
t.test('using deflate when `Accept-Encoding` request header is `deflate`', async (t) => {
|
|
245
|
+
t.plan(4)
|
|
246
|
+
|
|
247
|
+
const fastify = Fastify()
|
|
248
|
+
await fastify.register(compressPlugin, { global: true })
|
|
249
|
+
|
|
250
|
+
fastify.get('/', (request, reply) => {
|
|
251
|
+
reply
|
|
252
|
+
.type('text/plain')
|
|
253
|
+
.compress(createReadStream('./package.json'))
|
|
254
|
+
})
|
|
255
|
+
|
|
256
|
+
const response = await fastify.inject({
|
|
257
|
+
url: '/',
|
|
258
|
+
method: 'GET',
|
|
259
|
+
headers: {
|
|
260
|
+
'accept-encoding': 'deflate'
|
|
261
|
+
}
|
|
262
|
+
})
|
|
263
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
264
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
265
|
+
t.equal(response.headers['content-encoding'], 'deflate')
|
|
266
|
+
t.equal(response.headers.vary, 'accept-encoding')
|
|
267
|
+
t.notOk(response.headers['content-length'], 'no content length')
|
|
268
|
+
t.equal(payload.toString('utf-8'), file)
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
t.test('using gzip when `Accept-Encoding` request header is `gzip`', async (t) => {
|
|
272
|
+
t.plan(2)
|
|
273
|
+
|
|
274
|
+
const fastify = Fastify()
|
|
275
|
+
await fastify.register(compressPlugin, { global: true })
|
|
276
|
+
|
|
277
|
+
fastify.get('/', (request, reply) => {
|
|
278
|
+
reply
|
|
279
|
+
.type('text/plain')
|
|
280
|
+
.compress(createReadStream('./package.json'))
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
const response = await fastify.inject({
|
|
284
|
+
url: '/',
|
|
285
|
+
method: 'GET',
|
|
286
|
+
headers: {
|
|
287
|
+
'accept-encoding': 'gzip'
|
|
288
|
+
}
|
|
289
|
+
})
|
|
290
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
291
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
292
|
+
t.equal(response.headers['content-encoding'], 'gzip')
|
|
293
|
+
t.equal(payload.toString('utf-8'), file)
|
|
294
|
+
})
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
test('It should send compressed Buffer data when `global` is `true` :', async (t) => {
|
|
298
|
+
t.test('using brotli when `Accept-Encoding` request header is `br`', async (t) => {
|
|
299
|
+
t.plan(1)
|
|
300
|
+
|
|
301
|
+
const fastify = Fastify()
|
|
302
|
+
await fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
303
|
+
|
|
304
|
+
const buf = Buffer.from('hello world')
|
|
305
|
+
fastify.get('/', (request, reply) => {
|
|
306
|
+
reply.compress(buf)
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
const response = await fastify.inject({
|
|
310
|
+
url: '/',
|
|
311
|
+
method: 'GET',
|
|
312
|
+
headers: {
|
|
313
|
+
'accept-encoding': 'br'
|
|
314
|
+
}
|
|
315
|
+
})
|
|
316
|
+
const payload = zlib.brotliDecompressSync(response.rawPayload)
|
|
317
|
+
t.equal(payload.toString('utf-8'), buf.toString())
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
t.test('using deflate when `Accept-Encoding` request header is `deflate`', async (t) => {
|
|
321
|
+
t.plan(1)
|
|
322
|
+
|
|
323
|
+
const fastify = Fastify()
|
|
324
|
+
await fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
325
|
+
|
|
326
|
+
const buf = Buffer.from('hello world')
|
|
327
|
+
fastify.get('/', (request, reply) => {
|
|
328
|
+
reply.compress(buf)
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
const response = await fastify.inject({
|
|
332
|
+
url: '/',
|
|
333
|
+
method: 'GET',
|
|
334
|
+
headers: {
|
|
335
|
+
'accept-encoding': 'deflate'
|
|
336
|
+
}
|
|
337
|
+
})
|
|
338
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
339
|
+
t.equal(payload.toString('utf-8'), buf.toString())
|
|
340
|
+
})
|
|
341
|
+
|
|
342
|
+
t.test('using gzip when `Accept-Encoding` request header is `gzip`', async (t) => {
|
|
343
|
+
t.plan(1)
|
|
344
|
+
|
|
345
|
+
const fastify = Fastify()
|
|
346
|
+
fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
347
|
+
|
|
348
|
+
const buf = Buffer.from('hello world')
|
|
349
|
+
fastify.get('/', (request, reply) => {
|
|
350
|
+
reply.compress(buf)
|
|
351
|
+
})
|
|
352
|
+
|
|
353
|
+
const response = await fastify.inject({
|
|
354
|
+
url: '/',
|
|
355
|
+
method: 'GET',
|
|
356
|
+
headers: {
|
|
357
|
+
'accept-encoding': 'gzip'
|
|
358
|
+
}
|
|
359
|
+
})
|
|
360
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
361
|
+
t.equal(payload.toString('utf-8'), buf.toString())
|
|
362
|
+
})
|
|
363
|
+
})
|
|
364
|
+
|
|
365
|
+
test('It should send compressed JSON data when `global` is `true` :', async (t) => {
|
|
366
|
+
t.test('using brotli when `Accept-Encoding` request header is `br`', async (t) => {
|
|
367
|
+
t.plan(1)
|
|
368
|
+
|
|
369
|
+
const fastify = Fastify()
|
|
370
|
+
await fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
371
|
+
|
|
372
|
+
const json = { hello: 'world' }
|
|
373
|
+
fastify.get('/', (request, reply) => {
|
|
374
|
+
reply.compress(json)
|
|
375
|
+
})
|
|
376
|
+
|
|
377
|
+
const response = await fastify.inject({
|
|
378
|
+
url: '/',
|
|
379
|
+
method: 'GET',
|
|
380
|
+
headers: {
|
|
381
|
+
'accept-encoding': 'br'
|
|
382
|
+
}
|
|
383
|
+
})
|
|
384
|
+
const payload = zlib.brotliDecompressSync(response.rawPayload)
|
|
385
|
+
t.equal(payload.toString('utf-8'), JSON.stringify(json))
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
t.test('using deflate when `Accept-Encoding` request header is `deflate`', async (t) => {
|
|
389
|
+
t.plan(1)
|
|
390
|
+
|
|
391
|
+
const fastify = Fastify()
|
|
392
|
+
await fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
393
|
+
|
|
394
|
+
const json = { hello: 'world' }
|
|
395
|
+
fastify.get('/', (request, reply) => {
|
|
396
|
+
reply.compress(json)
|
|
397
|
+
})
|
|
398
|
+
|
|
399
|
+
const response = await fastify.inject({
|
|
400
|
+
url: '/',
|
|
401
|
+
method: 'GET',
|
|
402
|
+
headers: {
|
|
403
|
+
'accept-encoding': 'deflate'
|
|
404
|
+
}
|
|
405
|
+
})
|
|
406
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
407
|
+
t.equal(payload.toString('utf-8'), JSON.stringify(json))
|
|
408
|
+
})
|
|
409
|
+
|
|
410
|
+
t.test('using gzip when `Accept-Encoding` request header is `gzip`', async (t) => {
|
|
411
|
+
t.plan(1)
|
|
412
|
+
|
|
413
|
+
const fastify = Fastify()
|
|
414
|
+
await fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
415
|
+
|
|
416
|
+
const json = { hello: 'world' }
|
|
417
|
+
fastify.get('/', (request, reply) => {
|
|
418
|
+
reply.compress(json)
|
|
419
|
+
})
|
|
420
|
+
|
|
421
|
+
const response = await fastify.inject({
|
|
422
|
+
url: '/',
|
|
423
|
+
method: 'GET',
|
|
424
|
+
headers: {
|
|
425
|
+
'accept-encoding': 'gzip'
|
|
426
|
+
}
|
|
427
|
+
})
|
|
428
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
429
|
+
t.equal(payload.toString('utf-8'), JSON.stringify(json))
|
|
430
|
+
})
|
|
431
|
+
})
|
|
432
|
+
|
|
433
|
+
test('It should fallback to the default `gzip` encoding compression :', async (t) => {
|
|
434
|
+
t.test('when `Accept-Encoding` request header value is set to `*`', async (t) => {
|
|
435
|
+
t.plan(2)
|
|
436
|
+
|
|
437
|
+
const fastify = Fastify()
|
|
438
|
+
await fastify.register(compressPlugin, { global: true })
|
|
439
|
+
|
|
440
|
+
fastify.get('/', (request, reply) => {
|
|
441
|
+
reply
|
|
442
|
+
.type('text/plain')
|
|
443
|
+
.compress(createReadStream('./package.json'))
|
|
444
|
+
})
|
|
445
|
+
|
|
446
|
+
const response = await fastify.inject({
|
|
447
|
+
url: '/',
|
|
448
|
+
method: 'GET',
|
|
449
|
+
headers: {
|
|
450
|
+
'accept-encoding': '*'
|
|
451
|
+
}
|
|
452
|
+
})
|
|
453
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
454
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
455
|
+
t.equal(response.headers['content-encoding'], 'gzip')
|
|
456
|
+
t.equal(payload.toString('utf-8'), file)
|
|
457
|
+
})
|
|
458
|
+
|
|
459
|
+
t.test('when `Accept-Encoding` request header value is set to multiple `*` directives', async (t) => {
|
|
460
|
+
t.plan(2)
|
|
461
|
+
|
|
462
|
+
const fastify = Fastify()
|
|
463
|
+
await fastify.register(compressPlugin, { global: true })
|
|
464
|
+
|
|
465
|
+
fastify.get('/', (request, reply) => {
|
|
466
|
+
reply
|
|
467
|
+
.type('text/plain')
|
|
468
|
+
.compress(createReadStream('./package.json'))
|
|
469
|
+
})
|
|
470
|
+
|
|
471
|
+
const response = await fastify.inject({
|
|
472
|
+
url: '/',
|
|
473
|
+
method: 'GET',
|
|
474
|
+
headers: {
|
|
475
|
+
'accept-encoding': '*,*'
|
|
476
|
+
}
|
|
477
|
+
})
|
|
478
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
479
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
480
|
+
t.equal(response.headers['content-encoding'], 'gzip')
|
|
481
|
+
t.equal(payload.toString('utf-8'), file)
|
|
482
|
+
})
|
|
483
|
+
})
|
|
484
|
+
|
|
485
|
+
test('When a custom `zlib` option is provided, it should compress data :`', async (t) => {
|
|
486
|
+
t.test('using the custom `createBrotliCompress()` method', async (t) => {
|
|
487
|
+
t.plan(4)
|
|
488
|
+
|
|
489
|
+
let usedCustom = false
|
|
490
|
+
const customZlib = { createBrotliCompress: () => (usedCustom = true) && zlib.createBrotliCompress() }
|
|
491
|
+
|
|
492
|
+
const fastify = Fastify()
|
|
493
|
+
await fastify.register(compressPlugin, { global: true, zlib: customZlib })
|
|
494
|
+
|
|
495
|
+
fastify.get('/', (request, reply) => {
|
|
496
|
+
reply
|
|
497
|
+
.type('text/plain')
|
|
498
|
+
.compress(createReadStream('./package.json'))
|
|
499
|
+
})
|
|
500
|
+
|
|
501
|
+
const response = await fastify.inject({
|
|
502
|
+
url: '/',
|
|
503
|
+
method: 'GET',
|
|
504
|
+
headers: {
|
|
505
|
+
'accept-encoding': 'br'
|
|
506
|
+
}
|
|
507
|
+
})
|
|
508
|
+
t.equal(usedCustom, true)
|
|
509
|
+
|
|
510
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
511
|
+
const payload = zlib.brotliDecompressSync(response.rawPayload)
|
|
512
|
+
t.equal(response.headers['content-encoding'], 'br')
|
|
513
|
+
t.notOk(response.headers['content-length'], 'no content length')
|
|
514
|
+
t.equal(payload.toString('utf-8'), file)
|
|
515
|
+
})
|
|
516
|
+
|
|
517
|
+
t.test('using the custom `createDeflate()` method', async (t) => {
|
|
518
|
+
t.plan(4)
|
|
519
|
+
|
|
520
|
+
let usedCustom = false
|
|
521
|
+
const customZlib = { createDeflate: () => (usedCustom = true) && zlib.createDeflate() }
|
|
522
|
+
|
|
523
|
+
const fastify = Fastify()
|
|
524
|
+
await fastify.register(compressPlugin, { global: true, zlib: customZlib })
|
|
525
|
+
|
|
526
|
+
fastify.get('/', (request, reply) => {
|
|
527
|
+
reply
|
|
528
|
+
.type('text/plain')
|
|
529
|
+
.compress(createReadStream('./package.json'))
|
|
530
|
+
})
|
|
531
|
+
|
|
532
|
+
const response = await fastify.inject({
|
|
533
|
+
url: '/',
|
|
534
|
+
method: 'GET',
|
|
535
|
+
headers: {
|
|
536
|
+
'accept-encoding': 'deflate'
|
|
537
|
+
}
|
|
538
|
+
})
|
|
539
|
+
t.equal(usedCustom, true)
|
|
540
|
+
|
|
541
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
542
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
543
|
+
t.equal(response.headers['content-encoding'], 'deflate')
|
|
544
|
+
t.notOk(response.headers['content-length'], 'no content length')
|
|
545
|
+
t.equal(payload.toString('utf-8'), file)
|
|
546
|
+
})
|
|
547
|
+
|
|
548
|
+
t.test('using the custom `createGzip()` method', async (t) => {
|
|
549
|
+
t.plan(3)
|
|
550
|
+
|
|
551
|
+
let usedCustom = false
|
|
552
|
+
const customZlib = { createGzip: () => (usedCustom = true) && zlib.createGzip() }
|
|
553
|
+
|
|
554
|
+
const fastify = Fastify()
|
|
555
|
+
await fastify.register(compressPlugin, { global: true, zlib: customZlib })
|
|
556
|
+
|
|
557
|
+
fastify.get('/', (request, reply) => {
|
|
558
|
+
reply
|
|
559
|
+
.type('text/plain')
|
|
560
|
+
.compress(createReadStream('./package.json'))
|
|
561
|
+
})
|
|
562
|
+
|
|
563
|
+
const response = await fastify.inject({
|
|
564
|
+
url: '/',
|
|
565
|
+
method: 'GET',
|
|
566
|
+
headers: {
|
|
567
|
+
'accept-encoding': 'gzip'
|
|
568
|
+
}
|
|
569
|
+
})
|
|
570
|
+
t.equal(usedCustom, true)
|
|
571
|
+
|
|
572
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
573
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
574
|
+
t.equal(response.headers['content-encoding'], 'gzip')
|
|
575
|
+
t.equal(payload.toString('utf-8'), file)
|
|
576
|
+
})
|
|
577
|
+
})
|
|
578
|
+
|
|
579
|
+
test('When a malformed custom `zlib` option is provided, it should compress data :', async (t) => {
|
|
580
|
+
t.test('using the fallback default Node.js core `zlib.createBrotliCompress()` method', async (t) => {
|
|
581
|
+
t.plan(1)
|
|
582
|
+
|
|
583
|
+
const fastify = Fastify()
|
|
584
|
+
await fastify.register(compressPlugin, {
|
|
585
|
+
global: true,
|
|
586
|
+
threshold: 0,
|
|
587
|
+
zlib: true // will trigger a fallback on the default zlib.createBrotliCompress
|
|
588
|
+
})
|
|
589
|
+
|
|
590
|
+
fastify.get('/', (request, reply) => {
|
|
591
|
+
reply
|
|
592
|
+
.type('text/plain')
|
|
593
|
+
.compress('hello')
|
|
594
|
+
})
|
|
595
|
+
|
|
596
|
+
const response = await fastify.inject({
|
|
597
|
+
url: '/',
|
|
598
|
+
method: 'GET',
|
|
599
|
+
headers: {
|
|
600
|
+
'accept-encoding': 'br'
|
|
601
|
+
}
|
|
602
|
+
})
|
|
603
|
+
const payload = zlib.brotliDecompressSync(response.rawPayload)
|
|
604
|
+
t.equal(payload.toString('utf-8'), 'hello')
|
|
605
|
+
})
|
|
606
|
+
|
|
607
|
+
t.test('using the fallback default Node.js core `zlib.createDeflate()` method', async (t) => {
|
|
608
|
+
t.plan(1)
|
|
609
|
+
|
|
610
|
+
const fastify = Fastify()
|
|
611
|
+
await fastify.register(compressPlugin, {
|
|
612
|
+
global: true,
|
|
613
|
+
threshold: 0,
|
|
614
|
+
zlib: true // will trigger a fallback on the default zlib.createDeflate
|
|
615
|
+
})
|
|
616
|
+
|
|
617
|
+
fastify.get('/', (request, reply) => {
|
|
618
|
+
reply
|
|
619
|
+
.type('text/plain')
|
|
620
|
+
.compress('hello')
|
|
621
|
+
})
|
|
622
|
+
|
|
623
|
+
const response = await fastify.inject({
|
|
624
|
+
url: '/',
|
|
625
|
+
method: 'GET',
|
|
626
|
+
headers: {
|
|
627
|
+
'accept-encoding': 'deflate'
|
|
628
|
+
}
|
|
629
|
+
})
|
|
630
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
631
|
+
t.equal(payload.toString('utf-8'), 'hello')
|
|
632
|
+
})
|
|
633
|
+
|
|
634
|
+
t.test('using the fallback default Node.js core `zlib.createGzip()` method', async (t) => {
|
|
635
|
+
t.plan(1)
|
|
636
|
+
|
|
637
|
+
const fastify = Fastify()
|
|
638
|
+
await fastify.register(compressPlugin, {
|
|
639
|
+
global: true,
|
|
640
|
+
threshold: 0,
|
|
641
|
+
zlib: true // will trigger a fallback on the default zlib.createGzip
|
|
642
|
+
})
|
|
643
|
+
|
|
644
|
+
fastify.get('/', (request, reply) => {
|
|
645
|
+
reply
|
|
646
|
+
.type('text/plain')
|
|
647
|
+
.compress('hello')
|
|
648
|
+
})
|
|
649
|
+
|
|
650
|
+
const response = await fastify.inject({
|
|
651
|
+
url: '/',
|
|
652
|
+
method: 'GET',
|
|
653
|
+
headers: {
|
|
654
|
+
'accept-encoding': 'gzip'
|
|
655
|
+
}
|
|
656
|
+
})
|
|
657
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
658
|
+
t.equal(payload.toString('utf-8'), 'hello')
|
|
659
|
+
})
|
|
660
|
+
})
|
|
661
|
+
|
|
662
|
+
test('When `inflateIfDeflated` is `true` and `X-No-Compression` request header is `true` :', async (t) => {
|
|
663
|
+
t.test('it should uncompress payloads using the deflate algorithm', async (t) => {
|
|
664
|
+
t.plan(3)
|
|
665
|
+
|
|
666
|
+
const fastify = Fastify()
|
|
667
|
+
await fastify.register(compressPlugin, { threshold: 0, inflateIfDeflated: true })
|
|
668
|
+
|
|
669
|
+
const json = { hello: 'world' }
|
|
670
|
+
fastify.get('/', (request, reply) => {
|
|
671
|
+
reply.send(zlib.deflateSync(JSON.stringify(json)))
|
|
672
|
+
})
|
|
673
|
+
|
|
674
|
+
const response = await fastify.inject({
|
|
675
|
+
url: '/',
|
|
676
|
+
method: 'GET',
|
|
677
|
+
headers: {
|
|
678
|
+
'x-no-compression': true
|
|
679
|
+
}
|
|
680
|
+
})
|
|
681
|
+
t.equal(response.statusCode, 200)
|
|
682
|
+
t.notOk(response.headers['content-encoding'])
|
|
683
|
+
t.same(JSON.parse('' + response.payload), json)
|
|
684
|
+
})
|
|
685
|
+
|
|
686
|
+
t.test('it should uncompress payloads using the gzip algorithm', async (t) => {
|
|
687
|
+
t.plan(3)
|
|
688
|
+
|
|
689
|
+
const fastify = Fastify()
|
|
690
|
+
await fastify.register(compressPlugin, { threshold: 0, inflateIfDeflated: true })
|
|
691
|
+
|
|
692
|
+
const json = { hello: 'world' }
|
|
693
|
+
fastify.get('/', (request, reply) => {
|
|
694
|
+
reply.send(zlib.gzipSync(JSON.stringify(json)))
|
|
695
|
+
})
|
|
696
|
+
|
|
697
|
+
const response = await fastify.inject({
|
|
698
|
+
url: '/',
|
|
699
|
+
method: 'GET',
|
|
700
|
+
headers: {
|
|
701
|
+
'x-no-compression': true
|
|
702
|
+
}
|
|
703
|
+
})
|
|
704
|
+
t.equal(response.statusCode, 200)
|
|
705
|
+
t.notOk(response.headers['content-encoding'])
|
|
706
|
+
t.same(JSON.parse('' + response.payload), json)
|
|
707
|
+
})
|
|
708
|
+
})
|
|
709
|
+
|
|
710
|
+
test('it should not uncompress payloads using the zip algorithm', async (t) => {
|
|
711
|
+
t.plan(4)
|
|
712
|
+
|
|
713
|
+
const fastify = Fastify()
|
|
714
|
+
await fastify.register(compressPlugin, { threshold: 0, inflateIfDeflated: true })
|
|
715
|
+
|
|
716
|
+
const json = { hello: 'world' }
|
|
717
|
+
const zip = new AdmZip()
|
|
718
|
+
zip.addFile('file.zip', Buffer.from(JSON.stringify(json), 'utf-8'))
|
|
719
|
+
const fileBuffer = zip.toBuffer()
|
|
720
|
+
|
|
721
|
+
fastify.get('/', (request, reply) => {
|
|
722
|
+
reply.compress(fileBuffer)
|
|
723
|
+
})
|
|
724
|
+
|
|
725
|
+
const response = await fastify.inject({
|
|
726
|
+
url: '/',
|
|
727
|
+
method: 'GET',
|
|
728
|
+
headers: {
|
|
729
|
+
'x-no-compression': true
|
|
730
|
+
}
|
|
731
|
+
})
|
|
732
|
+
t.equal(response.statusCode, 200)
|
|
733
|
+
t.notOk(response.headers['content-encoding'])
|
|
734
|
+
t.same(response.rawPayload, fileBuffer)
|
|
735
|
+
t.equal(response.payload, fileBuffer.toString('utf-8'))
|
|
736
|
+
})
|
|
737
|
+
|
|
738
|
+
test('It should not compress :', async (t) => {
|
|
739
|
+
t.test('Using `reply.compress()` :', async (t) => {
|
|
740
|
+
t.test('when payload length is smaller than the `threshold` defined value', async (t) => {
|
|
741
|
+
t.plan(3)
|
|
742
|
+
|
|
743
|
+
const fastify = Fastify()
|
|
744
|
+
await fastify.register(compressPlugin, { threshold: 128 })
|
|
745
|
+
|
|
746
|
+
fastify.get('/', (request, reply) => {
|
|
747
|
+
reply
|
|
748
|
+
.type('text/plain')
|
|
749
|
+
.compress('a message')
|
|
750
|
+
})
|
|
751
|
+
|
|
752
|
+
const response = await fastify.inject({
|
|
753
|
+
url: '/',
|
|
754
|
+
method: 'GET',
|
|
755
|
+
headers: {
|
|
756
|
+
'accept-encoding': 'deflate'
|
|
757
|
+
}
|
|
758
|
+
})
|
|
759
|
+
t.equal(response.statusCode, 200)
|
|
760
|
+
t.notOk(response.headers['content-encoding'])
|
|
761
|
+
t.equal(response.payload, 'a message')
|
|
762
|
+
})
|
|
763
|
+
|
|
764
|
+
t.test('when `customTypes` is set and does not match `Content-Type` reply header or `mime-db`', async (t) => {
|
|
765
|
+
t.plan(2)
|
|
766
|
+
|
|
767
|
+
const fastify = Fastify()
|
|
768
|
+
await fastify.register(compressPlugin, { customTypes: /x-user-header$/ })
|
|
769
|
+
|
|
770
|
+
fastify.get('/', (request, reply) => {
|
|
771
|
+
reply
|
|
772
|
+
.type('application/x-other-type')
|
|
773
|
+
.compress(createReadStream('./package.json'))
|
|
774
|
+
})
|
|
775
|
+
|
|
776
|
+
const response = await fastify.inject({
|
|
777
|
+
url: '/',
|
|
778
|
+
method: 'GET',
|
|
779
|
+
headers: {
|
|
780
|
+
'accept-encoding': 'gzip'
|
|
781
|
+
}
|
|
782
|
+
})
|
|
783
|
+
t.notOk(response.headers['content-encoding'])
|
|
784
|
+
t.equal(response.statusCode, 200)
|
|
785
|
+
})
|
|
786
|
+
|
|
787
|
+
t.test('when `X-No-Compression` request header is `true`', async (t) => {
|
|
788
|
+
t.plan(3)
|
|
789
|
+
|
|
790
|
+
const fastify = Fastify()
|
|
791
|
+
await fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
792
|
+
|
|
793
|
+
const json = { hello: 'world' }
|
|
794
|
+
fastify.get('/', (request, reply) => {
|
|
795
|
+
reply.compress(json)
|
|
796
|
+
})
|
|
797
|
+
|
|
798
|
+
const response = await fastify.inject({
|
|
799
|
+
url: '/',
|
|
800
|
+
method: 'GET',
|
|
801
|
+
headers: {
|
|
802
|
+
'x-no-compression': true
|
|
803
|
+
}
|
|
804
|
+
})
|
|
805
|
+
t.equal(response.statusCode, 200)
|
|
806
|
+
t.notOk(response.headers['content-encoding'])
|
|
807
|
+
t.same(JSON.parse(response.payload), json)
|
|
808
|
+
})
|
|
809
|
+
|
|
810
|
+
t.test('when `Content-Type` reply header is not set and the content is not detected as a compressible type', async (t) => {
|
|
811
|
+
t.plan(2)
|
|
812
|
+
|
|
813
|
+
const fastify = Fastify()
|
|
814
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
815
|
+
|
|
816
|
+
fastify.addHook('onSend', async (request, response) => {
|
|
817
|
+
response.header('Content-Type', undefined)
|
|
818
|
+
})
|
|
819
|
+
|
|
820
|
+
const json = { hello: 'world' }
|
|
821
|
+
fastify.get('/', (request, reply) => {
|
|
822
|
+
// The auto-dectection will fallback as an 'application/json' type
|
|
823
|
+
reply.compress(json)
|
|
824
|
+
})
|
|
825
|
+
|
|
826
|
+
const response = await fastify.inject({
|
|
827
|
+
url: '/',
|
|
828
|
+
method: 'GET',
|
|
829
|
+
headers: {
|
|
830
|
+
accept: 'application/json',
|
|
831
|
+
'accept-encoding': 'identity'
|
|
832
|
+
}
|
|
833
|
+
})
|
|
834
|
+
t.notOk(response.headers['content-encoding'])
|
|
835
|
+
t.equal(response.payload, JSON.stringify(json))
|
|
836
|
+
})
|
|
837
|
+
|
|
838
|
+
t.test('when `Content-Type` reply header is a mime type with undefined compressible values', async (t) => {
|
|
839
|
+
t.plan(3)
|
|
840
|
+
|
|
841
|
+
const fastify = Fastify()
|
|
842
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
843
|
+
|
|
844
|
+
fastify.get('/', (request, reply) => {
|
|
845
|
+
reply
|
|
846
|
+
.type('image/webp')
|
|
847
|
+
.compress('hello')
|
|
848
|
+
})
|
|
849
|
+
|
|
850
|
+
const response = await fastify.inject({
|
|
851
|
+
url: '/',
|
|
852
|
+
method: 'GET',
|
|
853
|
+
headers: {
|
|
854
|
+
'accept-encoding': 'gzip, deflate, br'
|
|
855
|
+
}
|
|
856
|
+
})
|
|
857
|
+
t.equal(response.statusCode, 200)
|
|
858
|
+
t.notOk(response.headers['content-encoding'])
|
|
859
|
+
t.equal(response.payload, 'hello')
|
|
860
|
+
})
|
|
861
|
+
|
|
862
|
+
t.test('when `Content-Type` reply header value is `text/event-stream`', async (t) => {
|
|
863
|
+
t.plan(3)
|
|
864
|
+
|
|
865
|
+
const fastify = Fastify()
|
|
866
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
867
|
+
|
|
868
|
+
fastify.get('/', (_req, reply) => {
|
|
869
|
+
const stream = new PassThrough()
|
|
870
|
+
|
|
871
|
+
reply
|
|
872
|
+
.type('text/event-stream')
|
|
873
|
+
.compress(stream)
|
|
874
|
+
|
|
875
|
+
stream.write('event: open\n\n')
|
|
876
|
+
stream.write('event: change\ndata: schema\n\n')
|
|
877
|
+
stream.end()
|
|
878
|
+
})
|
|
879
|
+
|
|
880
|
+
const response = await fastify.inject({
|
|
881
|
+
url: '/',
|
|
882
|
+
method: 'GET'
|
|
883
|
+
})
|
|
884
|
+
t.equal(response.statusCode, 200)
|
|
885
|
+
t.notOk(response.headers['content-encoding'])
|
|
886
|
+
t.same(response.payload, 'event: open\n\nevent: change\ndata: schema\n\n')
|
|
887
|
+
})
|
|
888
|
+
|
|
889
|
+
t.test('when `Content-Type` reply header value is an invalid type', async (t) => {
|
|
890
|
+
t.plan(3)
|
|
891
|
+
|
|
892
|
+
const fastify = Fastify()
|
|
893
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
894
|
+
|
|
895
|
+
fastify.get('/', (request, reply) => {
|
|
896
|
+
reply
|
|
897
|
+
.type('something/invalid')
|
|
898
|
+
.compress('a message')
|
|
899
|
+
})
|
|
900
|
+
|
|
901
|
+
const response = await fastify.inject({
|
|
902
|
+
url: '/',
|
|
903
|
+
method: 'GET',
|
|
904
|
+
headers: {
|
|
905
|
+
'accept-encoding': 'deflate'
|
|
906
|
+
}
|
|
907
|
+
})
|
|
908
|
+
t.equal(response.statusCode, 200)
|
|
909
|
+
t.notOk(response.headers['content-encoding'])
|
|
910
|
+
t.equal(response.payload, 'a message')
|
|
911
|
+
})
|
|
912
|
+
|
|
913
|
+
t.test('when `Accept-Encoding` request header is missing', async (t) => {
|
|
914
|
+
t.plan(3)
|
|
915
|
+
|
|
916
|
+
const fastify = Fastify()
|
|
917
|
+
await fastify.register(compressPlugin, { global: true })
|
|
918
|
+
|
|
919
|
+
fastify.get('/', (request, reply) => {
|
|
920
|
+
reply
|
|
921
|
+
.type('text/plain')
|
|
922
|
+
.compress(createReadStream('./package.json'))
|
|
923
|
+
})
|
|
924
|
+
|
|
925
|
+
const response = await fastify.inject({
|
|
926
|
+
url: '/',
|
|
927
|
+
method: 'GET'
|
|
928
|
+
})
|
|
929
|
+
t.equal(response.headers.vary, 'accept-encoding')
|
|
930
|
+
t.equal(response.statusCode, 200)
|
|
931
|
+
t.notOk(response.headers['content-encoding'])
|
|
932
|
+
})
|
|
933
|
+
|
|
934
|
+
t.test('when `Accept-Encoding` request header is set to `identity`', async (t) => {
|
|
935
|
+
t.plan(2)
|
|
936
|
+
|
|
937
|
+
const fastify = Fastify()
|
|
938
|
+
await fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
939
|
+
|
|
940
|
+
fastify.get('/', (request, reply) => {
|
|
941
|
+
reply.compress({ hello: 'world' })
|
|
942
|
+
})
|
|
943
|
+
|
|
944
|
+
const response = await fastify.inject({
|
|
945
|
+
url: '/',
|
|
946
|
+
method: 'GET',
|
|
947
|
+
headers: {
|
|
948
|
+
'accept-encoding': 'identity'
|
|
949
|
+
}
|
|
950
|
+
})
|
|
951
|
+
const payload = JSON.parse(response.payload)
|
|
952
|
+
t.notOk(response.headers['content-encoding'])
|
|
953
|
+
t.same({ hello: 'world' }, payload)
|
|
954
|
+
})
|
|
955
|
+
|
|
956
|
+
t.test('when `Accept-Encoding` request header value is not supported', async (t) => {
|
|
957
|
+
t.plan(3)
|
|
958
|
+
|
|
959
|
+
const fastify = Fastify()
|
|
960
|
+
await fastify.register(compressPlugin, { global: true })
|
|
961
|
+
|
|
962
|
+
fastify.get('/', (request, reply) => {
|
|
963
|
+
reply
|
|
964
|
+
.type('text/plain')
|
|
965
|
+
.compress(createReadStream('./package.json'))
|
|
966
|
+
})
|
|
967
|
+
|
|
968
|
+
const response = await fastify.inject({
|
|
969
|
+
url: '/',
|
|
970
|
+
method: 'GET',
|
|
971
|
+
headers: {
|
|
972
|
+
'accept-encoding': 'invalid'
|
|
973
|
+
}
|
|
974
|
+
})
|
|
975
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
976
|
+
t.equal(response.headers.vary, 'accept-encoding')
|
|
977
|
+
t.equal(response.statusCode, 200)
|
|
978
|
+
t.equal(response.payload, file)
|
|
979
|
+
})
|
|
980
|
+
|
|
981
|
+
t.test('when `Accept-Encoding` request header value is not supported (with quality value)', async (t) => {
|
|
982
|
+
t.plan(3)
|
|
983
|
+
|
|
984
|
+
const fastify = Fastify()
|
|
985
|
+
await fastify.register(compressPlugin, { global: true })
|
|
986
|
+
|
|
987
|
+
fastify.get('/', (request, reply) => {
|
|
988
|
+
reply
|
|
989
|
+
.type('text/plain')
|
|
990
|
+
.compress(createReadStream('./package.json'))
|
|
991
|
+
})
|
|
992
|
+
|
|
993
|
+
const response = await fastify.inject({
|
|
994
|
+
url: '/',
|
|
995
|
+
method: 'GET',
|
|
996
|
+
headers: {
|
|
997
|
+
'accept-encoding': 'lzma;q=1.0'
|
|
998
|
+
}
|
|
999
|
+
})
|
|
1000
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
1001
|
+
t.equal(response.headers.vary, 'accept-encoding')
|
|
1002
|
+
t.equal(response.statusCode, 200)
|
|
1003
|
+
t.equal(response.payload, file)
|
|
1004
|
+
})
|
|
1005
|
+
|
|
1006
|
+
t.test('when `Accept-Encoding` request header is set to `identity and `inflateIfDeflated` is `true``', async (t) => {
|
|
1007
|
+
t.plan(2)
|
|
1008
|
+
|
|
1009
|
+
const fastify = Fastify()
|
|
1010
|
+
await fastify.register(compressPlugin, { global: true, inflateIfDeflated: true, threshold: 0 })
|
|
1011
|
+
|
|
1012
|
+
fastify.get('/', (request, reply) => {
|
|
1013
|
+
reply.compress({ hello: 'world' })
|
|
1014
|
+
})
|
|
1015
|
+
|
|
1016
|
+
const response = await fastify.inject({
|
|
1017
|
+
url: '/',
|
|
1018
|
+
method: 'GET',
|
|
1019
|
+
headers: {
|
|
1020
|
+
'accept-encoding': 'identity'
|
|
1021
|
+
}
|
|
1022
|
+
})
|
|
1023
|
+
const payload = JSON.parse(response.payload)
|
|
1024
|
+
t.notOk(response.headers['content-encoding'])
|
|
1025
|
+
t.same({ hello: 'world' }, payload)
|
|
1026
|
+
})
|
|
1027
|
+
})
|
|
1028
|
+
|
|
1029
|
+
t.test('Using `onSend` hook :', async (t) => {
|
|
1030
|
+
t.test('when there is no payload', async (t) => {
|
|
1031
|
+
t.plan(3)
|
|
1032
|
+
|
|
1033
|
+
const fastify = Fastify()
|
|
1034
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
1035
|
+
|
|
1036
|
+
fastify.get('/', (request, reply) => {
|
|
1037
|
+
reply.send(undefined)
|
|
1038
|
+
})
|
|
1039
|
+
|
|
1040
|
+
const response = await fastify.inject({
|
|
1041
|
+
url: '/',
|
|
1042
|
+
method: 'GET',
|
|
1043
|
+
headers: {
|
|
1044
|
+
'accept-encoding': 'gzip'
|
|
1045
|
+
}
|
|
1046
|
+
})
|
|
1047
|
+
t.equal(response.statusCode, 200)
|
|
1048
|
+
t.notOk(response.headers['content-encoding'])
|
|
1049
|
+
t.equal(response.payload, '')
|
|
1050
|
+
})
|
|
1051
|
+
|
|
1052
|
+
t.test('when payload length is smaller than the `threshold` defined value', async (t) => {
|
|
1053
|
+
t.plan(3)
|
|
1054
|
+
|
|
1055
|
+
const fastify = Fastify()
|
|
1056
|
+
await fastify.register(compressPlugin, { threshold: 128 })
|
|
1057
|
+
|
|
1058
|
+
fastify.get('/', (request, reply) => {
|
|
1059
|
+
reply
|
|
1060
|
+
.header('Content-Type', 'text/plain')
|
|
1061
|
+
.send('a message')
|
|
1062
|
+
})
|
|
1063
|
+
|
|
1064
|
+
const response = await fastify.inject({
|
|
1065
|
+
url: '/',
|
|
1066
|
+
method: 'GET',
|
|
1067
|
+
headers: {
|
|
1068
|
+
'accept-encoding': 'deflate'
|
|
1069
|
+
}
|
|
1070
|
+
})
|
|
1071
|
+
t.equal(response.statusCode, 200)
|
|
1072
|
+
t.notOk(response.headers['content-encoding'])
|
|
1073
|
+
t.equal(response.payload, 'a message')
|
|
1074
|
+
})
|
|
1075
|
+
|
|
1076
|
+
t.test('when `customTypes` is set and does not match `Content-Type` reply header or `mime-db`', async (t) => {
|
|
1077
|
+
t.plan(2)
|
|
1078
|
+
|
|
1079
|
+
const fastify = Fastify()
|
|
1080
|
+
await fastify.register(compressPlugin, { customTypes: /x-user-header$/ })
|
|
1081
|
+
|
|
1082
|
+
fastify.get('/', (request, reply) => {
|
|
1083
|
+
reply
|
|
1084
|
+
.type('application/x-other-type')
|
|
1085
|
+
.send(createReadStream('./package.json'))
|
|
1086
|
+
})
|
|
1087
|
+
|
|
1088
|
+
const response = await fastify.inject({
|
|
1089
|
+
url: '/',
|
|
1090
|
+
method: 'GET',
|
|
1091
|
+
headers: {
|
|
1092
|
+
'accept-encoding': 'gzip'
|
|
1093
|
+
}
|
|
1094
|
+
})
|
|
1095
|
+
t.notOk(response.headers['content-encoding'])
|
|
1096
|
+
t.equal(response.statusCode, 200)
|
|
1097
|
+
})
|
|
1098
|
+
|
|
1099
|
+
t.test('when `X-No-Compression` request header is `true`', async (t) => {
|
|
1100
|
+
t.plan(3)
|
|
1101
|
+
|
|
1102
|
+
const fastify = Fastify()
|
|
1103
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
1104
|
+
|
|
1105
|
+
const json = { hello: 'world' }
|
|
1106
|
+
fastify.get('/', (request, reply) => {
|
|
1107
|
+
reply.send(json)
|
|
1108
|
+
})
|
|
1109
|
+
|
|
1110
|
+
const response = await fastify.inject({
|
|
1111
|
+
url: '/',
|
|
1112
|
+
method: 'GET',
|
|
1113
|
+
headers: {
|
|
1114
|
+
'x-no-compression': true
|
|
1115
|
+
}
|
|
1116
|
+
})
|
|
1117
|
+
t.equal(response.statusCode, 200)
|
|
1118
|
+
t.notOk(response.headers['content-encoding'])
|
|
1119
|
+
t.same(JSON.parse(response.payload), json)
|
|
1120
|
+
})
|
|
1121
|
+
|
|
1122
|
+
t.test('when `Content-Type` reply header is not set and the content is not detected as a compressible type', async (t) => {
|
|
1123
|
+
t.plan(2)
|
|
1124
|
+
|
|
1125
|
+
const fastify = Fastify()
|
|
1126
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
1127
|
+
|
|
1128
|
+
fastify.addHook('onSend', async (request, response) => {
|
|
1129
|
+
response.header('Content-Type', undefined)
|
|
1130
|
+
})
|
|
1131
|
+
|
|
1132
|
+
const json = { hello: 'world' }
|
|
1133
|
+
fastify.get('/', (request, reply) => {
|
|
1134
|
+
// The auto-dectection will fallback as an 'application/json' type
|
|
1135
|
+
reply.send(json)
|
|
1136
|
+
})
|
|
1137
|
+
|
|
1138
|
+
const response = await fastify.inject({
|
|
1139
|
+
url: '/',
|
|
1140
|
+
method: 'GET',
|
|
1141
|
+
headers: {
|
|
1142
|
+
accept: 'application/json',
|
|
1143
|
+
'accept-encoding': 'identity'
|
|
1144
|
+
}
|
|
1145
|
+
})
|
|
1146
|
+
t.notOk(response.headers['content-encoding'])
|
|
1147
|
+
t.equal(response.payload, JSON.stringify(json))
|
|
1148
|
+
})
|
|
1149
|
+
|
|
1150
|
+
t.test('when `Content-Type` reply header is a mime type with undefined compressible values', async (t) => {
|
|
1151
|
+
t.plan(3)
|
|
1152
|
+
|
|
1153
|
+
const fastify = Fastify()
|
|
1154
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
1155
|
+
|
|
1156
|
+
fastify.get('/', (request, reply) => {
|
|
1157
|
+
reply
|
|
1158
|
+
.type('image/webp')
|
|
1159
|
+
.send('hello')
|
|
1160
|
+
})
|
|
1161
|
+
|
|
1162
|
+
const response = await fastify.inject({
|
|
1163
|
+
url: '/',
|
|
1164
|
+
method: 'GET',
|
|
1165
|
+
headers: {
|
|
1166
|
+
'accept-encoding': 'gzip, deflate, br'
|
|
1167
|
+
}
|
|
1168
|
+
})
|
|
1169
|
+
t.equal(response.statusCode, 200)
|
|
1170
|
+
t.notOk(response.headers['content-encoding'])
|
|
1171
|
+
t.equal(response.payload, 'hello')
|
|
1172
|
+
})
|
|
1173
|
+
|
|
1174
|
+
t.test('when `Content-Type` reply header value is `text/event-stream`', async (t) => {
|
|
1175
|
+
t.plan(3)
|
|
1176
|
+
|
|
1177
|
+
const fastify = Fastify()
|
|
1178
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
1179
|
+
|
|
1180
|
+
fastify.get('/', (_req, reply) => {
|
|
1181
|
+
const stream = new PassThrough()
|
|
1182
|
+
|
|
1183
|
+
reply
|
|
1184
|
+
.header('Content-Type', 'text/event-stream')
|
|
1185
|
+
.send(stream)
|
|
1186
|
+
|
|
1187
|
+
stream.write('event: open\n\n')
|
|
1188
|
+
stream.write('event: change\ndata: schema\n\n')
|
|
1189
|
+
stream.end()
|
|
1190
|
+
})
|
|
1191
|
+
|
|
1192
|
+
const response = await fastify.inject({
|
|
1193
|
+
url: '/',
|
|
1194
|
+
method: 'GET'
|
|
1195
|
+
})
|
|
1196
|
+
t.equal(response.statusCode, 200)
|
|
1197
|
+
t.notOk(response.headers['content-encoding'])
|
|
1198
|
+
t.same(response.payload, 'event: open\n\nevent: change\ndata: schema\n\n')
|
|
1199
|
+
})
|
|
1200
|
+
|
|
1201
|
+
t.test('when `Content-Type` reply header value is an invalid type', async (t) => {
|
|
1202
|
+
t.plan(3)
|
|
1203
|
+
|
|
1204
|
+
const fastify = Fastify()
|
|
1205
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
1206
|
+
|
|
1207
|
+
fastify.get('/', (request, reply) => {
|
|
1208
|
+
reply
|
|
1209
|
+
.header('Content-Type', 'something/invalid')
|
|
1210
|
+
.send('a message')
|
|
1211
|
+
})
|
|
1212
|
+
|
|
1213
|
+
const response = await fastify.inject({
|
|
1214
|
+
url: '/',
|
|
1215
|
+
method: 'GET',
|
|
1216
|
+
headers: {
|
|
1217
|
+
'accept-encoding': 'deflate'
|
|
1218
|
+
}
|
|
1219
|
+
})
|
|
1220
|
+
t.equal(response.statusCode, 200)
|
|
1221
|
+
t.notOk(response.headers['content-encoding'])
|
|
1222
|
+
t.equal(response.payload, 'a message')
|
|
1223
|
+
})
|
|
1224
|
+
|
|
1225
|
+
t.test('when `Accept-Encoding` request header is missing', async (t) => {
|
|
1226
|
+
t.plan(3)
|
|
1227
|
+
|
|
1228
|
+
const fastify = Fastify()
|
|
1229
|
+
await fastify.register(compressPlugin, { global: true })
|
|
1230
|
+
|
|
1231
|
+
fastify.get('/', (request, reply) => {
|
|
1232
|
+
reply
|
|
1233
|
+
.header('Content-Type', 'text/plain')
|
|
1234
|
+
.send(createReadStream('./package.json'))
|
|
1235
|
+
})
|
|
1236
|
+
|
|
1237
|
+
const response = await fastify.inject({
|
|
1238
|
+
url: '/',
|
|
1239
|
+
method: 'GET'
|
|
1240
|
+
})
|
|
1241
|
+
t.equal(response.headers.vary, 'accept-encoding')
|
|
1242
|
+
t.equal(response.statusCode, 200)
|
|
1243
|
+
t.notOk(response.headers['content-encoding'])
|
|
1244
|
+
})
|
|
1245
|
+
|
|
1246
|
+
t.test('when `Accept-Encoding` request header is set to `identity`', async (t) => {
|
|
1247
|
+
t.plan(2)
|
|
1248
|
+
|
|
1249
|
+
const fastify = Fastify()
|
|
1250
|
+
await fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
1251
|
+
|
|
1252
|
+
fastify.get('/', (request, reply) => {
|
|
1253
|
+
reply.send({ hello: 'world' })
|
|
1254
|
+
})
|
|
1255
|
+
|
|
1256
|
+
const response = await fastify.inject({
|
|
1257
|
+
url: '/',
|
|
1258
|
+
method: 'GET',
|
|
1259
|
+
headers: {
|
|
1260
|
+
'accept-encoding': 'identity'
|
|
1261
|
+
}
|
|
1262
|
+
})
|
|
1263
|
+
const payload = JSON.parse(response.payload)
|
|
1264
|
+
t.notOk(response.headers['content-encoding'])
|
|
1265
|
+
t.same({ hello: 'world' }, payload)
|
|
1266
|
+
})
|
|
1267
|
+
|
|
1268
|
+
t.test('when `Accept-Encoding` request header value is not supported', async (t) => {
|
|
1269
|
+
t.plan(2)
|
|
1270
|
+
|
|
1271
|
+
const fastify = Fastify()
|
|
1272
|
+
await fastify.register(compressPlugin, { global: true })
|
|
1273
|
+
|
|
1274
|
+
fastify.get('/', (request, reply) => {
|
|
1275
|
+
reply
|
|
1276
|
+
.header('Content-Type', 'text/plain')
|
|
1277
|
+
.send('something')
|
|
1278
|
+
})
|
|
1279
|
+
|
|
1280
|
+
const response = await fastify.inject({
|
|
1281
|
+
url: '/',
|
|
1282
|
+
method: 'GET',
|
|
1283
|
+
headers: {
|
|
1284
|
+
'accept-encoding': 'invalid'
|
|
1285
|
+
}
|
|
1286
|
+
})
|
|
1287
|
+
t.equal(response.statusCode, 200)
|
|
1288
|
+
t.equal(response.payload, 'something')
|
|
1289
|
+
})
|
|
1290
|
+
|
|
1291
|
+
t.test('when `Accept-Encoding` request header value is not supported (with quality value)', async (t) => {
|
|
1292
|
+
t.plan(3)
|
|
1293
|
+
|
|
1294
|
+
const fastify = Fastify()
|
|
1295
|
+
await fastify.register(compressPlugin, { global: true })
|
|
1296
|
+
|
|
1297
|
+
fastify.get('/', (request, reply) => {
|
|
1298
|
+
reply
|
|
1299
|
+
.header('Content-Type', 'text/plain')
|
|
1300
|
+
.send(createReadStream('./package.json'))
|
|
1301
|
+
})
|
|
1302
|
+
|
|
1303
|
+
const response = await fastify.inject({
|
|
1304
|
+
url: '/',
|
|
1305
|
+
method: 'GET',
|
|
1306
|
+
headers: {
|
|
1307
|
+
'accept-encoding': 'lzma;q=1.0'
|
|
1308
|
+
}
|
|
1309
|
+
})
|
|
1310
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
1311
|
+
t.equal(response.headers.vary, 'accept-encoding')
|
|
1312
|
+
t.equal(response.statusCode, 200)
|
|
1313
|
+
t.equal(response.payload, file)
|
|
1314
|
+
})
|
|
1315
|
+
|
|
1316
|
+
t.test('when `Accept-Encoding` request header is set to `identity and `inflateIfDeflated` is `true``', async (t) => {
|
|
1317
|
+
t.plan(2)
|
|
1318
|
+
|
|
1319
|
+
const fastify = Fastify()
|
|
1320
|
+
await fastify.register(compressPlugin, { global: true, inflateIfDeflated: true, threshold: 0 })
|
|
1321
|
+
|
|
1322
|
+
fastify.get('/', (request, reply) => {
|
|
1323
|
+
reply.send({ hello: 'world' })
|
|
1324
|
+
})
|
|
1325
|
+
|
|
1326
|
+
const response = await fastify.inject({
|
|
1327
|
+
url: '/',
|
|
1328
|
+
method: 'GET',
|
|
1329
|
+
headers: {
|
|
1330
|
+
'accept-encoding': 'identity'
|
|
1331
|
+
}
|
|
1332
|
+
})
|
|
1333
|
+
const payload = JSON.parse(response.payload)
|
|
1334
|
+
t.notOk(response.headers['content-encoding'])
|
|
1335
|
+
t.same({ hello: 'world' }, payload)
|
|
1336
|
+
})
|
|
1337
|
+
})
|
|
1338
|
+
})
|
|
1339
|
+
|
|
1340
|
+
test('It should not double-compress :', async (t) => {
|
|
1341
|
+
t.test('when using `reply.compress()` to send an already deflated Stream', async (t) => {
|
|
1342
|
+
t.plan(2)
|
|
1343
|
+
|
|
1344
|
+
const fastify = Fastify()
|
|
1345
|
+
await fastify.register(compressPlugin, { global: true })
|
|
1346
|
+
|
|
1347
|
+
fastify.get('/', (request, reply) => {
|
|
1348
|
+
reply
|
|
1349
|
+
.type('text/plain')
|
|
1350
|
+
.compress(
|
|
1351
|
+
createReadStream('./package.json').pipe(zlib.createDeflate())
|
|
1352
|
+
)
|
|
1353
|
+
})
|
|
1354
|
+
|
|
1355
|
+
const response = await fastify.inject({
|
|
1356
|
+
url: '/',
|
|
1357
|
+
method: 'GET',
|
|
1358
|
+
headers: {
|
|
1359
|
+
'accept-encoding': 'deflate'
|
|
1360
|
+
}
|
|
1361
|
+
})
|
|
1362
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
1363
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
1364
|
+
t.equal(response.headers['content-encoding'], 'deflate')
|
|
1365
|
+
t.equal(payload.toString('utf-8'), file)
|
|
1366
|
+
})
|
|
1367
|
+
|
|
1368
|
+
t.test('when using `reply.compress()` to send an already gzipped Stream', async (t) => {
|
|
1369
|
+
t.plan(2)
|
|
1370
|
+
|
|
1371
|
+
const fastify = Fastify()
|
|
1372
|
+
await fastify.register(compressPlugin, { global: true })
|
|
1373
|
+
|
|
1374
|
+
fastify.get('/', (request, reply) => {
|
|
1375
|
+
reply
|
|
1376
|
+
.type('text/plain')
|
|
1377
|
+
.compress(
|
|
1378
|
+
createReadStream('./package.json').pipe(zlib.createGzip())
|
|
1379
|
+
)
|
|
1380
|
+
})
|
|
1381
|
+
|
|
1382
|
+
const response = await fastify.inject({
|
|
1383
|
+
url: '/',
|
|
1384
|
+
method: 'GET',
|
|
1385
|
+
headers: {
|
|
1386
|
+
'accept-encoding': 'gzip'
|
|
1387
|
+
}
|
|
1388
|
+
})
|
|
1389
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
1390
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
1391
|
+
t.equal(response.headers['content-encoding'], 'gzip')
|
|
1392
|
+
t.equal(payload.toString('utf-8'), file)
|
|
1393
|
+
})
|
|
1394
|
+
|
|
1395
|
+
t.test('when using `onSend` hook to send an already brotli compressed Stream', async (t) => {
|
|
1396
|
+
t.plan(3)
|
|
1397
|
+
|
|
1398
|
+
const fastify = Fastify()
|
|
1399
|
+
await fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
1400
|
+
|
|
1401
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
1402
|
+
fastify.get('/', (request, reply) => {
|
|
1403
|
+
const payload = zlib.brotliCompressSync(file)
|
|
1404
|
+
|
|
1405
|
+
reply
|
|
1406
|
+
.type('application/json')
|
|
1407
|
+
.header('content-encoding', 'br')
|
|
1408
|
+
.header('content-length', payload.length)
|
|
1409
|
+
.send(payload)
|
|
1410
|
+
})
|
|
1411
|
+
|
|
1412
|
+
const response = await fastify.inject({
|
|
1413
|
+
url: '/',
|
|
1414
|
+
method: 'GET',
|
|
1415
|
+
headers: {
|
|
1416
|
+
'accept-encoding': 'br,gzip,deflate'
|
|
1417
|
+
}
|
|
1418
|
+
})
|
|
1419
|
+
const payload = zlib.brotliDecompressSync(response.rawPayload)
|
|
1420
|
+
t.equal(response.headers['content-encoding'], 'br')
|
|
1421
|
+
t.equal(response.headers['content-length'], response.rawPayload.length)
|
|
1422
|
+
t.equal(payload.toString('utf-8'), file)
|
|
1423
|
+
})
|
|
1424
|
+
|
|
1425
|
+
t.test('when using `onSend` hook to send an already deflated Stream', async (t) => {
|
|
1426
|
+
t.plan(3)
|
|
1427
|
+
|
|
1428
|
+
const fastify = Fastify()
|
|
1429
|
+
await fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
1430
|
+
|
|
1431
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
1432
|
+
fastify.get('/', (request, reply) => {
|
|
1433
|
+
const payload = zlib.deflateSync(file)
|
|
1434
|
+
|
|
1435
|
+
reply
|
|
1436
|
+
.type('application/json')
|
|
1437
|
+
.header('content-encoding', 'deflate')
|
|
1438
|
+
.header('content-length', payload.length)
|
|
1439
|
+
.send(payload)
|
|
1440
|
+
})
|
|
1441
|
+
|
|
1442
|
+
const response = await fastify.inject({
|
|
1443
|
+
url: '/',
|
|
1444
|
+
method: 'GET',
|
|
1445
|
+
headers: {
|
|
1446
|
+
'accept-encoding': 'br,gzip,deflate'
|
|
1447
|
+
}
|
|
1448
|
+
})
|
|
1449
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
1450
|
+
t.equal(response.headers['content-encoding'], 'deflate')
|
|
1451
|
+
t.equal(response.headers['content-length'], response.rawPayload.length)
|
|
1452
|
+
t.equal(payload.toString('utf-8'), file)
|
|
1453
|
+
})
|
|
1454
|
+
|
|
1455
|
+
t.test('when using `onSend` hook to send an already gzipped Stream', async (t) => {
|
|
1456
|
+
t.plan(3)
|
|
1457
|
+
|
|
1458
|
+
const fastify = Fastify()
|
|
1459
|
+
await fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
1460
|
+
|
|
1461
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
1462
|
+
fastify.get('/', (request, reply) => {
|
|
1463
|
+
const payload = zlib.gzipSync(file)
|
|
1464
|
+
|
|
1465
|
+
reply
|
|
1466
|
+
.type('application/json')
|
|
1467
|
+
.header('content-encoding', 'gzip')
|
|
1468
|
+
.header('content-length', payload.length)
|
|
1469
|
+
.send(payload)
|
|
1470
|
+
})
|
|
1471
|
+
|
|
1472
|
+
const response = await fastify.inject({
|
|
1473
|
+
url: '/',
|
|
1474
|
+
method: 'GET',
|
|
1475
|
+
headers: {
|
|
1476
|
+
'accept-encoding': 'gzip'
|
|
1477
|
+
}
|
|
1478
|
+
})
|
|
1479
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
1480
|
+
t.equal(response.headers['content-encoding'], 'gzip')
|
|
1481
|
+
t.equal(response.headers['content-length'], response.rawPayload.length)
|
|
1482
|
+
t.equal(payload.toString('utf-8'), file)
|
|
1483
|
+
})
|
|
1484
|
+
})
|
|
1485
|
+
|
|
1486
|
+
test('It should not compress Stream data and add a `Content-Encoding` reply header :', async (t) => {
|
|
1487
|
+
t.test('Using `onSend` hook if `Accept-Encoding` request header value is `identity`', async (t) => {
|
|
1488
|
+
t.test('when `inflateIfDeflated` is `true` and `encodings` is not set', async (t) => {
|
|
1489
|
+
t.plan(3)
|
|
1490
|
+
|
|
1491
|
+
const fastify = Fastify()
|
|
1492
|
+
await fastify.register(compressPlugin, { global: true, inflateIfDeflated: true })
|
|
1493
|
+
|
|
1494
|
+
fastify.get('/', (request, reply) => {
|
|
1495
|
+
reply
|
|
1496
|
+
.header('Content-Type', 'application/octet-stream')
|
|
1497
|
+
.send(createReadStream('./package.json'))
|
|
1498
|
+
})
|
|
1499
|
+
|
|
1500
|
+
const response = await fastify.inject({
|
|
1501
|
+
url: '/',
|
|
1502
|
+
method: 'GET',
|
|
1503
|
+
headers: {
|
|
1504
|
+
'accept-encoding': 'identity'
|
|
1505
|
+
}
|
|
1506
|
+
})
|
|
1507
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
1508
|
+
t.equal(response.statusCode, 200)
|
|
1509
|
+
t.equal(response.headers['content-encoding'], 'identity')
|
|
1510
|
+
t.equal(file, response.payload)
|
|
1511
|
+
})
|
|
1512
|
+
|
|
1513
|
+
t.test('when `inflateIfDeflated` is `true` and `encodings` is set', async (t) => {
|
|
1514
|
+
t.plan(3)
|
|
1515
|
+
|
|
1516
|
+
const fastify = Fastify()
|
|
1517
|
+
await fastify.register(compressPlugin, {
|
|
1518
|
+
global: true,
|
|
1519
|
+
inflateIfDeflated: true,
|
|
1520
|
+
encodings: ['deflate', 'gzip']
|
|
1521
|
+
})
|
|
1522
|
+
|
|
1523
|
+
fastify.get('/', (request, reply) => {
|
|
1524
|
+
reply.send(createReadStream('./package.json'))
|
|
1525
|
+
})
|
|
1526
|
+
|
|
1527
|
+
const response = await fastify.inject({
|
|
1528
|
+
url: '/',
|
|
1529
|
+
method: 'GET',
|
|
1530
|
+
headers: {
|
|
1531
|
+
accept: 'application/json',
|
|
1532
|
+
'accept-encoding': 'identity'
|
|
1533
|
+
}
|
|
1534
|
+
})
|
|
1535
|
+
const file = readFileSync('./package.json', 'utf-8')
|
|
1536
|
+
t.equal(response.statusCode, 200)
|
|
1537
|
+
t.equal(response.headers['content-encoding'], 'identity')
|
|
1538
|
+
t.same(response.payload, file)
|
|
1539
|
+
})
|
|
1540
|
+
})
|
|
1541
|
+
|
|
1542
|
+
t.test('Using `reply.compress()` if `Accept-Encoding` request header value is `identity`', async (t) => {
|
|
1543
|
+
t.test('when `inflateIfDeflated` is `true` and `encodings` is not set', async (t) => {
|
|
1544
|
+
t.plan(3)
|
|
1545
|
+
|
|
1546
|
+
const fastify = Fastify()
|
|
1547
|
+
await fastify.register(compressPlugin, { global: true, inflateIfDeflated: true })
|
|
1548
|
+
|
|
1549
|
+
fastify.get('/', (request, reply) => {
|
|
1550
|
+
reply
|
|
1551
|
+
.type('application/octet-stream')
|
|
1552
|
+
.compress(createReadStream('./package.json'))
|
|
1553
|
+
})
|
|
1554
|
+
|
|
1555
|
+
const response = await fastify.inject({
|
|
1556
|
+
url: '/',
|
|
1557
|
+
method: 'GET',
|
|
1558
|
+
headers: {
|
|
1559
|
+
'accept-encoding': 'identity'
|
|
1560
|
+
}
|
|
1561
|
+
})
|
|
1562
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
1563
|
+
t.equal(response.statusCode, 200)
|
|
1564
|
+
t.equal(response.headers['content-encoding'], 'identity')
|
|
1565
|
+
t.equal(file, response.payload)
|
|
1566
|
+
})
|
|
1567
|
+
|
|
1568
|
+
t.test('when `inflateIfDeflated` is `true` and `encodings` is set', async (t) => {
|
|
1569
|
+
t.plan(3)
|
|
1570
|
+
|
|
1571
|
+
const fastify = Fastify()
|
|
1572
|
+
await fastify.register(compressPlugin, {
|
|
1573
|
+
global: true,
|
|
1574
|
+
inflateIfDeflated: true,
|
|
1575
|
+
encodings: ['deflate', 'gzip']
|
|
1576
|
+
})
|
|
1577
|
+
|
|
1578
|
+
fastify.get('/', (request, reply) => {
|
|
1579
|
+
reply.compress(createReadStream('./package.json'))
|
|
1580
|
+
})
|
|
1581
|
+
|
|
1582
|
+
const response = await fastify.inject({
|
|
1583
|
+
url: '/',
|
|
1584
|
+
method: 'GET',
|
|
1585
|
+
headers: {
|
|
1586
|
+
accept: 'application/json',
|
|
1587
|
+
'accept-encoding': 'identity'
|
|
1588
|
+
}
|
|
1589
|
+
})
|
|
1590
|
+
const file = readFileSync('./package.json', 'utf-8')
|
|
1591
|
+
t.equal(response.statusCode, 200)
|
|
1592
|
+
t.equal(response.headers['content-encoding'], 'identity')
|
|
1593
|
+
t.same(response.payload, file)
|
|
1594
|
+
})
|
|
1595
|
+
})
|
|
1596
|
+
})
|
|
1597
|
+
|
|
1598
|
+
test('It should return a serialized payload when `inflateIfDeflated` is `true` and `X-No-Compression` request header is `true`', async (t) => {
|
|
1599
|
+
t.plan(6)
|
|
1600
|
+
|
|
1601
|
+
const fastify = Fastify()
|
|
1602
|
+
await fastify.register(compressPlugin, {
|
|
1603
|
+
global: true,
|
|
1604
|
+
inflateIfDeflated: true,
|
|
1605
|
+
threshold: 0
|
|
1606
|
+
})
|
|
1607
|
+
|
|
1608
|
+
const json = { hello: 'world' }
|
|
1609
|
+
const compressedBufferPayload = zlib.brotliCompressSync(Buffer.from(json.toString()))
|
|
1610
|
+
|
|
1611
|
+
fastify.get('/one', (request, reply) => {
|
|
1612
|
+
reply.send(json)
|
|
1613
|
+
})
|
|
1614
|
+
|
|
1615
|
+
fastify.get('/two', (request, reply) => {
|
|
1616
|
+
reply.send(compressedBufferPayload)
|
|
1617
|
+
})
|
|
1618
|
+
|
|
1619
|
+
const one = await fastify.inject({
|
|
1620
|
+
url: '/one',
|
|
1621
|
+
method: 'GET',
|
|
1622
|
+
headers: {
|
|
1623
|
+
'x-no-compression': true
|
|
1624
|
+
}
|
|
1625
|
+
})
|
|
1626
|
+
t.equal(one.statusCode, 200)
|
|
1627
|
+
t.notOk(one.headers['content-encoding'])
|
|
1628
|
+
t.same(JSON.parse(one.payload), json)
|
|
1629
|
+
|
|
1630
|
+
const two = await fastify.inject({
|
|
1631
|
+
url: '/two',
|
|
1632
|
+
method: 'GET',
|
|
1633
|
+
headers: {
|
|
1634
|
+
'x-no-compression': true
|
|
1635
|
+
}
|
|
1636
|
+
})
|
|
1637
|
+
t.equal(two.statusCode, 200)
|
|
1638
|
+
t.notOk(two.headers['content-encoding'])
|
|
1639
|
+
t.equal(two.payload, compressedBufferPayload.toString())
|
|
1640
|
+
})
|
|
1641
|
+
|
|
1642
|
+
test('It should close the stream', (t) => {
|
|
1643
|
+
t.plan(4)
|
|
1644
|
+
|
|
1645
|
+
const fastify = Fastify()
|
|
1646
|
+
fastify.register(compressPlugin, { global: true })
|
|
1647
|
+
|
|
1648
|
+
fastify.get('/', (request, reply) => {
|
|
1649
|
+
const stream = createReadStream('./package.json')
|
|
1650
|
+
stream.on('close', () => t.ok('stream closed'))
|
|
1651
|
+
|
|
1652
|
+
reply
|
|
1653
|
+
.type('text/plain')
|
|
1654
|
+
.compress(stream)
|
|
1655
|
+
})
|
|
1656
|
+
|
|
1657
|
+
fastify.inject({
|
|
1658
|
+
url: '/',
|
|
1659
|
+
method: 'GET'
|
|
1660
|
+
}, (err, response) => {
|
|
1661
|
+
t.error(err)
|
|
1662
|
+
|
|
1663
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
1664
|
+
t.equal(response.statusCode, 200)
|
|
1665
|
+
t.equal(file, response.payload)
|
|
1666
|
+
})
|
|
1667
|
+
})
|
|
1668
|
+
|
|
1669
|
+
test('It should log an existing error with stream onEnd handler', async (t) => {
|
|
1670
|
+
t.plan(1)
|
|
1671
|
+
|
|
1672
|
+
let actual = null
|
|
1673
|
+
const logger = new Writable({
|
|
1674
|
+
write (chunk, encoding, callback) {
|
|
1675
|
+
actual = JSON.parse(chunk.toString())
|
|
1676
|
+
callback()
|
|
1677
|
+
}
|
|
1678
|
+
})
|
|
1679
|
+
|
|
1680
|
+
const fastify = Fastify({
|
|
1681
|
+
global: true,
|
|
1682
|
+
logger: {
|
|
1683
|
+
level: 'error',
|
|
1684
|
+
stream: logger
|
|
1685
|
+
}
|
|
1686
|
+
})
|
|
1687
|
+
await fastify.register(compressPlugin)
|
|
1688
|
+
|
|
1689
|
+
const expect = new Error('something wrong')
|
|
1690
|
+
|
|
1691
|
+
fastify.get('/', (request, reply) => {
|
|
1692
|
+
const stream = new Readable({
|
|
1693
|
+
read (size) {
|
|
1694
|
+
this.destroy(expect)
|
|
1695
|
+
}
|
|
1696
|
+
})
|
|
1697
|
+
|
|
1698
|
+
reply
|
|
1699
|
+
.type('text/plain')
|
|
1700
|
+
.compress(stream)
|
|
1701
|
+
})
|
|
1702
|
+
|
|
1703
|
+
await fastify.inject({
|
|
1704
|
+
url: '/',
|
|
1705
|
+
method: 'GET',
|
|
1706
|
+
headers: {
|
|
1707
|
+
'accept-encoding': 'gzip'
|
|
1708
|
+
}
|
|
1709
|
+
})
|
|
1710
|
+
t.equal(actual.msg, expect.message)
|
|
1711
|
+
})
|
|
1712
|
+
|
|
1713
|
+
test('It should support stream1 :', async (t) => {
|
|
1714
|
+
t.test('when using `reply.compress()`', async (t) => {
|
|
1715
|
+
t.plan(2)
|
|
1716
|
+
|
|
1717
|
+
const fastify = Fastify()
|
|
1718
|
+
await fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
1719
|
+
|
|
1720
|
+
fastify.get('/', (request, reply) => {
|
|
1721
|
+
const stream = JSONStream.stringify()
|
|
1722
|
+
|
|
1723
|
+
reply
|
|
1724
|
+
.type('text/plain')
|
|
1725
|
+
.compress(stream)
|
|
1726
|
+
|
|
1727
|
+
stream.write({ hello: 'world' })
|
|
1728
|
+
stream.end({ a: 42 })
|
|
1729
|
+
})
|
|
1730
|
+
|
|
1731
|
+
const response = await fastify.inject({
|
|
1732
|
+
url: '/',
|
|
1733
|
+
method: 'GET',
|
|
1734
|
+
headers: {
|
|
1735
|
+
'accept-encoding': 'gzip'
|
|
1736
|
+
}
|
|
1737
|
+
})
|
|
1738
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
1739
|
+
t.equal(response.headers['content-encoding'], 'gzip')
|
|
1740
|
+
t.same(JSON.parse(payload.toString()), [{ hello: 'world' }, { a: 42 }])
|
|
1741
|
+
})
|
|
1742
|
+
|
|
1743
|
+
t.test('when using `onSend` hook', async (t) => {
|
|
1744
|
+
t.plan(2)
|
|
1745
|
+
|
|
1746
|
+
const fastify = Fastify()
|
|
1747
|
+
await fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
1748
|
+
|
|
1749
|
+
fastify.get('/', (request, reply) => {
|
|
1750
|
+
const stream = JSONStream.stringify()
|
|
1751
|
+
|
|
1752
|
+
reply
|
|
1753
|
+
.type('text/plain')
|
|
1754
|
+
.send(stream)
|
|
1755
|
+
|
|
1756
|
+
stream.write({ hello: 'world' })
|
|
1757
|
+
stream.end({ a: 42 })
|
|
1758
|
+
})
|
|
1759
|
+
|
|
1760
|
+
const response = await fastify.inject({
|
|
1761
|
+
url: '/',
|
|
1762
|
+
method: 'GET',
|
|
1763
|
+
headers: {
|
|
1764
|
+
'accept-encoding': 'gzip'
|
|
1765
|
+
}
|
|
1766
|
+
})
|
|
1767
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
1768
|
+
t.equal(response.headers['content-encoding'], 'gzip')
|
|
1769
|
+
t.same(JSON.parse(payload.toString()), [{ hello: 'world' }, { a: 42 }])
|
|
1770
|
+
})
|
|
1771
|
+
})
|
|
1772
|
+
|
|
1773
|
+
test('It should remove `Content-Length` header :', async (t) => {
|
|
1774
|
+
t.test('using `reply.compress()`', async (t) => {
|
|
1775
|
+
t.plan(3)
|
|
1776
|
+
|
|
1777
|
+
const fastify = Fastify()
|
|
1778
|
+
await fastify.register(compressPlugin, { global: true })
|
|
1779
|
+
|
|
1780
|
+
fastify.get('/', (request, reply) => {
|
|
1781
|
+
readFile('./package.json', 'utf8', (err, data) => {
|
|
1782
|
+
if (err) {
|
|
1783
|
+
return reply.send(err)
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
reply
|
|
1787
|
+
.type('text/plain')
|
|
1788
|
+
.header('content-length', '' + data.length)
|
|
1789
|
+
.compress(data)
|
|
1790
|
+
})
|
|
1791
|
+
})
|
|
1792
|
+
|
|
1793
|
+
const response = await fastify.inject({
|
|
1794
|
+
url: '/',
|
|
1795
|
+
method: 'GET',
|
|
1796
|
+
headers: {
|
|
1797
|
+
'accept-encoding': 'deflate'
|
|
1798
|
+
}
|
|
1799
|
+
})
|
|
1800
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
1801
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
1802
|
+
t.equal(response.headers['content-encoding'], 'deflate')
|
|
1803
|
+
t.notOk(response.headers['content-length'], 'no content length')
|
|
1804
|
+
t.equal(payload.toString('utf-8'), file)
|
|
1805
|
+
})
|
|
1806
|
+
|
|
1807
|
+
t.test('using `reply.compress()` on a Stream when `inflateIfDeflated` is `true` and `X-No-Compression` request header is `true`', async (t) => {
|
|
1808
|
+
t.plan(3)
|
|
1809
|
+
|
|
1810
|
+
const fastify = Fastify()
|
|
1811
|
+
await fastify.register(compressPlugin, { global: true, inflateIfDeflated: true })
|
|
1812
|
+
|
|
1813
|
+
fastify.get('/', (request, reply) => {
|
|
1814
|
+
reply
|
|
1815
|
+
.type('application/octet-stream')
|
|
1816
|
+
.compress(createReadStream('./package.json'))
|
|
1817
|
+
})
|
|
1818
|
+
|
|
1819
|
+
const response = await fastify.inject({
|
|
1820
|
+
url: '/',
|
|
1821
|
+
method: 'GET',
|
|
1822
|
+
headers: {
|
|
1823
|
+
'x-no-compression': true
|
|
1824
|
+
}
|
|
1825
|
+
})
|
|
1826
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
1827
|
+
t.equal(response.statusCode, 200)
|
|
1828
|
+
t.notOk(response.headers['content-length'], 'no content length')
|
|
1829
|
+
t.equal(file, response.payload)
|
|
1830
|
+
})
|
|
1831
|
+
|
|
1832
|
+
t.test('using `onSend` hook', async (t) => {
|
|
1833
|
+
t.plan(3)
|
|
1834
|
+
|
|
1835
|
+
const fastify = Fastify()
|
|
1836
|
+
await fastify.register(compressPlugin, { global: true })
|
|
1837
|
+
|
|
1838
|
+
fastify.get('/', (request, reply) => {
|
|
1839
|
+
readFile('./package.json', 'utf8', (err, data) => {
|
|
1840
|
+
if (err) {
|
|
1841
|
+
return reply.send(err)
|
|
1842
|
+
}
|
|
1843
|
+
|
|
1844
|
+
reply
|
|
1845
|
+
.type('text/plain')
|
|
1846
|
+
.header('content-length', '' + data.length)
|
|
1847
|
+
.send(data)
|
|
1848
|
+
})
|
|
1849
|
+
})
|
|
1850
|
+
|
|
1851
|
+
const response = await fastify.inject({
|
|
1852
|
+
url: '/',
|
|
1853
|
+
method: 'GET',
|
|
1854
|
+
headers: {
|
|
1855
|
+
'accept-encoding': 'deflate'
|
|
1856
|
+
}
|
|
1857
|
+
})
|
|
1858
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
1859
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
1860
|
+
t.equal(response.headers['content-encoding'], 'deflate')
|
|
1861
|
+
t.notOk(response.headers['content-length'], 'no content length')
|
|
1862
|
+
t.equal(payload.toString('utf-8'), file)
|
|
1863
|
+
})
|
|
1864
|
+
|
|
1865
|
+
t.test('using `onSend` hook on a Stream When `inflateIfDeflated` is `true` and `X-No-Compression` request header is `true`', async (t) => {
|
|
1866
|
+
t.plan(3)
|
|
1867
|
+
|
|
1868
|
+
const fastify = Fastify()
|
|
1869
|
+
await fastify.register(compressPlugin, { global: true, inflateIfDeflated: true })
|
|
1870
|
+
|
|
1871
|
+
fastify.get('/', (request, reply) => {
|
|
1872
|
+
reply
|
|
1873
|
+
.header('Content-Type', 'application/octet-stream')
|
|
1874
|
+
.send(createReadStream('./package.json'))
|
|
1875
|
+
})
|
|
1876
|
+
|
|
1877
|
+
const response = await fastify.inject({
|
|
1878
|
+
url: '/',
|
|
1879
|
+
method: 'GET',
|
|
1880
|
+
headers: {
|
|
1881
|
+
'x-no-compression': true
|
|
1882
|
+
}
|
|
1883
|
+
})
|
|
1884
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
1885
|
+
t.equal(response.statusCode, 200)
|
|
1886
|
+
t.notOk(response.headers['content-length'], 'no content length')
|
|
1887
|
+
t.equal(file, response.payload)
|
|
1888
|
+
})
|
|
1889
|
+
})
|
|
1890
|
+
|
|
1891
|
+
test('When `removeContentLengthHeader` is `false`, it should not remove `Content-Length` header :', async (t) => {
|
|
1892
|
+
t.test('using `reply.compress()`', async (t) => {
|
|
1893
|
+
t.plan(3)
|
|
1894
|
+
|
|
1895
|
+
const fastify = Fastify()
|
|
1896
|
+
await fastify.register(compressPlugin, { global: true, removeContentLengthHeader: false })
|
|
1897
|
+
|
|
1898
|
+
fastify.get('/', (request, reply) => {
|
|
1899
|
+
readFile('./package.json', 'utf8', (err, data) => {
|
|
1900
|
+
if (err) {
|
|
1901
|
+
return reply.send(err)
|
|
1902
|
+
}
|
|
1903
|
+
|
|
1904
|
+
reply
|
|
1905
|
+
.type('text/plain')
|
|
1906
|
+
.header('content-length', '' + data.length)
|
|
1907
|
+
.compress(data)
|
|
1908
|
+
})
|
|
1909
|
+
})
|
|
1910
|
+
|
|
1911
|
+
const response = await fastify.inject({
|
|
1912
|
+
url: '/',
|
|
1913
|
+
method: 'GET',
|
|
1914
|
+
headers: {
|
|
1915
|
+
'accept-encoding': 'deflate'
|
|
1916
|
+
}
|
|
1917
|
+
})
|
|
1918
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
1919
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
1920
|
+
t.equal(response.headers['content-encoding'], 'deflate')
|
|
1921
|
+
t.equal(response.headers['content-length'], payload.length.toString())
|
|
1922
|
+
t.equal(payload.toString('utf-8'), file)
|
|
1923
|
+
})
|
|
1924
|
+
|
|
1925
|
+
t.test('using `onSend` hook', async (t) => {
|
|
1926
|
+
t.plan(3)
|
|
1927
|
+
|
|
1928
|
+
const fastify = Fastify()
|
|
1929
|
+
await fastify.register(compressPlugin, { global: true, removeContentLengthHeader: false })
|
|
1930
|
+
|
|
1931
|
+
fastify.get('/', (request, reply) => {
|
|
1932
|
+
readFile('./package.json', 'utf8', (err, data) => {
|
|
1933
|
+
if (err) {
|
|
1934
|
+
return reply.send(err)
|
|
1935
|
+
}
|
|
1936
|
+
|
|
1937
|
+
reply
|
|
1938
|
+
.type('text/plain')
|
|
1939
|
+
.header('content-length', '' + data.length)
|
|
1940
|
+
.send(data)
|
|
1941
|
+
})
|
|
1942
|
+
})
|
|
1943
|
+
|
|
1944
|
+
const response = await fastify.inject({
|
|
1945
|
+
url: '/',
|
|
1946
|
+
method: 'GET',
|
|
1947
|
+
headers: {
|
|
1948
|
+
'accept-encoding': 'deflate'
|
|
1949
|
+
}
|
|
1950
|
+
})
|
|
1951
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
1952
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
1953
|
+
t.equal(response.headers['content-encoding'], 'deflate')
|
|
1954
|
+
t.equal(response.headers['content-length'], payload.length.toString())
|
|
1955
|
+
t.equal(payload.toString('utf-8'), file)
|
|
1956
|
+
})
|
|
1957
|
+
})
|
|
1958
|
+
|
|
1959
|
+
test('It should add hooks correctly: ', async (t) => {
|
|
1960
|
+
t.test('`onRequest` hooks', async (t) => {
|
|
1961
|
+
t.plan(14)
|
|
1962
|
+
|
|
1963
|
+
const fastify = Fastify()
|
|
1964
|
+
|
|
1965
|
+
fastify.addHook('onRequest', async (request, reply) => {
|
|
1966
|
+
reply.header('x-fastify-global-test', 'ok')
|
|
1967
|
+
})
|
|
1968
|
+
|
|
1969
|
+
await fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
1970
|
+
|
|
1971
|
+
fastify.get('/one', {
|
|
1972
|
+
onRequest: [
|
|
1973
|
+
async (request, reply) => { reply.header('x-fastify-test-one', 'ok') }
|
|
1974
|
+
]
|
|
1975
|
+
}, (request, reply) => {
|
|
1976
|
+
reply
|
|
1977
|
+
.type('text/plain')
|
|
1978
|
+
.compress(createReadStream('./package.json'))
|
|
1979
|
+
})
|
|
1980
|
+
|
|
1981
|
+
fastify.get('/two', {
|
|
1982
|
+
onRequest: async (request, reply) => { reply.header('x-fastify-test-two', 'ok') }
|
|
1983
|
+
}, (request, reply) => {
|
|
1984
|
+
reply
|
|
1985
|
+
.type('text/plain')
|
|
1986
|
+
.compress(createReadStream('./package.json'))
|
|
1987
|
+
})
|
|
1988
|
+
|
|
1989
|
+
fastify.get('/three', { onRequest: null }, (request, reply) => {
|
|
1990
|
+
reply
|
|
1991
|
+
.type('text/plain')
|
|
1992
|
+
.compress(createReadStream('./package.json'))
|
|
1993
|
+
})
|
|
1994
|
+
|
|
1995
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
1996
|
+
await fastify.inject({
|
|
1997
|
+
url: '/one',
|
|
1998
|
+
method: 'GET',
|
|
1999
|
+
headers: {
|
|
2000
|
+
'accept-encoding': 'deflate'
|
|
2001
|
+
}
|
|
2002
|
+
}).then((response) => {
|
|
2003
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
2004
|
+
t.equal(response.statusCode, 200)
|
|
2005
|
+
t.notOk(response.headers['content-length'], 'no content length')
|
|
2006
|
+
t.equal(response.headers['x-fastify-global-test'], 'ok')
|
|
2007
|
+
t.equal(response.headers['x-fastify-test-one'], 'ok')
|
|
2008
|
+
t.equal(payload.toString('utf-8'), file)
|
|
2009
|
+
}).catch((err) => {
|
|
2010
|
+
t.error(err)
|
|
2011
|
+
})
|
|
2012
|
+
|
|
2013
|
+
await fastify.inject({
|
|
2014
|
+
url: '/two',
|
|
2015
|
+
method: 'GET',
|
|
2016
|
+
headers: {
|
|
2017
|
+
'accept-encoding': 'deflate'
|
|
2018
|
+
}
|
|
2019
|
+
}).then((response) => {
|
|
2020
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
2021
|
+
t.equal(response.statusCode, 200)
|
|
2022
|
+
t.notOk(response.headers['content-length'], 'no content length')
|
|
2023
|
+
t.equal(response.headers['x-fastify-global-test'], 'ok')
|
|
2024
|
+
t.equal(response.headers['x-fastify-test-two'], 'ok')
|
|
2025
|
+
t.equal(payload.toString('utf-8'), file)
|
|
2026
|
+
}).catch((err) => {
|
|
2027
|
+
t.error(err)
|
|
2028
|
+
})
|
|
2029
|
+
|
|
2030
|
+
await fastify.inject({
|
|
2031
|
+
url: '/three',
|
|
2032
|
+
method: 'GET',
|
|
2033
|
+
headers: {
|
|
2034
|
+
'accept-encoding': 'deflate'
|
|
2035
|
+
}
|
|
2036
|
+
}).then((response) => {
|
|
2037
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
2038
|
+
t.equal(response.statusCode, 200)
|
|
2039
|
+
t.notOk(response.headers['content-length'], 'no content length')
|
|
2040
|
+
t.equal(response.headers['x-fastify-global-test'], 'ok')
|
|
2041
|
+
t.equal(payload.toString('utf-8'), file)
|
|
2042
|
+
}).catch((err) => {
|
|
2043
|
+
t.error(err)
|
|
2044
|
+
})
|
|
2045
|
+
})
|
|
2046
|
+
|
|
2047
|
+
t.test('`onSend` hooks', async (t) => {
|
|
2048
|
+
t.plan(14)
|
|
2049
|
+
|
|
2050
|
+
const fastify = Fastify()
|
|
2051
|
+
|
|
2052
|
+
fastify.addHook('onSend', async (request, reply) => {
|
|
2053
|
+
reply.header('x-fastify-global-test', 'ok')
|
|
2054
|
+
})
|
|
2055
|
+
|
|
2056
|
+
await fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
2057
|
+
|
|
2058
|
+
fastify.get('/one', {
|
|
2059
|
+
onSend: [
|
|
2060
|
+
async (request, reply) => { reply.header('x-fastify-test-one', 'ok') }
|
|
2061
|
+
]
|
|
2062
|
+
}, (request, reply) => {
|
|
2063
|
+
reply
|
|
2064
|
+
.type('text/plain')
|
|
2065
|
+
.compress(createReadStream('./package.json'))
|
|
2066
|
+
})
|
|
2067
|
+
|
|
2068
|
+
fastify.get('/two', {
|
|
2069
|
+
onSend: async (request, reply) => { reply.header('x-fastify-test-two', 'ok') }
|
|
2070
|
+
}, (request, reply) => {
|
|
2071
|
+
reply
|
|
2072
|
+
.type('text/plain')
|
|
2073
|
+
.compress(createReadStream('./package.json'))
|
|
2074
|
+
})
|
|
2075
|
+
|
|
2076
|
+
fastify.get('/three', { onSend: null }, (request, reply) => {
|
|
2077
|
+
reply
|
|
2078
|
+
.type('text/plain')
|
|
2079
|
+
.compress(createReadStream('./package.json'))
|
|
2080
|
+
})
|
|
2081
|
+
|
|
2082
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
2083
|
+
await fastify.inject({
|
|
2084
|
+
url: '/one',
|
|
2085
|
+
method: 'GET',
|
|
2086
|
+
headers: {
|
|
2087
|
+
'accept-encoding': 'deflate'
|
|
2088
|
+
}
|
|
2089
|
+
}).then((response) => {
|
|
2090
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
2091
|
+
t.equal(response.statusCode, 200)
|
|
2092
|
+
t.notOk(response.headers['content-length'], 'no content length')
|
|
2093
|
+
t.equal(response.headers['x-fastify-global-test'], 'ok')
|
|
2094
|
+
t.equal(response.headers['x-fastify-test-one'], 'ok')
|
|
2095
|
+
t.equal(payload.toString('utf-8'), file)
|
|
2096
|
+
}).catch((err) => {
|
|
2097
|
+
t.error(err)
|
|
2098
|
+
})
|
|
2099
|
+
|
|
2100
|
+
await fastify.inject({
|
|
2101
|
+
url: '/two',
|
|
2102
|
+
method: 'GET',
|
|
2103
|
+
headers: {
|
|
2104
|
+
'accept-encoding': 'deflate'
|
|
2105
|
+
}
|
|
2106
|
+
}).then((response) => {
|
|
2107
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
2108
|
+
t.equal(response.statusCode, 200)
|
|
2109
|
+
t.notOk(response.headers['content-length'], 'no content length')
|
|
2110
|
+
t.equal(response.headers['x-fastify-global-test'], 'ok')
|
|
2111
|
+
t.equal(response.headers['x-fastify-test-two'], 'ok')
|
|
2112
|
+
t.equal(payload.toString('utf-8'), file)
|
|
2113
|
+
}).catch((err) => {
|
|
2114
|
+
t.error(err)
|
|
2115
|
+
})
|
|
2116
|
+
|
|
2117
|
+
await fastify.inject({
|
|
2118
|
+
url: '/three',
|
|
2119
|
+
method: 'GET',
|
|
2120
|
+
headers: {
|
|
2121
|
+
'accept-encoding': 'deflate'
|
|
2122
|
+
}
|
|
2123
|
+
}).then((response) => {
|
|
2124
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
2125
|
+
t.equal(response.statusCode, 200)
|
|
2126
|
+
t.notOk(response.headers['content-length'], 'no content length')
|
|
2127
|
+
t.equal(response.headers['x-fastify-global-test'], 'ok')
|
|
2128
|
+
t.equal(payload.toString('utf-8'), file)
|
|
2129
|
+
}).catch((err) => {
|
|
2130
|
+
t.error(err)
|
|
2131
|
+
})
|
|
2132
|
+
})
|
|
2133
|
+
|
|
2134
|
+
t.test('`preParsing` hooks', async (t) => {
|
|
2135
|
+
t.plan(14)
|
|
2136
|
+
|
|
2137
|
+
const fastify = Fastify()
|
|
2138
|
+
|
|
2139
|
+
fastify.addHook('preParsing', async (request, reply, payload) => {
|
|
2140
|
+
reply.header('x-fastify-global-test', 'ok')
|
|
2141
|
+
return payload
|
|
2142
|
+
})
|
|
2143
|
+
|
|
2144
|
+
await fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
2145
|
+
|
|
2146
|
+
fastify.get('/one', {
|
|
2147
|
+
preParsing: [
|
|
2148
|
+
async (request, reply, payload) => {
|
|
2149
|
+
reply.header('x-fastify-test-one', 'ok')
|
|
2150
|
+
return payload
|
|
2151
|
+
}
|
|
2152
|
+
]
|
|
2153
|
+
}, (request, reply) => {
|
|
2154
|
+
reply
|
|
2155
|
+
.type('text/plain')
|
|
2156
|
+
.compress(createReadStream('./package.json'))
|
|
2157
|
+
})
|
|
2158
|
+
|
|
2159
|
+
fastify.get('/two', {
|
|
2160
|
+
preParsing: async (request, reply, payload) => {
|
|
2161
|
+
reply.header('x-fastify-test-two', 'ok')
|
|
2162
|
+
return payload
|
|
2163
|
+
}
|
|
2164
|
+
}, (request, reply) => {
|
|
2165
|
+
reply
|
|
2166
|
+
.type('text/plain')
|
|
2167
|
+
.compress(createReadStream('./package.json'))
|
|
2168
|
+
})
|
|
2169
|
+
|
|
2170
|
+
fastify.get('/three', { preParsing: null }, (request, reply) => {
|
|
2171
|
+
reply
|
|
2172
|
+
.type('text/plain')
|
|
2173
|
+
.compress(createReadStream('./package.json'))
|
|
2174
|
+
})
|
|
2175
|
+
|
|
2176
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
2177
|
+
await fastify.inject({
|
|
2178
|
+
url: '/one',
|
|
2179
|
+
method: 'GET',
|
|
2180
|
+
headers: {
|
|
2181
|
+
'accept-encoding': 'deflate'
|
|
2182
|
+
}
|
|
2183
|
+
}).then((response) => {
|
|
2184
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
2185
|
+
t.equal(response.statusCode, 200)
|
|
2186
|
+
t.notOk(response.headers['content-length'], 'no content length')
|
|
2187
|
+
t.equal(response.headers['x-fastify-global-test'], 'ok')
|
|
2188
|
+
t.equal(response.headers['x-fastify-test-one'], 'ok')
|
|
2189
|
+
t.equal(payload.toString('utf-8'), file)
|
|
2190
|
+
}).catch((err) => {
|
|
2191
|
+
t.error(err)
|
|
2192
|
+
})
|
|
2193
|
+
|
|
2194
|
+
await fastify.inject({
|
|
2195
|
+
url: '/two',
|
|
2196
|
+
method: 'GET',
|
|
2197
|
+
headers: {
|
|
2198
|
+
'accept-encoding': 'deflate'
|
|
2199
|
+
}
|
|
2200
|
+
}).then((response) => {
|
|
2201
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
2202
|
+
t.equal(response.statusCode, 200)
|
|
2203
|
+
t.notOk(response.headers['content-length'], 'no content length')
|
|
2204
|
+
t.equal(response.headers['x-fastify-global-test'], 'ok')
|
|
2205
|
+
t.equal(response.headers['x-fastify-test-two'], 'ok')
|
|
2206
|
+
t.equal(payload.toString('utf-8'), file)
|
|
2207
|
+
}).catch((err) => {
|
|
2208
|
+
t.error(err)
|
|
2209
|
+
})
|
|
2210
|
+
|
|
2211
|
+
await fastify.inject({
|
|
2212
|
+
url: '/three',
|
|
2213
|
+
method: 'GET',
|
|
2214
|
+
headers: {
|
|
2215
|
+
'accept-encoding': 'deflate'
|
|
2216
|
+
}
|
|
2217
|
+
}).then((response) => {
|
|
2218
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
2219
|
+
t.equal(response.statusCode, 200)
|
|
2220
|
+
t.notOk(response.headers['content-length'], 'no content length')
|
|
2221
|
+
t.equal(response.headers['x-fastify-global-test'], 'ok')
|
|
2222
|
+
t.equal(payload.toString('utf-8'), file)
|
|
2223
|
+
}).catch((err) => {
|
|
2224
|
+
t.error(err)
|
|
2225
|
+
})
|
|
2226
|
+
})
|
|
2227
|
+
})
|
|
2228
|
+
|
|
2229
|
+
test('When `Accept-Encoding` request header values are not supported and `onUnsupportedEncoding` is defined :', async (t) => {
|
|
2230
|
+
t.test('it should call the defined `onUnsupportedEncoding()` method', async (t) => {
|
|
2231
|
+
t.plan(2)
|
|
2232
|
+
|
|
2233
|
+
const fastify = Fastify()
|
|
2234
|
+
await fastify.register(compressPlugin, {
|
|
2235
|
+
global: true,
|
|
2236
|
+
onUnsupportedEncoding: (encoding, request, reply) => {
|
|
2237
|
+
reply.code(406)
|
|
2238
|
+
return JSON.stringify({ hello: encoding })
|
|
2239
|
+
}
|
|
2240
|
+
})
|
|
2241
|
+
|
|
2242
|
+
fastify.get('/', (request, reply) => {
|
|
2243
|
+
reply
|
|
2244
|
+
.header('Content-Type', 'text/plain')
|
|
2245
|
+
.send(createReadStream('./package.json'))
|
|
2246
|
+
})
|
|
2247
|
+
|
|
2248
|
+
const response = await fastify.inject({
|
|
2249
|
+
url: '/',
|
|
2250
|
+
method: 'GET',
|
|
2251
|
+
headers: {
|
|
2252
|
+
'accept-encoding': 'hello'
|
|
2253
|
+
}
|
|
2254
|
+
})
|
|
2255
|
+
t.equal(response.statusCode, 406)
|
|
2256
|
+
t.same(JSON.parse(response.payload), { hello: 'hello' })
|
|
2257
|
+
})
|
|
2258
|
+
|
|
2259
|
+
t.test('it should call the defined `onUnsupportedEncoding()` method and throw an error', async (t) => {
|
|
2260
|
+
t.plan(2)
|
|
2261
|
+
|
|
2262
|
+
const fastify = Fastify()
|
|
2263
|
+
await fastify.register(compressPlugin, {
|
|
2264
|
+
global: true,
|
|
2265
|
+
onUnsupportedEncoding: (encoding, request, reply) => {
|
|
2266
|
+
reply.code(406)
|
|
2267
|
+
throw new Error('testing error')
|
|
2268
|
+
}
|
|
2269
|
+
})
|
|
2270
|
+
|
|
2271
|
+
fastify.get('/', (request, reply) => {
|
|
2272
|
+
reply
|
|
2273
|
+
.header('Content-Type', 'text/plain')
|
|
2274
|
+
.send(createReadStream('./package.json'))
|
|
2275
|
+
})
|
|
2276
|
+
|
|
2277
|
+
const response = await fastify.inject({
|
|
2278
|
+
url: '/',
|
|
2279
|
+
method: 'GET',
|
|
2280
|
+
headers: {
|
|
2281
|
+
'accept-encoding': 'hello'
|
|
2282
|
+
}
|
|
2283
|
+
})
|
|
2284
|
+
t.equal(response.statusCode, 406)
|
|
2285
|
+
t.same(JSON.parse(response.payload), {
|
|
2286
|
+
error: 'Not Acceptable',
|
|
2287
|
+
message: 'testing error',
|
|
2288
|
+
statusCode: 406
|
|
2289
|
+
})
|
|
2290
|
+
})
|
|
2291
|
+
})
|
|
2292
|
+
|
|
2293
|
+
test('`Accept-Encoding` request header values :', async (t) => {
|
|
2294
|
+
t.test('can contain white space', async (t) => {
|
|
2295
|
+
t.plan(2)
|
|
2296
|
+
|
|
2297
|
+
const fastify = Fastify()
|
|
2298
|
+
await fastify.register(compressPlugin, { threshold: 0 })
|
|
2299
|
+
|
|
2300
|
+
const json = { hello: 'world' }
|
|
2301
|
+
|
|
2302
|
+
fastify.get('/', (request, reply) => {
|
|
2303
|
+
reply.send(json)
|
|
2304
|
+
})
|
|
2305
|
+
|
|
2306
|
+
const response = await fastify.inject({
|
|
2307
|
+
url: '/',
|
|
2308
|
+
method: 'GET',
|
|
2309
|
+
headers: {
|
|
2310
|
+
'accept-encoding': 'hello, gzip'
|
|
2311
|
+
}
|
|
2312
|
+
})
|
|
2313
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
2314
|
+
t.equal(response.headers['content-encoding'], 'gzip')
|
|
2315
|
+
t.equal(payload.toString('utf-8'), JSON.stringify(json))
|
|
2316
|
+
})
|
|
2317
|
+
|
|
2318
|
+
t.test('can contain mixed uppercase and lowercase characters (e.g.: compressing a Stream)', async (t) => {
|
|
2319
|
+
t.plan(2)
|
|
2320
|
+
|
|
2321
|
+
const fastify = Fastify()
|
|
2322
|
+
await fastify.register(compressPlugin, { global: true })
|
|
2323
|
+
|
|
2324
|
+
fastify.get('/', (request, reply) => {
|
|
2325
|
+
reply
|
|
2326
|
+
.type('text/plain')
|
|
2327
|
+
.compress(createReadStream('./package.json'))
|
|
2328
|
+
})
|
|
2329
|
+
|
|
2330
|
+
const response = await fastify.inject({
|
|
2331
|
+
url: '/',
|
|
2332
|
+
method: 'GET',
|
|
2333
|
+
headers: {
|
|
2334
|
+
'accept-encoding': 'GZiP'
|
|
2335
|
+
}
|
|
2336
|
+
})
|
|
2337
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
2338
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
2339
|
+
t.equal(response.headers['content-encoding'], 'gzip')
|
|
2340
|
+
t.equal(payload.toString('utf-8'), file)
|
|
2341
|
+
})
|
|
2342
|
+
|
|
2343
|
+
t.test('can contain mixed uppercase and lowercase characters (e.g.: compressing a Buffer)', async (t) => {
|
|
2344
|
+
t.plan(1)
|
|
2345
|
+
|
|
2346
|
+
const fastify = Fastify()
|
|
2347
|
+
await fastify.register(compressPlugin, { global: true, threshold: 0 })
|
|
2348
|
+
|
|
2349
|
+
const buf = Buffer.from('hello world')
|
|
2350
|
+
fastify.get('/', (request, reply) => {
|
|
2351
|
+
reply.compress(buf)
|
|
2352
|
+
})
|
|
2353
|
+
|
|
2354
|
+
const response = await fastify.inject({
|
|
2355
|
+
url: '/',
|
|
2356
|
+
method: 'GET',
|
|
2357
|
+
headers: {
|
|
2358
|
+
'accept-encoding': 'GzIp'
|
|
2359
|
+
}
|
|
2360
|
+
})
|
|
2361
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
2362
|
+
t.equal(payload.toString('utf-8'), buf.toString())
|
|
2363
|
+
})
|
|
2364
|
+
|
|
2365
|
+
t.test('should support `gzip` alias value `x-gzip`', async (t) => {
|
|
2366
|
+
t.plan(2)
|
|
2367
|
+
|
|
2368
|
+
const fastify = Fastify()
|
|
2369
|
+
await fastify.register(compressPlugin, { global: true })
|
|
2370
|
+
|
|
2371
|
+
fastify.get('/', (request, reply) => {
|
|
2372
|
+
reply
|
|
2373
|
+
.type('text/plain')
|
|
2374
|
+
.compress(
|
|
2375
|
+
createReadStream('./package.json')
|
|
2376
|
+
)
|
|
2377
|
+
})
|
|
2378
|
+
|
|
2379
|
+
const response = await fastify.inject({
|
|
2380
|
+
url: '/',
|
|
2381
|
+
method: 'GET',
|
|
2382
|
+
headers: {
|
|
2383
|
+
'accept-encoding': 'x-gzip'
|
|
2384
|
+
}
|
|
2385
|
+
})
|
|
2386
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
2387
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
2388
|
+
t.equal(response.headers['content-encoding'], 'gzip')
|
|
2389
|
+
t.equal(payload.toString('utf-8'), file)
|
|
2390
|
+
})
|
|
2391
|
+
|
|
2392
|
+
t.test('should support quality syntax', async (t) => {
|
|
2393
|
+
t.plan(2)
|
|
2394
|
+
|
|
2395
|
+
const fastify = Fastify()
|
|
2396
|
+
await fastify.register(compressPlugin, { global: true })
|
|
2397
|
+
|
|
2398
|
+
fastify.get('/', (request, reply) => {
|
|
2399
|
+
reply
|
|
2400
|
+
.type('text/plain')
|
|
2401
|
+
.compress(
|
|
2402
|
+
createReadStream('./package.json').pipe(zlib.createDeflate())
|
|
2403
|
+
)
|
|
2404
|
+
})
|
|
2405
|
+
|
|
2406
|
+
const response = await fastify.inject({
|
|
2407
|
+
url: '/',
|
|
2408
|
+
method: 'GET',
|
|
2409
|
+
headers: {
|
|
2410
|
+
'accept-encoding': 'gzip;q=0.5,deflate;q=0.6,identity;q=0.3'
|
|
2411
|
+
}
|
|
2412
|
+
})
|
|
2413
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
2414
|
+
const payload = zlib.inflateSync(response.rawPayload)
|
|
2415
|
+
t.equal(response.headers['content-encoding'], 'deflate')
|
|
2416
|
+
t.equal(payload.toString('utf-8'), file)
|
|
2417
|
+
})
|
|
2418
|
+
})
|
|
2419
|
+
|
|
2420
|
+
test('It should compress data if `customTypes` is set and matches `Content-Type` reply header value', async (t) => {
|
|
2421
|
+
t.plan(2)
|
|
2422
|
+
const fastify = Fastify()
|
|
2423
|
+
await fastify.register(compressPlugin, { customTypes: /x-user-header$/ })
|
|
2424
|
+
|
|
2425
|
+
fastify.get('/', (request, reply) => {
|
|
2426
|
+
reply
|
|
2427
|
+
.type('application/x-user-header')
|
|
2428
|
+
.send(createReadStream('./package.json'))
|
|
2429
|
+
})
|
|
2430
|
+
|
|
2431
|
+
const response = await fastify.inject({
|
|
2432
|
+
url: '/',
|
|
2433
|
+
method: 'GET',
|
|
2434
|
+
headers: {
|
|
2435
|
+
'accept-encoding': 'gzip'
|
|
2436
|
+
}
|
|
2437
|
+
})
|
|
2438
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
2439
|
+
const payload = zlib.gunzipSync(response.rawPayload)
|
|
2440
|
+
t.equal(response.headers['content-encoding'], 'gzip')
|
|
2441
|
+
t.equal(payload.toString('utf-8'), file)
|
|
2442
|
+
})
|
|
2443
|
+
|
|
2444
|
+
test('It should not apply `customTypes` option if the passed value is not a RegExp', async (t) => {
|
|
2445
|
+
t.plan(2)
|
|
2446
|
+
|
|
2447
|
+
const fastify = Fastify()
|
|
2448
|
+
await fastify.register(compressPlugin, { customTypes: 'x-user-header' })
|
|
2449
|
+
|
|
2450
|
+
fastify.get('/', (request, reply) => {
|
|
2451
|
+
reply
|
|
2452
|
+
.type('application/x-user-header')
|
|
2453
|
+
.send(createReadStream('./package.json'))
|
|
2454
|
+
})
|
|
2455
|
+
|
|
2456
|
+
const response = await fastify.inject({
|
|
2457
|
+
url: '/',
|
|
2458
|
+
method: 'GET',
|
|
2459
|
+
headers: {
|
|
2460
|
+
'accept-encoding': 'gzip'
|
|
2461
|
+
}
|
|
2462
|
+
})
|
|
2463
|
+
t.notOk(response.headers['content-encoding'])
|
|
2464
|
+
t.equal(response.statusCode, 200)
|
|
2465
|
+
})
|
|
2466
|
+
|
|
2467
|
+
test('When `encodings` option is set, it should only use the registered value', async (t) => {
|
|
2468
|
+
t.plan(2)
|
|
2469
|
+
|
|
2470
|
+
const fastify = Fastify()
|
|
2471
|
+
await fastify.register(compressPlugin, { encodings: ['deflate'] })
|
|
2472
|
+
|
|
2473
|
+
fastify.get('/', (request, reply) => {
|
|
2474
|
+
reply.send(createReadStream('./package.json'))
|
|
2475
|
+
})
|
|
2476
|
+
|
|
2477
|
+
const response = await fastify.inject({
|
|
2478
|
+
url: '/',
|
|
2479
|
+
method: 'GET',
|
|
2480
|
+
headers: {
|
|
2481
|
+
'accept-encoding': 'br,gzip,deflate'
|
|
2482
|
+
}
|
|
2483
|
+
})
|
|
2484
|
+
t.equal(response.headers['content-encoding'], 'deflate')
|
|
2485
|
+
t.equal(response.statusCode, 200)
|
|
2486
|
+
})
|
|
2487
|
+
|
|
2488
|
+
test('It should send data compressed according to `brotliOptions`', async (t) => {
|
|
2489
|
+
t.plan(2)
|
|
2490
|
+
const brotliOptions = {
|
|
2491
|
+
params: {
|
|
2492
|
+
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
|
|
2493
|
+
[zlib.constants.BROTLI_PARAM_QUALITY]: 4
|
|
2494
|
+
}
|
|
2495
|
+
}
|
|
2496
|
+
|
|
2497
|
+
const fastify = Fastify()
|
|
2498
|
+
await fastify.register(compressPlugin, { global: true, brotliOptions })
|
|
2499
|
+
|
|
2500
|
+
fastify.get('/', (request, reply) => {
|
|
2501
|
+
reply
|
|
2502
|
+
.type('text/plain')
|
|
2503
|
+
.compress(createReadStream('./package.json'))
|
|
2504
|
+
})
|
|
2505
|
+
|
|
2506
|
+
const response = await fastify.inject({
|
|
2507
|
+
url: '/',
|
|
2508
|
+
method: 'GET',
|
|
2509
|
+
headers: {
|
|
2510
|
+
'accept-encoding': 'br'
|
|
2511
|
+
}
|
|
2512
|
+
})
|
|
2513
|
+
|
|
2514
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
2515
|
+
const payload = zlib.brotliDecompressSync(response.rawPayload, brotliOptions)
|
|
2516
|
+
t.equal(response.headers['content-encoding'], 'br')
|
|
2517
|
+
t.equal(payload.toString('utf-8'), file)
|
|
2518
|
+
})
|
|
2519
|
+
|
|
2520
|
+
test('It should send data compressed according to `zlibOptions` :', async (t) => {
|
|
2521
|
+
t.test('when using deflate encoding', async (t) => {
|
|
2522
|
+
t.plan(2)
|
|
2523
|
+
|
|
2524
|
+
const zlibOptions = {
|
|
2525
|
+
level: 1,
|
|
2526
|
+
dictionary: Buffer.from('fastifycompress')
|
|
2527
|
+
}
|
|
2528
|
+
|
|
2529
|
+
const fastify = Fastify()
|
|
2530
|
+
await fastify.register(compressPlugin, {
|
|
2531
|
+
global: true,
|
|
2532
|
+
zlibOptions
|
|
2533
|
+
})
|
|
2534
|
+
|
|
2535
|
+
fastify.get('/', (request, reply) => {
|
|
2536
|
+
reply
|
|
2537
|
+
.type('text/plain')
|
|
2538
|
+
.compress(createReadStream('./package.json'))
|
|
2539
|
+
})
|
|
2540
|
+
|
|
2541
|
+
const response = await fastify.inject({
|
|
2542
|
+
url: '/',
|
|
2543
|
+
method: 'GET',
|
|
2544
|
+
headers: {
|
|
2545
|
+
'accept-encoding': 'deflate'
|
|
2546
|
+
}
|
|
2547
|
+
})
|
|
2548
|
+
const file = readFileSync('./package.json')
|
|
2549
|
+
t.equal(response.headers['content-encoding'], 'deflate')
|
|
2550
|
+
t.same(response.rawPayload, zlib.deflateSync(file, zlibOptions))
|
|
2551
|
+
})
|
|
2552
|
+
|
|
2553
|
+
t.test('when using gzip encoding', async (t) => {
|
|
2554
|
+
t.plan(2)
|
|
2555
|
+
|
|
2556
|
+
const zlibOptions = { level: 1 }
|
|
2557
|
+
|
|
2558
|
+
const fastify = Fastify()
|
|
2559
|
+
await fastify.register(compressPlugin, {
|
|
2560
|
+
global: true,
|
|
2561
|
+
zlibOptions
|
|
2562
|
+
})
|
|
2563
|
+
|
|
2564
|
+
fastify.get('/', (request, reply) => {
|
|
2565
|
+
reply
|
|
2566
|
+
.type('text/plain')
|
|
2567
|
+
.compress(createReadStream('./package.json'))
|
|
2568
|
+
})
|
|
2569
|
+
|
|
2570
|
+
const response = await fastify.inject({
|
|
2571
|
+
url: '/',
|
|
2572
|
+
method: 'GET',
|
|
2573
|
+
headers: {
|
|
2574
|
+
'accept-encoding': 'gzip'
|
|
2575
|
+
}
|
|
2576
|
+
})
|
|
2577
|
+
const file = readFileSync('./package.json')
|
|
2578
|
+
t.equal(response.headers['content-encoding'], 'gzip')
|
|
2579
|
+
t.same(response.rawPayload, zlib.gzipSync(file, zlibOptions))
|
|
2580
|
+
})
|
|
2581
|
+
})
|
|
2582
|
+
|
|
2583
|
+
test('It should concat `accept-encoding` to `Vary` reply header if present', async (t) => {
|
|
2584
|
+
t.plan(2)
|
|
2585
|
+
|
|
2586
|
+
const fastify = Fastify()
|
|
2587
|
+
await fastify.register(compressPlugin, { global: true })
|
|
2588
|
+
|
|
2589
|
+
fastify.get('/', (request, reply) => {
|
|
2590
|
+
reply
|
|
2591
|
+
.header('vary', 'different-header')
|
|
2592
|
+
.type('text/plain')
|
|
2593
|
+
.compress(createReadStream('./package.json'))
|
|
2594
|
+
})
|
|
2595
|
+
|
|
2596
|
+
fastify.get('/foo', (request, reply) => {
|
|
2597
|
+
reply
|
|
2598
|
+
.header('vary', ['different-header', 'my-header'])
|
|
2599
|
+
.type('text/plain')
|
|
2600
|
+
.compress(createReadStream('./package.json'))
|
|
2601
|
+
})
|
|
2602
|
+
|
|
2603
|
+
await fastify.inject({
|
|
2604
|
+
url: '/',
|
|
2605
|
+
method: 'GET',
|
|
2606
|
+
headers: {
|
|
2607
|
+
'accept-encoding': 'deflate'
|
|
2608
|
+
}
|
|
2609
|
+
}).then((response) => {
|
|
2610
|
+
t.same(response.headers.vary, 'different-header, accept-encoding')
|
|
2611
|
+
}).catch((err) => {
|
|
2612
|
+
t.error(err)
|
|
2613
|
+
})
|
|
2614
|
+
|
|
2615
|
+
await fastify.inject({
|
|
2616
|
+
url: '/foo',
|
|
2617
|
+
method: 'GET',
|
|
2618
|
+
headers: {
|
|
2619
|
+
'accept-encoding': 'deflate'
|
|
2620
|
+
}
|
|
2621
|
+
}).then((response) => {
|
|
2622
|
+
t.same(response.headers.vary, 'different-header, my-header, accept-encoding')
|
|
2623
|
+
}).catch((err) => {
|
|
2624
|
+
t.error(err)
|
|
2625
|
+
})
|
|
2626
|
+
})
|
|
2627
|
+
|
|
2628
|
+
test('It should not add `accept-encoding` to `Vary` reply header if already present', async (t) => {
|
|
2629
|
+
t.plan(2)
|
|
2630
|
+
|
|
2631
|
+
const fastify = Fastify()
|
|
2632
|
+
await fastify.register(compressPlugin, { global: true })
|
|
2633
|
+
|
|
2634
|
+
fastify.get('/', (request, reply) => {
|
|
2635
|
+
reply
|
|
2636
|
+
.header('vary', 'accept-encoding,different-header')
|
|
2637
|
+
.type('text/plain')
|
|
2638
|
+
.compress(createReadStream('./package.json'))
|
|
2639
|
+
})
|
|
2640
|
+
|
|
2641
|
+
fastify.get('/foo', (request, reply) => {
|
|
2642
|
+
reply
|
|
2643
|
+
.header('vary', 'accept-encoding, different-header, my-header')
|
|
2644
|
+
.type('text/plain')
|
|
2645
|
+
.compress(createReadStream('./package.json'))
|
|
2646
|
+
})
|
|
2647
|
+
|
|
2648
|
+
await fastify.inject({
|
|
2649
|
+
url: '/',
|
|
2650
|
+
method: 'GET',
|
|
2651
|
+
headers: {
|
|
2652
|
+
'accept-encoding': 'deflate'
|
|
2653
|
+
}
|
|
2654
|
+
}).then((response) => {
|
|
2655
|
+
t.same(response.headers.vary, 'accept-encoding,different-header')
|
|
2656
|
+
}).catch((err) => {
|
|
2657
|
+
t.error(err)
|
|
2658
|
+
})
|
|
2659
|
+
|
|
2660
|
+
await fastify.inject({
|
|
2661
|
+
url: '/foo',
|
|
2662
|
+
method: 'GET',
|
|
2663
|
+
headers: {
|
|
2664
|
+
'accept-encoding': 'deflate'
|
|
2665
|
+
}
|
|
2666
|
+
}).then((response) => {
|
|
2667
|
+
t.same(response.headers.vary, 'accept-encoding, different-header, my-header')
|
|
2668
|
+
}).catch((err) => {
|
|
2669
|
+
t.error(err)
|
|
2670
|
+
})
|
|
2671
|
+
})
|
|
2672
|
+
|
|
2673
|
+
test('It should follow the `Accept-Encoding` request header encoding order', async (t) => {
|
|
2674
|
+
t.plan(2)
|
|
2675
|
+
|
|
2676
|
+
const fastify = Fastify()
|
|
2677
|
+
await fastify.register(compressPlugin, { global: true })
|
|
2678
|
+
|
|
2679
|
+
fastify.get('/', (request, reply) => {
|
|
2680
|
+
reply
|
|
2681
|
+
.type('text/plain')
|
|
2682
|
+
.compress(createReadStream('./package.json'))
|
|
2683
|
+
})
|
|
2684
|
+
|
|
2685
|
+
const response = await fastify.inject({
|
|
2686
|
+
url: '/',
|
|
2687
|
+
method: 'GET',
|
|
2688
|
+
headers: {
|
|
2689
|
+
'accept-encoding': 'invalid,br,gzip'
|
|
2690
|
+
}
|
|
2691
|
+
})
|
|
2692
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
2693
|
+
const payload = zlib.brotliDecompressSync(response.rawPayload)
|
|
2694
|
+
t.equal(response.headers['content-encoding'], 'br')
|
|
2695
|
+
t.equal(payload.toString('utf-8'), file)
|
|
2696
|
+
})
|
|
2697
|
+
|
|
2698
|
+
test('It should sort and follow custom `encodings` options', async (t) => {
|
|
2699
|
+
t.plan(2)
|
|
2700
|
+
|
|
2701
|
+
const fastify = Fastify()
|
|
2702
|
+
await fastify.register(compressPlugin, {
|
|
2703
|
+
global: true,
|
|
2704
|
+
encodings: ['gzip', 'br']
|
|
2705
|
+
})
|
|
2706
|
+
|
|
2707
|
+
fastify.get('/', (request, reply) => {
|
|
2708
|
+
reply
|
|
2709
|
+
.type('text/plain')
|
|
2710
|
+
.compress(createReadStream('./package.json'))
|
|
2711
|
+
})
|
|
2712
|
+
|
|
2713
|
+
const response = await fastify.inject({
|
|
2714
|
+
url: '/',
|
|
2715
|
+
method: 'GET',
|
|
2716
|
+
headers: {
|
|
2717
|
+
'accept-encoding': 'hello,gzip,br'
|
|
2718
|
+
}
|
|
2719
|
+
})
|
|
2720
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
2721
|
+
const payload = zlib.brotliDecompressSync(response.rawPayload)
|
|
2722
|
+
t.equal(response.headers['content-encoding'], 'br')
|
|
2723
|
+
t.equal(payload.toString('utf-8'), file)
|
|
2724
|
+
})
|
|
2725
|
+
|
|
2726
|
+
test('It should sort and follow custom `requestEncodings` options', async (t) => {
|
|
2727
|
+
t.plan(2)
|
|
2728
|
+
|
|
2729
|
+
const fastify = Fastify()
|
|
2730
|
+
await fastify.register(compressPlugin, {
|
|
2731
|
+
global: true,
|
|
2732
|
+
requestEncodings: ['gzip', 'br']
|
|
2733
|
+
})
|
|
2734
|
+
|
|
2735
|
+
fastify.get('/', (request, reply) => {
|
|
2736
|
+
reply
|
|
2737
|
+
.type('text/plain')
|
|
2738
|
+
.compress(createReadStream('./package.json'))
|
|
2739
|
+
})
|
|
2740
|
+
|
|
2741
|
+
const response = await fastify.inject({
|
|
2742
|
+
url: '/',
|
|
2743
|
+
method: 'GET',
|
|
2744
|
+
headers: {
|
|
2745
|
+
'accept-encoding': 'hello,gzip,br'
|
|
2746
|
+
}
|
|
2747
|
+
})
|
|
2748
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
2749
|
+
const payload = zlib.brotliDecompressSync(response.rawPayload)
|
|
2750
|
+
t.equal(response.headers['content-encoding'], 'br')
|
|
2751
|
+
t.equal(payload.toString('utf-8'), file)
|
|
2752
|
+
})
|
|
2753
|
+
|
|
2754
|
+
test('It should uncompress data when `Accept-Encoding` request header is missing :', async (t) => {
|
|
2755
|
+
t.test('using the fallback Node.js `zlib.createInflate()` method', async (t) => {
|
|
2756
|
+
t.plan(3)
|
|
2757
|
+
|
|
2758
|
+
const fastify = Fastify()
|
|
2759
|
+
await fastify.register(compressPlugin, {
|
|
2760
|
+
global: true,
|
|
2761
|
+
inflateIfDeflated: true,
|
|
2762
|
+
threshold: 0,
|
|
2763
|
+
zlib: true // will trigger a fallback on the default zlib.createInflate
|
|
2764
|
+
})
|
|
2765
|
+
|
|
2766
|
+
const json = { hello: 'world' }
|
|
2767
|
+
fastify.get('/', (request, reply) => {
|
|
2768
|
+
reply.send(zlib.deflateSync(JSON.stringify(json)))
|
|
2769
|
+
})
|
|
2770
|
+
|
|
2771
|
+
const response = await fastify.inject({
|
|
2772
|
+
url: '/',
|
|
2773
|
+
method: 'GET'
|
|
2774
|
+
})
|
|
2775
|
+
t.equal(response.statusCode, 200)
|
|
2776
|
+
t.notOk(response.headers['content-encoding'])
|
|
2777
|
+
t.same(JSON.parse('' + response.payload), json)
|
|
2778
|
+
})
|
|
2779
|
+
|
|
2780
|
+
t.test('using the fallback Node.js `zlib.createGunzip()` method', async (t) => {
|
|
2781
|
+
t.plan(3)
|
|
2782
|
+
|
|
2783
|
+
const fastify = Fastify()
|
|
2784
|
+
await fastify.register(compressPlugin, {
|
|
2785
|
+
global: true,
|
|
2786
|
+
inflateIfDeflated: true,
|
|
2787
|
+
threshold: 0,
|
|
2788
|
+
zlib: true // will trigger a fallback on the default zlib.createGunzip
|
|
2789
|
+
})
|
|
2790
|
+
|
|
2791
|
+
const json = { hello: 'world' }
|
|
2792
|
+
fastify.get('/', (request, reply) => {
|
|
2793
|
+
reply.send(zlib.gzipSync(JSON.stringify(json)))
|
|
2794
|
+
})
|
|
2795
|
+
|
|
2796
|
+
const response = await fastify.inject({
|
|
2797
|
+
url: '/',
|
|
2798
|
+
method: 'GET'
|
|
2799
|
+
})
|
|
2800
|
+
t.equal(response.statusCode, 200)
|
|
2801
|
+
t.notOk(response.headers['content-encoding'])
|
|
2802
|
+
t.same(JSON.parse('' + response.payload), json)
|
|
2803
|
+
})
|
|
2804
|
+
|
|
2805
|
+
t.test('when the data is a deflated Buffer', async (t) => {
|
|
2806
|
+
t.plan(3)
|
|
2807
|
+
|
|
2808
|
+
const fastify = Fastify()
|
|
2809
|
+
await fastify.register(compressPlugin, {
|
|
2810
|
+
global: true,
|
|
2811
|
+
inflateIfDeflated: true,
|
|
2812
|
+
threshold: 0
|
|
2813
|
+
})
|
|
2814
|
+
|
|
2815
|
+
const json = { hello: 'world' }
|
|
2816
|
+
fastify.get('/', (request, reply) => {
|
|
2817
|
+
reply.send(zlib.deflateSync(JSON.stringify(json)))
|
|
2818
|
+
})
|
|
2819
|
+
|
|
2820
|
+
const response = await fastify.inject({
|
|
2821
|
+
url: '/',
|
|
2822
|
+
method: 'GET'
|
|
2823
|
+
})
|
|
2824
|
+
t.equal(response.statusCode, 200)
|
|
2825
|
+
t.notOk(response.headers['content-encoding'])
|
|
2826
|
+
t.same(JSON.parse('' + response.payload), json)
|
|
2827
|
+
})
|
|
2828
|
+
|
|
2829
|
+
t.test('when the data is a gzipped Buffer', async (t) => {
|
|
2830
|
+
t.plan(3)
|
|
2831
|
+
|
|
2832
|
+
const fastify = Fastify()
|
|
2833
|
+
await fastify.register(compressPlugin, {
|
|
2834
|
+
global: true,
|
|
2835
|
+
inflateIfDeflated: true,
|
|
2836
|
+
threshold: 0
|
|
2837
|
+
})
|
|
2838
|
+
|
|
2839
|
+
const json = { hello: 'world' }
|
|
2840
|
+
fastify.get('/', (request, reply) => {
|
|
2841
|
+
reply.send(zlib.gzipSync(JSON.stringify(json)))
|
|
2842
|
+
})
|
|
2843
|
+
|
|
2844
|
+
const response = await fastify.inject({
|
|
2845
|
+
url: '/',
|
|
2846
|
+
method: 'GET'
|
|
2847
|
+
})
|
|
2848
|
+
t.equal(response.statusCode, 200)
|
|
2849
|
+
t.notOk(response.headers['content-encoding'])
|
|
2850
|
+
t.same(JSON.parse('' + response.payload), json)
|
|
2851
|
+
})
|
|
2852
|
+
|
|
2853
|
+
t.test('when the data is a deflated Stream', async (t) => {
|
|
2854
|
+
t.plan(3)
|
|
2855
|
+
|
|
2856
|
+
const fastify = Fastify()
|
|
2857
|
+
await fastify.register(compressPlugin, {
|
|
2858
|
+
global: true,
|
|
2859
|
+
inflateIfDeflated: true,
|
|
2860
|
+
threshold: 0
|
|
2861
|
+
})
|
|
2862
|
+
|
|
2863
|
+
fastify.get('/', (request, reply) => {
|
|
2864
|
+
reply.send(
|
|
2865
|
+
createReadStream('./package.json')
|
|
2866
|
+
.pipe(zlib.createDeflate())
|
|
2867
|
+
)
|
|
2868
|
+
})
|
|
2869
|
+
|
|
2870
|
+
const response = await fastify.inject({
|
|
2871
|
+
url: '/',
|
|
2872
|
+
method: 'GET'
|
|
2873
|
+
})
|
|
2874
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
2875
|
+
t.equal(response.statusCode, 200)
|
|
2876
|
+
t.notOk(response.headers['content-encoding'])
|
|
2877
|
+
t.equal(response.rawPayload.toString('utf-8'), file)
|
|
2878
|
+
})
|
|
2879
|
+
|
|
2880
|
+
t.test('when the data is a gzipped Stream', async (t) => {
|
|
2881
|
+
t.plan(3)
|
|
2882
|
+
|
|
2883
|
+
const fastify = Fastify()
|
|
2884
|
+
await fastify.register(compressPlugin, {
|
|
2885
|
+
global: true,
|
|
2886
|
+
inflateIfDeflated: true,
|
|
2887
|
+
threshold: 0
|
|
2888
|
+
})
|
|
2889
|
+
|
|
2890
|
+
fastify.get('/', (request, reply) => {
|
|
2891
|
+
reply.send(
|
|
2892
|
+
createReadStream('./package.json')
|
|
2893
|
+
.pipe(zlib.createGzip())
|
|
2894
|
+
)
|
|
2895
|
+
})
|
|
2896
|
+
|
|
2897
|
+
const response = await fastify.inject({
|
|
2898
|
+
url: '/',
|
|
2899
|
+
method: 'GET'
|
|
2900
|
+
})
|
|
2901
|
+
const file = readFileSync('./package.json', 'utf8')
|
|
2902
|
+
t.equal(response.statusCode, 200)
|
|
2903
|
+
t.notOk(response.headers['content-encoding'])
|
|
2904
|
+
t.equal(response.rawPayload.toString('utf-8'), file)
|
|
2905
|
+
})
|
|
2906
|
+
|
|
2907
|
+
t.test('when the data has been compressed multiple times', async (t) => {
|
|
2908
|
+
t.plan(3)
|
|
2909
|
+
|
|
2910
|
+
const fastify = Fastify()
|
|
2911
|
+
await fastify.register(compressPlugin, {
|
|
2912
|
+
global: true,
|
|
2913
|
+
inflateIfDeflated: true,
|
|
2914
|
+
threshold: 0
|
|
2915
|
+
})
|
|
2916
|
+
|
|
2917
|
+
const json = { hello: 'world' }
|
|
2918
|
+
fastify.get('/', (request, reply) => {
|
|
2919
|
+
reply.send(
|
|
2920
|
+
[0, 1, 2, 3, 4, 5, 6].reduce(
|
|
2921
|
+
(x) => zlib.gzipSync(x), JSON.stringify(json)
|
|
2922
|
+
)
|
|
2923
|
+
)
|
|
2924
|
+
})
|
|
2925
|
+
|
|
2926
|
+
const response = await fastify.inject({
|
|
2927
|
+
url: '/',
|
|
2928
|
+
method: 'GET'
|
|
2929
|
+
})
|
|
2930
|
+
t.equal(response.statusCode, 200)
|
|
2931
|
+
t.notOk(response.headers['content-encoding'])
|
|
2932
|
+
t.same(JSON.parse('' + response.payload), json)
|
|
2933
|
+
})
|
|
2934
|
+
})
|
|
2935
|
+
|
|
2936
|
+
test('When `onUnsupportedEncoding` is set and the `Accept-Encoding` request header value is an unsupported encoding', async (t) => {
|
|
2937
|
+
t.test('it should call the defined `onUnsupportedEncoding()` method', async (t) => {
|
|
2938
|
+
t.plan(3)
|
|
2939
|
+
|
|
2940
|
+
const fastify = Fastify()
|
|
2941
|
+
await fastify.register(compressPlugin, {
|
|
2942
|
+
global: true,
|
|
2943
|
+
onUnsupportedEncoding: (encoding, request, reply) => {
|
|
2944
|
+
reply.code(406)
|
|
2945
|
+
return JSON.stringify({ hello: encoding })
|
|
2946
|
+
}
|
|
2947
|
+
})
|
|
2948
|
+
|
|
2949
|
+
fastify.get('/', (request, reply) => {
|
|
2950
|
+
reply
|
|
2951
|
+
.type('text/plain')
|
|
2952
|
+
.compress(createReadStream('./package.json'))
|
|
2953
|
+
})
|
|
2954
|
+
|
|
2955
|
+
const response = await fastify.inject({
|
|
2956
|
+
url: '/',
|
|
2957
|
+
method: 'GET',
|
|
2958
|
+
headers: {
|
|
2959
|
+
'accept-encoding': 'hello'
|
|
2960
|
+
}
|
|
2961
|
+
})
|
|
2962
|
+
t.equal(response.statusCode, 406)
|
|
2963
|
+
t.equal(response.headers.vary, 'accept-encoding')
|
|
2964
|
+
t.same(JSON.parse(response.payload), { hello: 'hello' })
|
|
2965
|
+
})
|
|
2966
|
+
|
|
2967
|
+
t.test('it should call the defined `onUnsupportedEncoding()` method and throw an error', async (t) => {
|
|
2968
|
+
t.plan(2)
|
|
2969
|
+
|
|
2970
|
+
const fastify = Fastify()
|
|
2971
|
+
await fastify.register(compressPlugin, {
|
|
2972
|
+
global: true,
|
|
2973
|
+
onUnsupportedEncoding: (encoding, request, reply) => {
|
|
2974
|
+
reply.code(406)
|
|
2975
|
+
throw new Error('testing error')
|
|
2976
|
+
}
|
|
2977
|
+
})
|
|
2978
|
+
|
|
2979
|
+
fastify.get('/', (request, reply) => {
|
|
2980
|
+
reply
|
|
2981
|
+
.type('text/plain')
|
|
2982
|
+
.compress(createReadStream('./package.json'))
|
|
2983
|
+
})
|
|
2984
|
+
|
|
2985
|
+
const response = await fastify.inject({
|
|
2986
|
+
url: '/',
|
|
2987
|
+
method: 'GET',
|
|
2988
|
+
headers: {
|
|
2989
|
+
'accept-encoding': 'hello'
|
|
2990
|
+
}
|
|
2991
|
+
})
|
|
2992
|
+
t.equal(response.statusCode, 406)
|
|
2993
|
+
t.same(JSON.parse(response.payload), {
|
|
2994
|
+
error: 'Not Acceptable',
|
|
2995
|
+
message: 'testing error',
|
|
2996
|
+
statusCode: 406
|
|
2997
|
+
})
|
|
2998
|
+
})
|
|
2999
|
+
})
|
|
3000
|
+
|
|
3001
|
+
test('It should error :', async (t) => {
|
|
3002
|
+
t.test('when `encodings` array is empty', (t) => {
|
|
3003
|
+
t.plan(1)
|
|
3004
|
+
|
|
3005
|
+
const fastify = Fastify()
|
|
3006
|
+
fastify.register(compressPlugin, { encodings: [] })
|
|
3007
|
+
|
|
3008
|
+
fastify.ready(err => {
|
|
3009
|
+
t.ok(err instanceof Error)
|
|
3010
|
+
})
|
|
3011
|
+
})
|
|
3012
|
+
|
|
3013
|
+
t.test('when no entries in `encodings` are supported', (t) => {
|
|
3014
|
+
t.plan(1)
|
|
3015
|
+
|
|
3016
|
+
const fastify = Fastify()
|
|
3017
|
+
fastify.register(compressPlugin, {
|
|
3018
|
+
encodings: ['(not-a-real-encoding)']
|
|
3019
|
+
})
|
|
3020
|
+
|
|
3021
|
+
fastify.ready(err => {
|
|
3022
|
+
t.ok(err instanceof Error)
|
|
3023
|
+
})
|
|
3024
|
+
})
|
|
3025
|
+
})
|
|
3026
|
+
|
|
3027
|
+
test('It should return an error when using `reply.compress()` with a missing payload', async (t) => {
|
|
3028
|
+
t.plan(2)
|
|
3029
|
+
|
|
3030
|
+
const fastify = Fastify()
|
|
3031
|
+
await fastify.register(compressPlugin, { global: true })
|
|
3032
|
+
|
|
3033
|
+
fastify.get('/', (request, reply) => {
|
|
3034
|
+
reply.compress()
|
|
3035
|
+
})
|
|
3036
|
+
|
|
3037
|
+
const response = await fastify.inject({
|
|
3038
|
+
url: '/',
|
|
3039
|
+
method: 'GET'
|
|
3040
|
+
})
|
|
3041
|
+
const payload = JSON.parse(response.payload)
|
|
3042
|
+
t.equal(response.statusCode, 500)
|
|
3043
|
+
t.same({
|
|
3044
|
+
error: 'Internal Server Error',
|
|
3045
|
+
message: 'Internal server error',
|
|
3046
|
+
statusCode: 500
|
|
3047
|
+
}, payload)
|
|
3048
|
+
})
|