@fastify/compress 8.0.0-pre.fv5.2 → 8.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/.c8rc +14 -0
- package/index.js +5 -5
- package/package.json +8 -8
- package/test/global-compress.test.js +493 -492
- package/test/global-decompress.test.js +70 -60
- package/test/regression/issue-288.test.js +4 -4
- package/test/routes-compress.test.js +80 -72
- package/test/routes-decompress.test.js +45 -41
- package/test/utils.test.js +52 -48
- package/.taprc +0 -3
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { test } = require('
|
|
3
|
+
const { test, describe } = require('node:test')
|
|
4
4
|
const { createReadStream } = require('node:fs')
|
|
5
5
|
const path = require('node:path')
|
|
6
6
|
const zlib = require('node:zlib')
|
|
@@ -18,8 +18,8 @@ function createPayload (compressor) {
|
|
|
18
18
|
return payload
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
describe('It should decompress the request payload :', async () => {
|
|
22
|
+
test('using brotli algorithm when `Content-Encoding` request header value is set to `br`', async (t) => {
|
|
23
23
|
t.plan(2)
|
|
24
24
|
|
|
25
25
|
const fastify = Fastify()
|
|
@@ -38,11 +38,11 @@ test('It should decompress the request payload :', async (t) => {
|
|
|
38
38
|
},
|
|
39
39
|
payload: createPayload(zlib.createBrotliCompress)
|
|
40
40
|
})
|
|
41
|
-
t.equal(response.statusCode, 200)
|
|
42
|
-
t.equal(response.body, '@fastify/compress')
|
|
41
|
+
t.assert.equal(response.statusCode, 200)
|
|
42
|
+
t.assert.equal(response.body, '@fastify/compress')
|
|
43
43
|
})
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
test('using deflate algorithm when `Content-Encoding` request header value is set to `deflate`', async (t) => {
|
|
46
46
|
t.plan(2)
|
|
47
47
|
|
|
48
48
|
const fastify = Fastify()
|
|
@@ -61,11 +61,11 @@ test('It should decompress the request payload :', async (t) => {
|
|
|
61
61
|
},
|
|
62
62
|
payload: createPayload(zlib.createDeflate)
|
|
63
63
|
})
|
|
64
|
-
t.equal(response.statusCode, 200)
|
|
65
|
-
t.equal(response.body, '@fastify/compress')
|
|
64
|
+
t.assert.equal(response.statusCode, 200)
|
|
65
|
+
t.assert.equal(response.body, '@fastify/compress')
|
|
66
66
|
})
|
|
67
67
|
|
|
68
|
-
|
|
68
|
+
test('using gzip algorithm when `Content-Encoding` request header value is set to `gzip`', async (t) => {
|
|
69
69
|
t.plan(2)
|
|
70
70
|
|
|
71
71
|
const fastify = Fastify()
|
|
@@ -84,11 +84,11 @@ test('It should decompress the request payload :', async (t) => {
|
|
|
84
84
|
},
|
|
85
85
|
payload: createPayload(zlib.createGzip)
|
|
86
86
|
})
|
|
87
|
-
t.equal(response.statusCode, 200)
|
|
88
|
-
t.equal(response.body, '@fastify/compress')
|
|
87
|
+
t.assert.equal(response.statusCode, 200)
|
|
88
|
+
t.assert.equal(response.body, '@fastify/compress')
|
|
89
89
|
})
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
test('using the `forceRequestEncoding` provided algorithm over the `Content-Encoding` request header value', async (t) => {
|
|
92
92
|
t.plan(2)
|
|
93
93
|
|
|
94
94
|
const fastify = Fastify()
|
|
@@ -107,13 +107,13 @@ test('It should decompress the request payload :', async (t) => {
|
|
|
107
107
|
},
|
|
108
108
|
payload: createPayload(zlib.createGzip)
|
|
109
109
|
})
|
|
110
|
-
t.equal(response.statusCode, 200)
|
|
111
|
-
t.equal(response.body, '@fastify/compress')
|
|
110
|
+
t.assert.equal(response.statusCode, 200)
|
|
111
|
+
t.assert.equal(response.body, '@fastify/compress')
|
|
112
112
|
})
|
|
113
113
|
})
|
|
114
114
|
|
|
115
|
-
|
|
116
|
-
|
|
115
|
+
describe('It should not decompress :', async () => {
|
|
116
|
+
test('when `Content-Encoding` request header is missing', async (t) => {
|
|
117
117
|
t.plan(2)
|
|
118
118
|
|
|
119
119
|
const fastify = Fastify()
|
|
@@ -131,11 +131,11 @@ test('It should not decompress :', async (t) => {
|
|
|
131
131
|
},
|
|
132
132
|
payload: createPayload()
|
|
133
133
|
})
|
|
134
|
-
t.equal(response.statusCode, 200)
|
|
135
|
-
t.equal(response.body, '@fastify/compress')
|
|
134
|
+
t.assert.equal(response.statusCode, 200)
|
|
135
|
+
t.assert.equal(response.body, '@fastify/compress')
|
|
136
136
|
})
|
|
137
137
|
|
|
138
|
-
|
|
138
|
+
test('when `Content-Encoding` request header value is set to `identity`', async (t) => {
|
|
139
139
|
t.plan(2)
|
|
140
140
|
|
|
141
141
|
const fastify = Fastify()
|
|
@@ -154,13 +154,13 @@ test('It should not decompress :', async (t) => {
|
|
|
154
154
|
},
|
|
155
155
|
payload: createPayload()
|
|
156
156
|
})
|
|
157
|
-
t.equal(response.statusCode, 200)
|
|
158
|
-
t.equal(response.body, '@fastify/compress')
|
|
157
|
+
t.assert.equal(response.statusCode, 200)
|
|
158
|
+
t.assert.equal(response.body, '@fastify/compress')
|
|
159
159
|
})
|
|
160
160
|
})
|
|
161
161
|
|
|
162
|
-
|
|
163
|
-
|
|
162
|
+
describe('It should return an error :', async () => {
|
|
163
|
+
test('when `Content-Encoding` request header value is not supported', async (t) => {
|
|
164
164
|
t.plan(2)
|
|
165
165
|
|
|
166
166
|
const fastify = Fastify()
|
|
@@ -179,8 +179,8 @@ test('It should return an error :', async (t) => {
|
|
|
179
179
|
},
|
|
180
180
|
payload: createPayload(zlib.createDeflate)
|
|
181
181
|
})
|
|
182
|
-
t.equal(response.statusCode, 415)
|
|
183
|
-
t.
|
|
182
|
+
t.assert.equal(response.statusCode, 415)
|
|
183
|
+
t.assert.deepEqual(response.json(), {
|
|
184
184
|
statusCode: 415,
|
|
185
185
|
code: 'FST_CP_ERR_INVALID_CONTENT_ENCODING',
|
|
186
186
|
error: 'Unsupported Media Type',
|
|
@@ -188,7 +188,7 @@ test('It should return an error :', async (t) => {
|
|
|
188
188
|
})
|
|
189
189
|
})
|
|
190
190
|
|
|
191
|
-
|
|
191
|
+
test('when `Content-Encoding` request header value is disabled by the `requestEncodings` option', async (t) => {
|
|
192
192
|
t.plan(2)
|
|
193
193
|
|
|
194
194
|
const fastify = Fastify()
|
|
@@ -207,8 +207,8 @@ test('It should return an error :', async (t) => {
|
|
|
207
207
|
},
|
|
208
208
|
payload: createPayload(zlib.createDeflate)
|
|
209
209
|
})
|
|
210
|
-
t.equal(response.statusCode, 415)
|
|
211
|
-
t.
|
|
210
|
+
t.assert.equal(response.statusCode, 415)
|
|
211
|
+
t.assert.deepEqual(response.json(), {
|
|
212
212
|
statusCode: 415,
|
|
213
213
|
code: 'FST_CP_ERR_INVALID_CONTENT_ENCODING',
|
|
214
214
|
error: 'Unsupported Media Type',
|
|
@@ -216,7 +216,7 @@ test('It should return an error :', async (t) => {
|
|
|
216
216
|
})
|
|
217
217
|
})
|
|
218
218
|
|
|
219
|
-
|
|
219
|
+
test('when the compressed payload is invalid according to `Content-Encoding` request header value', async (t) => {
|
|
220
220
|
t.plan(2)
|
|
221
221
|
|
|
222
222
|
const fastify = Fastify()
|
|
@@ -235,8 +235,8 @@ test('It should return an error :', async (t) => {
|
|
|
235
235
|
},
|
|
236
236
|
payload: createPayload(zlib.createGzip)
|
|
237
237
|
})
|
|
238
|
-
t.equal(response.statusCode, 400)
|
|
239
|
-
t.
|
|
238
|
+
t.assert.equal(response.statusCode, 400)
|
|
239
|
+
t.assert.deepEqual(response.json(), {
|
|
240
240
|
statusCode: 400,
|
|
241
241
|
code: 'FST_CP_ERR_INVALID_CONTENT',
|
|
242
242
|
error: 'Bad Request',
|
|
@@ -245,8 +245,8 @@ test('It should return an error :', async (t) => {
|
|
|
245
245
|
})
|
|
246
246
|
})
|
|
247
247
|
|
|
248
|
-
|
|
249
|
-
|
|
248
|
+
describe('It should return the error returned by :', async () => {
|
|
249
|
+
test('`onUnsupportedRequestEncoding`', async (t) => {
|
|
250
250
|
t.plan(2)
|
|
251
251
|
|
|
252
252
|
const fastify = Fastify()
|
|
@@ -274,8 +274,8 @@ test('It should return the error returned by :', async (t) => {
|
|
|
274
274
|
},
|
|
275
275
|
payload: createPayload(zlib.createDeflate)
|
|
276
276
|
})
|
|
277
|
-
t.equal(response.statusCode, 400)
|
|
278
|
-
t.
|
|
277
|
+
t.assert.equal(response.statusCode, 400)
|
|
278
|
+
t.assert.deepEqual(response.json(), {
|
|
279
279
|
statusCode: 400,
|
|
280
280
|
code: 'INVALID',
|
|
281
281
|
error: 'Bad Request',
|
|
@@ -283,7 +283,7 @@ test('It should return the error returned by :', async (t) => {
|
|
|
283
283
|
})
|
|
284
284
|
})
|
|
285
285
|
|
|
286
|
-
|
|
286
|
+
test('`onInvalidRequestPayload`', async (t) => {
|
|
287
287
|
t.plan(2)
|
|
288
288
|
|
|
289
289
|
const fastify = Fastify()
|
|
@@ -311,8 +311,8 @@ test('It should return the error returned by :', async (t) => {
|
|
|
311
311
|
},
|
|
312
312
|
payload: createPayload(zlib.createGzip)
|
|
313
313
|
})
|
|
314
|
-
t.equal(response.statusCode, 400)
|
|
315
|
-
t.
|
|
314
|
+
t.assert.equal(response.statusCode, 400)
|
|
315
|
+
t.assert.deepEqual(response.json(), {
|
|
316
316
|
statusCode: 400,
|
|
317
317
|
code: 'INVALID',
|
|
318
318
|
error: 'Bad Request',
|
|
@@ -321,8 +321,8 @@ test('It should return the error returned by :', async (t) => {
|
|
|
321
321
|
})
|
|
322
322
|
})
|
|
323
323
|
|
|
324
|
-
|
|
325
|
-
|
|
324
|
+
describe('It should return the default error :', async () => {
|
|
325
|
+
test('when `onUnsupportedRequestEncoding` throws', async (t) => {
|
|
326
326
|
t.plan(2)
|
|
327
327
|
|
|
328
328
|
const fastify = Fastify()
|
|
@@ -345,8 +345,8 @@ test('It should return the default error :', async (t) => {
|
|
|
345
345
|
},
|
|
346
346
|
payload: createPayload(zlib.createDeflate)
|
|
347
347
|
})
|
|
348
|
-
t.equal(response.statusCode, 415)
|
|
349
|
-
t.
|
|
348
|
+
t.assert.equal(response.statusCode, 415)
|
|
349
|
+
t.assert.deepEqual(response.json(), {
|
|
350
350
|
statusCode: 415,
|
|
351
351
|
code: 'FST_CP_ERR_INVALID_CONTENT_ENCODING',
|
|
352
352
|
error: 'Unsupported Media Type',
|
|
@@ -354,7 +354,7 @@ test('It should return the default error :', async (t) => {
|
|
|
354
354
|
})
|
|
355
355
|
})
|
|
356
356
|
|
|
357
|
-
|
|
357
|
+
test('when `onInvalidRequestPayload` throws', async (t) => {
|
|
358
358
|
t.plan(2)
|
|
359
359
|
|
|
360
360
|
const fastify = Fastify()
|
|
@@ -377,8 +377,8 @@ test('It should return the default error :', async (t) => {
|
|
|
377
377
|
},
|
|
378
378
|
payload: createPayload(zlib.createGzip)
|
|
379
379
|
})
|
|
380
|
-
t.equal(response.statusCode, 400)
|
|
381
|
-
t.
|
|
380
|
+
t.assert.equal(response.statusCode, 400)
|
|
381
|
+
t.assert.deepEqual(response.json(), {
|
|
382
382
|
statusCode: 400,
|
|
383
383
|
code: 'FST_CP_ERR_INVALID_CONTENT',
|
|
384
384
|
error: 'Bad Request',
|
|
@@ -387,37 +387,47 @@ test('It should return the default error :', async (t) => {
|
|
|
387
387
|
})
|
|
388
388
|
})
|
|
389
389
|
|
|
390
|
-
test('It should validate `requestEncodings` option', (t) => {
|
|
390
|
+
test('It should validate `requestEncodings` option', async (t) => {
|
|
391
391
|
t.plan(1)
|
|
392
392
|
|
|
393
393
|
const fastify = Fastify()
|
|
394
394
|
fastify.register(compressPlugin, { requestEncodings: [] })
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
395
|
+
t.assert.rejects(
|
|
396
|
+
async () => fastify.ready(),
|
|
397
|
+
{
|
|
398
|
+
name: 'Error',
|
|
399
|
+
message: 'The `requestEncodings` option array must have at least 1 item.'
|
|
400
|
+
}
|
|
401
|
+
)
|
|
399
402
|
})
|
|
400
403
|
|
|
401
|
-
|
|
402
|
-
|
|
404
|
+
describe('It should make sure that at least one encoding value is supported :', async () => {
|
|
405
|
+
test('when setting `requestEncodings`', async (t) => {
|
|
403
406
|
t.plan(1)
|
|
404
407
|
|
|
405
408
|
const fastify = Fastify()
|
|
406
409
|
fastify.register(compressPlugin, { requestEncodings: ['whatever'] })
|
|
407
410
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
+
t.assert.rejects(
|
|
412
|
+
async () => fastify.ready(),
|
|
413
|
+
{
|
|
414
|
+
name: 'Error',
|
|
415
|
+
message: 'None of the passed `requestEncodings` were supported — request decompression not possible.'
|
|
416
|
+
}
|
|
417
|
+
)
|
|
411
418
|
})
|
|
412
419
|
|
|
413
|
-
|
|
420
|
+
test('when setting `forceRequestEncodings`', async (t) => {
|
|
414
421
|
t.plan(1)
|
|
415
422
|
|
|
416
423
|
const fastify = Fastify()
|
|
417
424
|
fastify.register(compressPlugin, { forceRequestEncoding: ['whatever'] })
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
425
|
+
t.assert.rejects(
|
|
426
|
+
async () => fastify.ready(),
|
|
427
|
+
{
|
|
428
|
+
name: 'Error',
|
|
429
|
+
message: 'Unsupported decompression encoding whatever.'
|
|
430
|
+
}
|
|
431
|
+
)
|
|
422
432
|
})
|
|
423
433
|
})
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { test } = require('
|
|
3
|
+
const { test } = require('node:test')
|
|
4
4
|
const Fastify = require('fastify')
|
|
5
5
|
const fastifyCompress = require('../..')
|
|
6
6
|
const { request } = require('node:http')
|
|
@@ -35,7 +35,7 @@ test('should not corrupt the file content', async (t) => {
|
|
|
35
35
|
})
|
|
36
36
|
.join('\n')
|
|
37
37
|
const fastify = new Fastify()
|
|
38
|
-
t.
|
|
38
|
+
t.after(() => fastify.close())
|
|
39
39
|
|
|
40
40
|
fastify.register(async (instance, opts) => {
|
|
41
41
|
await fastify.register(fastifyCompress)
|
|
@@ -57,6 +57,6 @@ test('should not corrupt the file content', async (t) => {
|
|
|
57
57
|
fetch(`${address}/no-compress`)
|
|
58
58
|
])
|
|
59
59
|
|
|
60
|
-
t.equal(body1, body2)
|
|
61
|
-
t.equal(body1, twoByteUnicodeContent)
|
|
60
|
+
t.assert.equal(body1, body2)
|
|
61
|
+
t.assert.equal(body1, twoByteUnicodeContent)
|
|
62
62
|
})
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { test } = require('
|
|
3
|
+
const { test, describe } = require('node:test')
|
|
4
4
|
const { createReadStream, readFile, readFileSync } = require('node:fs')
|
|
5
5
|
const zlib = require('node:zlib')
|
|
6
6
|
const Fastify = require('fastify')
|
|
7
7
|
const compressPlugin = require('../index')
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
describe('When using routes `compress` settings :', async (t) => {
|
|
10
|
+
test('it should compress data using the route custom provided `createDeflate` method', async (t) => {
|
|
11
11
|
t.plan(12)
|
|
12
|
+
const equal = t.assert.equal
|
|
12
13
|
|
|
13
14
|
let usedCustomGlobal = false
|
|
14
15
|
let usedCustom = false
|
|
@@ -39,15 +40,15 @@ test('When using routes `compress` settings :', async (t) => {
|
|
|
39
40
|
'accept-encoding': 'deflate'
|
|
40
41
|
}
|
|
41
42
|
}).then((response) => {
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
equal(usedCustom, false)
|
|
44
|
+
equal(usedCustomGlobal, true)
|
|
44
45
|
|
|
45
46
|
const file = readFileSync('./package.json', 'utf8')
|
|
46
47
|
const payload = zlib.inflateSync(response.rawPayload)
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
t.
|
|
50
|
-
|
|
48
|
+
equal(response.headers.vary, 'accept-encoding')
|
|
49
|
+
equal(response.headers['content-encoding'], 'deflate')
|
|
50
|
+
t.assert.ok(!response.headers['content-length'], 'no content length')
|
|
51
|
+
equal(payload.toString('utf-8'), file)
|
|
51
52
|
|
|
52
53
|
usedCustom = false
|
|
53
54
|
usedCustomGlobal = false
|
|
@@ -60,19 +61,20 @@ test('When using routes `compress` settings :', async (t) => {
|
|
|
60
61
|
'accept-encoding': 'deflate'
|
|
61
62
|
}
|
|
62
63
|
})
|
|
63
|
-
|
|
64
|
-
|
|
64
|
+
equal(usedCustom, true)
|
|
65
|
+
equal(usedCustomGlobal, false)
|
|
65
66
|
|
|
66
67
|
const file = readFileSync('./package.json', 'utf8')
|
|
67
68
|
const payload = zlib.inflateSync(response.rawPayload)
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
t.
|
|
71
|
-
|
|
69
|
+
equal(response.headers.vary, 'accept-encoding')
|
|
70
|
+
equal(response.headers['content-encoding'], 'deflate')
|
|
71
|
+
t.assert.ok(!response.headers['content-length'], 'no content length')
|
|
72
|
+
equal(payload.toString('utf-8'), file)
|
|
72
73
|
})
|
|
73
74
|
|
|
74
|
-
|
|
75
|
+
test('it should compress data using the route custom provided `createGzip` method', async (t) => {
|
|
75
76
|
t.plan(10)
|
|
77
|
+
const equal = t.assert.equal
|
|
76
78
|
|
|
77
79
|
let usedCustomGlobal = false
|
|
78
80
|
let usedCustom = false
|
|
@@ -101,14 +103,14 @@ test('When using routes `compress` settings :', async (t) => {
|
|
|
101
103
|
'accept-encoding': 'gzip'
|
|
102
104
|
}
|
|
103
105
|
}).then((response) => {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
+
equal(usedCustom, false)
|
|
107
|
+
equal(usedCustomGlobal, true)
|
|
106
108
|
|
|
107
109
|
const file = readFileSync('./package.json', 'utf8')
|
|
108
110
|
const payload = zlib.gunzipSync(response.rawPayload)
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
111
|
+
equal(response.headers.vary, 'accept-encoding')
|
|
112
|
+
equal(response.headers['content-encoding'], 'gzip')
|
|
113
|
+
equal(payload.toString('utf-8'), file)
|
|
112
114
|
|
|
113
115
|
usedCustom = false
|
|
114
116
|
usedCustomGlobal = false
|
|
@@ -121,18 +123,19 @@ test('When using routes `compress` settings :', async (t) => {
|
|
|
121
123
|
'accept-encoding': 'gzip'
|
|
122
124
|
}
|
|
123
125
|
})
|
|
124
|
-
|
|
125
|
-
|
|
126
|
+
equal(usedCustom, true)
|
|
127
|
+
equal(usedCustomGlobal, false)
|
|
126
128
|
|
|
127
129
|
const file = readFileSync('./package.json', 'utf8')
|
|
128
130
|
const payload = zlib.gunzipSync(response.rawPayload)
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
131
|
+
equal(response.headers.vary, 'accept-encoding')
|
|
132
|
+
equal(response.headers['content-encoding'], 'gzip')
|
|
133
|
+
equal(payload.toString('utf-8'), file)
|
|
132
134
|
})
|
|
133
135
|
|
|
134
|
-
|
|
136
|
+
test('it should not compress data when `global` is `false` unless `compress` routes settings have been set up', async (t) => {
|
|
135
137
|
t.plan(12)
|
|
138
|
+
const equal = t.assert.equal
|
|
136
139
|
|
|
137
140
|
let usedCustom = false
|
|
138
141
|
const customZlib = { createGzip: () => (usedCustom = true) && zlib.createGzip() }
|
|
@@ -142,7 +145,7 @@ test('When using routes `compress` settings :', async (t) => {
|
|
|
142
145
|
|
|
143
146
|
fastify.get('/', (request, reply) => {
|
|
144
147
|
// compress function should still be available
|
|
145
|
-
t.
|
|
148
|
+
t.assert.ok(typeof reply.compress === 'function')
|
|
146
149
|
|
|
147
150
|
reply.send({ foo: 1 })
|
|
148
151
|
})
|
|
@@ -168,10 +171,10 @@ test('When using routes `compress` settings :', async (t) => {
|
|
|
168
171
|
'accept-encoding': 'gzip'
|
|
169
172
|
}
|
|
170
173
|
}).then((response) => {
|
|
171
|
-
t.equal(usedCustom, false)
|
|
172
|
-
t.
|
|
173
|
-
t.
|
|
174
|
-
t.equal(response.rawPayload.toString('utf-8'), JSON.stringify({ foo: 1 }))
|
|
174
|
+
t.assert.equal(usedCustom, false)
|
|
175
|
+
t.assert.ok(!response.headers.vary)
|
|
176
|
+
t.assert.ok(!response.headers['content-encoding'])
|
|
177
|
+
t.assert.equal(response.rawPayload.toString('utf-8'), JSON.stringify({ foo: 1 }))
|
|
175
178
|
|
|
176
179
|
usedCustom = false
|
|
177
180
|
})
|
|
@@ -183,13 +186,13 @@ test('When using routes `compress` settings :', async (t) => {
|
|
|
183
186
|
'accept-encoding': 'gzip'
|
|
184
187
|
}
|
|
185
188
|
}).then((response) => {
|
|
186
|
-
|
|
189
|
+
equal(usedCustom, true)
|
|
187
190
|
|
|
188
191
|
const file = readFileSync('./package.json', 'utf8')
|
|
189
192
|
const payload = zlib.gunzipSync(response.rawPayload)
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
+
equal(response.headers.vary, 'accept-encoding')
|
|
194
|
+
equal(response.headers['content-encoding'], 'gzip')
|
|
195
|
+
equal(payload.toString('utf-8'), file)
|
|
193
196
|
})
|
|
194
197
|
|
|
195
198
|
const response = await fastify.inject({
|
|
@@ -200,14 +203,14 @@ test('When using routes `compress` settings :', async (t) => {
|
|
|
200
203
|
}
|
|
201
204
|
})
|
|
202
205
|
const payload = zlib.gunzipSync(response.rawPayload)
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
+
equal(response.headers.vary, 'accept-encoding')
|
|
207
|
+
equal(response.headers['content-encoding'], 'gzip')
|
|
208
|
+
equal(payload.toString('utf-8'), JSON.stringify({ foo: 1 }))
|
|
206
209
|
})
|
|
207
210
|
|
|
208
|
-
|
|
211
|
+
test('it should not compress data when route `compress` option is set to `false`', async (t) => {
|
|
209
212
|
t.plan(3)
|
|
210
|
-
|
|
213
|
+
const equal = t.assert.equal
|
|
211
214
|
const content = { message: 'Hello World!' }
|
|
212
215
|
|
|
213
216
|
const fastify = Fastify()
|
|
@@ -226,13 +229,15 @@ test('When using routes `compress` settings :', async (t) => {
|
|
|
226
229
|
'accept-encoding': 'gzip'
|
|
227
230
|
}
|
|
228
231
|
})
|
|
229
|
-
|
|
230
|
-
t.
|
|
231
|
-
t.
|
|
232
|
+
|
|
233
|
+
t.assert.ok(!response.headers.vary)
|
|
234
|
+
t.assert.ok(!response.headers['content-encoding'])
|
|
235
|
+
equal(response.rawPayload.toString('utf-8'), JSON.stringify(content))
|
|
232
236
|
})
|
|
233
237
|
|
|
234
|
-
|
|
238
|
+
test('it should throw an error on invalid route `compress` settings', async (t) => {
|
|
235
239
|
t.plan(1)
|
|
240
|
+
const equal = t.assert.equal
|
|
236
241
|
|
|
237
242
|
const fastify = Fastify()
|
|
238
243
|
await fastify.register(compressPlugin, { global: false })
|
|
@@ -244,14 +249,15 @@ test('When using routes `compress` settings :', async (t) => {
|
|
|
244
249
|
reply.send('')
|
|
245
250
|
})
|
|
246
251
|
} catch (err) {
|
|
247
|
-
|
|
252
|
+
equal(err.message, 'Unknown value for route compress configuration')
|
|
248
253
|
}
|
|
249
254
|
})
|
|
250
255
|
})
|
|
251
256
|
|
|
252
|
-
|
|
253
|
-
|
|
257
|
+
describe('When `compress.removeContentLengthHeader` is `false`, it should not remove `Content-Length` header :', async () => {
|
|
258
|
+
test('using `reply.compress()`', async (t) => {
|
|
254
259
|
t.plan(4)
|
|
260
|
+
const equal = t.assert.equal
|
|
255
261
|
|
|
256
262
|
const fastify = Fastify()
|
|
257
263
|
await fastify.register(compressPlugin, { global: true })
|
|
@@ -280,14 +286,15 @@ test('When `compress.removeContentLengthHeader` is `false`, it should not remove
|
|
|
280
286
|
})
|
|
281
287
|
const file = readFileSync('./package.json', 'utf8')
|
|
282
288
|
const payload = zlib.inflateSync(response.rawPayload)
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
289
|
+
equal(response.headers.vary, 'accept-encoding')
|
|
290
|
+
equal(response.headers['content-encoding'], 'deflate')
|
|
291
|
+
equal(response.headers['content-length'], payload.length.toString())
|
|
292
|
+
equal(payload.toString('utf-8'), file)
|
|
287
293
|
})
|
|
288
294
|
|
|
289
|
-
|
|
295
|
+
test('using `onSend` hook', async (t) => {
|
|
290
296
|
t.plan(4)
|
|
297
|
+
const equal = t.assert.equal
|
|
291
298
|
|
|
292
299
|
const fastify = Fastify()
|
|
293
300
|
await fastify.register(compressPlugin, { global: true })
|
|
@@ -316,16 +323,17 @@ test('When `compress.removeContentLengthHeader` is `false`, it should not remove
|
|
|
316
323
|
})
|
|
317
324
|
const file = readFileSync('./package.json', 'utf8')
|
|
318
325
|
const payload = zlib.inflateSync(response.rawPayload)
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
326
|
+
equal(response.headers.vary, 'accept-encoding')
|
|
327
|
+
equal(response.headers['content-encoding'], 'deflate')
|
|
328
|
+
equal(response.headers['content-length'], payload.length.toString())
|
|
329
|
+
equal(payload.toString('utf-8'), file)
|
|
323
330
|
})
|
|
324
331
|
})
|
|
325
332
|
|
|
326
|
-
|
|
327
|
-
|
|
333
|
+
describe('When using the old routes `{ config: compress }` option :', async () => {
|
|
334
|
+
test('it should compress data using the route custom provided `createGzip` method', async (t) => {
|
|
328
335
|
t.plan(10)
|
|
336
|
+
const equal = t.assert.equal
|
|
329
337
|
|
|
330
338
|
let usedCustomGlobal = false
|
|
331
339
|
let usedCustom = false
|
|
@@ -358,14 +366,14 @@ test('When using the old routes `{ config: compress }` option :', async (t) => {
|
|
|
358
366
|
'accept-encoding': 'gzip'
|
|
359
367
|
}
|
|
360
368
|
}).then((response) => {
|
|
361
|
-
|
|
362
|
-
|
|
369
|
+
equal(usedCustom, false)
|
|
370
|
+
equal(usedCustomGlobal, true)
|
|
363
371
|
|
|
364
372
|
const file = readFileSync('./package.json', 'utf8')
|
|
365
373
|
const payload = zlib.gunzipSync(response.rawPayload)
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
374
|
+
equal(response.headers.vary, 'accept-encoding')
|
|
375
|
+
equal(response.headers['content-encoding'], 'gzip')
|
|
376
|
+
equal(payload.toString('utf-8'), file)
|
|
369
377
|
|
|
370
378
|
usedCustom = false
|
|
371
379
|
usedCustomGlobal = false
|
|
@@ -378,17 +386,17 @@ test('When using the old routes `{ config: compress }` option :', async (t) => {
|
|
|
378
386
|
'accept-encoding': 'gzip'
|
|
379
387
|
}
|
|
380
388
|
})
|
|
381
|
-
|
|
382
|
-
|
|
389
|
+
equal(usedCustom, true)
|
|
390
|
+
equal(usedCustomGlobal, false)
|
|
383
391
|
|
|
384
392
|
const file = readFileSync('./package.json', 'utf8')
|
|
385
393
|
const payload = zlib.gunzipSync(response.rawPayload)
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
394
|
+
equal(response.headers.vary, 'accept-encoding')
|
|
395
|
+
equal(response.headers['content-encoding'], 'gzip')
|
|
396
|
+
equal(payload.toString('utf-8'), file)
|
|
389
397
|
})
|
|
390
398
|
|
|
391
|
-
|
|
399
|
+
test('it should use the old routes `{ config: compress }` options over routes `compress` options', async (t) => {
|
|
392
400
|
t.plan(1)
|
|
393
401
|
|
|
394
402
|
const fastify = Fastify()
|
|
@@ -406,7 +414,7 @@ test('When using the old routes `{ config: compress }` option :', async (t) => {
|
|
|
406
414
|
reply.send('')
|
|
407
415
|
})
|
|
408
416
|
} catch (err) {
|
|
409
|
-
t.equal(err.message, 'Unknown value for route compress configuration')
|
|
417
|
+
t.assert.equal(err.message, 'Unknown value for route compress configuration')
|
|
410
418
|
}
|
|
411
419
|
})
|
|
412
420
|
})
|
|
@@ -430,5 +438,5 @@ test('It should avoid to trigger `onSend` hook twice', async (t) => {
|
|
|
430
438
|
'accept-encoding': 'br'
|
|
431
439
|
}
|
|
432
440
|
})
|
|
433
|
-
t.
|
|
441
|
+
t.assert.deepEqual(JSON.parse(zlib.brotliDecompressSync(response.rawPayload)), { hi: true })
|
|
434
442
|
})
|