@fastify/compress 7.0.0 → 7.0.2

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.
@@ -263,14 +263,14 @@ test('It should send compressed Stream data when `global` is `true` :', async (t
263
263
  })
264
264
  const file = readFileSync('./package.json', 'utf8')
265
265
  const payload = zlib.inflateSync(response.rawPayload)
266
- t.equal(response.headers['content-encoding'], 'deflate')
267
266
  t.equal(response.headers.vary, 'accept-encoding')
267
+ t.equal(response.headers['content-encoding'], 'deflate')
268
268
  t.notOk(response.headers['content-length'], 'no content length')
269
269
  t.equal(payload.toString('utf-8'), file)
270
270
  })
271
271
 
272
272
  t.test('using gzip when `Accept-Encoding` request header is `gzip`', async (t) => {
273
- t.plan(2)
273
+ t.plan(3)
274
274
 
275
275
  const fastify = Fastify()
276
276
  await fastify.register(compressPlugin, { global: true })
@@ -290,6 +290,7 @@ test('It should send compressed Stream data when `global` is `true` :', async (t
290
290
  })
291
291
  const file = readFileSync('./package.json', 'utf8')
292
292
  const payload = zlib.gunzipSync(response.rawPayload)
293
+ t.equal(response.headers.vary, 'accept-encoding')
293
294
  t.equal(response.headers['content-encoding'], 'gzip')
294
295
  t.equal(payload.toString('utf-8'), file)
295
296
  })
@@ -433,7 +434,7 @@ test('It should send compressed JSON data when `global` is `true` :', async (t)
433
434
 
434
435
  test('It should fallback to the default `gzip` encoding compression :', async (t) => {
435
436
  t.test('when `Accept-Encoding` request header value is set to `*`', async (t) => {
436
- t.plan(2)
437
+ t.plan(3)
437
438
 
438
439
  const fastify = Fastify()
439
440
  await fastify.register(compressPlugin, { global: true })
@@ -453,12 +454,13 @@ test('It should fallback to the default `gzip` encoding compression :', async (t
453
454
  })
454
455
  const file = readFileSync('./package.json', 'utf8')
455
456
  const payload = zlib.gunzipSync(response.rawPayload)
457
+ t.equal(response.headers.vary, 'accept-encoding')
456
458
  t.equal(response.headers['content-encoding'], 'gzip')
457
459
  t.equal(payload.toString('utf-8'), file)
458
460
  })
459
461
 
460
462
  t.test('when `Accept-Encoding` request header value is set to multiple `*` directives', async (t) => {
461
- t.plan(2)
463
+ t.plan(3)
462
464
 
463
465
  const fastify = Fastify()
464
466
  await fastify.register(compressPlugin, { global: true })
@@ -478,6 +480,7 @@ test('It should fallback to the default `gzip` encoding compression :', async (t
478
480
  })
479
481
  const file = readFileSync('./package.json', 'utf8')
480
482
  const payload = zlib.gunzipSync(response.rawPayload)
483
+ t.equal(response.headers.vary, 'accept-encoding')
481
484
  t.equal(response.headers['content-encoding'], 'gzip')
482
485
  t.equal(payload.toString('utf-8'), file)
483
486
  })
@@ -485,7 +488,7 @@ test('It should fallback to the default `gzip` encoding compression :', async (t
485
488
 
486
489
  test('When a custom `zlib` option is provided, it should compress data :`', async (t) => {
487
490
  t.test('using the custom `createBrotliCompress()` method', async (t) => {
488
- t.plan(4)
491
+ t.plan(5)
489
492
 
490
493
  let usedCustom = false
491
494
  const customZlib = { createBrotliCompress: () => (usedCustom = true) && zlib.createBrotliCompress() }
@@ -510,13 +513,14 @@ test('When a custom `zlib` option is provided, it should compress data :`', asyn
510
513
 
511
514
  const file = readFileSync('./package.json', 'utf8')
512
515
  const payload = zlib.brotliDecompressSync(response.rawPayload)
516
+ t.equal(response.headers.vary, 'accept-encoding')
513
517
  t.equal(response.headers['content-encoding'], 'br')
514
518
  t.notOk(response.headers['content-length'], 'no content length')
515
519
  t.equal(payload.toString('utf-8'), file)
516
520
  })
517
521
 
518
522
  t.test('using the custom `createDeflate()` method', async (t) => {
519
- t.plan(4)
523
+ t.plan(5)
520
524
 
521
525
  let usedCustom = false
522
526
  const customZlib = { createDeflate: () => (usedCustom = true) && zlib.createDeflate() }
@@ -541,13 +545,14 @@ test('When a custom `zlib` option is provided, it should compress data :`', asyn
541
545
 
542
546
  const file = readFileSync('./package.json', 'utf8')
543
547
  const payload = zlib.inflateSync(response.rawPayload)
548
+ t.equal(response.headers.vary, 'accept-encoding')
544
549
  t.equal(response.headers['content-encoding'], 'deflate')
545
550
  t.notOk(response.headers['content-length'], 'no content length')
546
551
  t.equal(payload.toString('utf-8'), file)
547
552
  })
548
553
 
549
554
  t.test('using the custom `createGzip()` method', async (t) => {
550
- t.plan(3)
555
+ t.plan(4)
551
556
 
552
557
  let usedCustom = false
553
558
  const customZlib = { createGzip: () => (usedCustom = true) && zlib.createGzip() }
@@ -572,6 +577,7 @@ test('When a custom `zlib` option is provided, it should compress data :`', asyn
572
577
 
573
578
  const file = readFileSync('./package.json', 'utf8')
574
579
  const payload = zlib.gunzipSync(response.rawPayload)
580
+ t.equal(response.headers.vary, 'accept-encoding')
575
581
  t.equal(response.headers['content-encoding'], 'gzip')
576
582
  t.equal(payload.toString('utf-8'), file)
577
583
  })
@@ -662,7 +668,7 @@ test('When a malformed custom `zlib` option is provided, it should compress data
662
668
 
663
669
  test('When `inflateIfDeflated` is `true` and `X-No-Compression` request header is `true` :', async (t) => {
664
670
  t.test('it should uncompress payloads using the deflate algorithm', async (t) => {
665
- t.plan(3)
671
+ t.plan(4)
666
672
 
667
673
  const fastify = Fastify()
668
674
  await fastify.register(compressPlugin, { threshold: 0, inflateIfDeflated: true })
@@ -680,12 +686,13 @@ test('When `inflateIfDeflated` is `true` and `X-No-Compression` request header i
680
686
  }
681
687
  })
682
688
  t.equal(response.statusCode, 200)
689
+ t.notOk(response.headers.vary)
683
690
  t.notOk(response.headers['content-encoding'])
684
691
  t.same(JSON.parse('' + response.payload), json)
685
692
  })
686
693
 
687
694
  t.test('it should uncompress payloads using the gzip algorithm', async (t) => {
688
- t.plan(3)
695
+ t.plan(4)
689
696
 
690
697
  const fastify = Fastify()
691
698
  await fastify.register(compressPlugin, { threshold: 0, inflateIfDeflated: true })
@@ -703,13 +710,14 @@ test('When `inflateIfDeflated` is `true` and `X-No-Compression` request header i
703
710
  }
704
711
  })
705
712
  t.equal(response.statusCode, 200)
713
+ t.notOk(response.headers.vary)
706
714
  t.notOk(response.headers['content-encoding'])
707
715
  t.same(JSON.parse('' + response.payload), json)
708
716
  })
709
717
  })
710
718
 
711
719
  test('it should not uncompress payloads using the zip algorithm', async (t) => {
712
- t.plan(4)
720
+ t.plan(5)
713
721
 
714
722
  const fastify = Fastify()
715
723
  await fastify.register(compressPlugin, { threshold: 0, inflateIfDeflated: true })
@@ -731,6 +739,7 @@ test('it should not uncompress payloads using the zip algorithm', async (t) => {
731
739
  }
732
740
  })
733
741
  t.equal(response.statusCode, 200)
742
+ t.notOk(response.headers.vary)
734
743
  t.notOk(response.headers['content-encoding'])
735
744
  t.same(response.rawPayload, fileBuffer)
736
745
  t.equal(response.payload, fileBuffer.toString('utf-8'))
@@ -739,7 +748,7 @@ test('it should not uncompress payloads using the zip algorithm', async (t) => {
739
748
  test('It should not compress :', async (t) => {
740
749
  t.test('Using `reply.compress()` :', async (t) => {
741
750
  t.test('when payload length is smaller than the `threshold` defined value', async (t) => {
742
- t.plan(3)
751
+ t.plan(4)
743
752
 
744
753
  const fastify = Fastify()
745
754
  await fastify.register(compressPlugin, { threshold: 128 })
@@ -758,12 +767,13 @@ test('It should not compress :', async (t) => {
758
767
  }
759
768
  })
760
769
  t.equal(response.statusCode, 200)
770
+ t.notOk(response.headers.vary)
761
771
  t.notOk(response.headers['content-encoding'])
762
772
  t.equal(response.payload, 'a message')
763
773
  })
764
774
 
765
775
  t.test('when `customTypes` is set and does not match `Content-Type` reply header or `mime-db`', async (t) => {
766
- t.plan(2)
776
+ t.plan(3)
767
777
 
768
778
  const fastify = Fastify()
769
779
  await fastify.register(compressPlugin, { customTypes: /x-user-header$/u })
@@ -781,12 +791,13 @@ test('It should not compress :', async (t) => {
781
791
  'accept-encoding': 'gzip'
782
792
  }
783
793
  })
794
+ t.notOk(response.headers.vary, 'accept-encoding')
784
795
  t.notOk(response.headers['content-encoding'])
785
796
  t.equal(response.statusCode, 200)
786
797
  })
787
798
 
788
799
  t.test('when `customTypes` is a function and returns false on the provided `Content-Type` reply header`', async (t) => {
789
- t.plan(2)
800
+ t.plan(3)
790
801
 
791
802
  const fastify = Fastify()
792
803
  await fastify.register(compressPlugin, { customTypes: value => value === 'application/x-user-header' })
@@ -804,12 +815,13 @@ test('It should not compress :', async (t) => {
804
815
  'accept-encoding': 'gzip'
805
816
  }
806
817
  })
818
+ t.notOk(response.headers.vary)
807
819
  t.notOk(response.headers['content-encoding'])
808
820
  t.equal(response.statusCode, 200)
809
821
  })
810
822
 
811
823
  t.test('when `X-No-Compression` request header is `true`', async (t) => {
812
- t.plan(3)
824
+ t.plan(4)
813
825
 
814
826
  const fastify = Fastify()
815
827
  await fastify.register(compressPlugin, { global: true, threshold: 0 })
@@ -827,12 +839,13 @@ test('It should not compress :', async (t) => {
827
839
  }
828
840
  })
829
841
  t.equal(response.statusCode, 200)
842
+ t.notOk(response.headers.vary)
830
843
  t.notOk(response.headers['content-encoding'])
831
844
  t.same(JSON.parse(response.payload), json)
832
845
  })
833
846
 
834
847
  t.test('when `Content-Type` reply header is not set and the content is not detected as a compressible type', async (t) => {
835
- t.plan(2)
848
+ t.plan(3)
836
849
 
837
850
  const fastify = Fastify()
838
851
  await fastify.register(compressPlugin, { threshold: 0 })
@@ -855,12 +868,13 @@ test('It should not compress :', async (t) => {
855
868
  'accept-encoding': 'identity'
856
869
  }
857
870
  })
871
+ t.notOk(response.headers.vary)
858
872
  t.notOk(response.headers['content-encoding'])
859
873
  t.equal(response.payload, JSON.stringify(json))
860
874
  })
861
875
 
862
876
  t.test('when `Content-Type` reply header is a mime type with undefined compressible values', async (t) => {
863
- t.plan(3)
877
+ t.plan(4)
864
878
 
865
879
  const fastify = Fastify()
866
880
  await fastify.register(compressPlugin, { threshold: 0 })
@@ -879,12 +893,13 @@ test('It should not compress :', async (t) => {
879
893
  }
880
894
  })
881
895
  t.equal(response.statusCode, 200)
896
+ t.notOk(response.headers.vary)
882
897
  t.notOk(response.headers['content-encoding'])
883
898
  t.equal(response.payload, 'hello')
884
899
  })
885
900
 
886
901
  t.test('when `Content-Type` reply header value is `text/event-stream`', async (t) => {
887
- t.plan(3)
902
+ t.plan(4)
888
903
 
889
904
  const fastify = Fastify()
890
905
  await fastify.register(compressPlugin, { threshold: 0 })
@@ -906,12 +921,13 @@ test('It should not compress :', async (t) => {
906
921
  method: 'GET'
907
922
  })
908
923
  t.equal(response.statusCode, 200)
924
+ t.notOk(response.headers.vary)
909
925
  t.notOk(response.headers['content-encoding'])
910
926
  t.same(response.payload, 'event: open\n\nevent: change\ndata: schema\n\n')
911
927
  })
912
928
 
913
929
  t.test('when `Content-Type` reply header value is an invalid type', async (t) => {
914
- t.plan(3)
930
+ t.plan(4)
915
931
 
916
932
  const fastify = Fastify()
917
933
  await fastify.register(compressPlugin, { threshold: 0 })
@@ -930,6 +946,7 @@ test('It should not compress :', async (t) => {
930
946
  }
931
947
  })
932
948
  t.equal(response.statusCode, 200)
949
+ t.notOk(response.headers.vary)
933
950
  t.notOk(response.headers['content-encoding'])
934
951
  t.equal(response.payload, 'a message')
935
952
  })
@@ -950,13 +967,13 @@ test('It should not compress :', async (t) => {
950
967
  url: '/',
951
968
  method: 'GET'
952
969
  })
953
- t.equal(response.headers.vary, 'accept-encoding')
954
970
  t.equal(response.statusCode, 200)
971
+ t.notOk(response.headers.vary)
955
972
  t.notOk(response.headers['content-encoding'])
956
973
  })
957
974
 
958
975
  t.test('when `Accept-Encoding` request header is set to `identity`', async (t) => {
959
- t.plan(2)
976
+ t.plan(3)
960
977
 
961
978
  const fastify = Fastify()
962
979
  await fastify.register(compressPlugin, { global: true, threshold: 0 })
@@ -973,6 +990,7 @@ test('It should not compress :', async (t) => {
973
990
  }
974
991
  })
975
992
  const payload = JSON.parse(response.payload)
993
+ t.notOk(response.headers.vary)
976
994
  t.notOk(response.headers['content-encoding'])
977
995
  t.same({ hello: 'world' }, payload)
978
996
  })
@@ -997,8 +1015,8 @@ test('It should not compress :', async (t) => {
997
1015
  }
998
1016
  })
999
1017
  const file = readFileSync('./package.json', 'utf8')
1000
- t.equal(response.headers.vary, 'accept-encoding')
1001
1018
  t.equal(response.statusCode, 200)
1019
+ t.notOk(response.headers.vary)
1002
1020
  t.equal(response.payload, file)
1003
1021
  })
1004
1022
 
@@ -1022,13 +1040,13 @@ test('It should not compress :', async (t) => {
1022
1040
  }
1023
1041
  })
1024
1042
  const file = readFileSync('./package.json', 'utf8')
1025
- t.equal(response.headers.vary, 'accept-encoding')
1026
1043
  t.equal(response.statusCode, 200)
1044
+ t.notOk(response.headers.vary)
1027
1045
  t.equal(response.payload, file)
1028
1046
  })
1029
1047
 
1030
1048
  t.test('when `Accept-Encoding` request header is set to `identity and `inflateIfDeflated` is `true``', async (t) => {
1031
- t.plan(2)
1049
+ t.plan(3)
1032
1050
 
1033
1051
  const fastify = Fastify()
1034
1052
  await fastify.register(compressPlugin, { global: true, inflateIfDeflated: true, threshold: 0 })
@@ -1045,6 +1063,7 @@ test('It should not compress :', async (t) => {
1045
1063
  }
1046
1064
  })
1047
1065
  const payload = JSON.parse(response.payload)
1066
+ t.notOk(response.headers.vary)
1048
1067
  t.notOk(response.headers['content-encoding'])
1049
1068
  t.same({ hello: 'world' }, payload)
1050
1069
  })
@@ -1052,7 +1071,7 @@ test('It should not compress :', async (t) => {
1052
1071
 
1053
1072
  t.test('Using `onSend` hook :', async (t) => {
1054
1073
  t.test('when there is no payload', async (t) => {
1055
- t.plan(3)
1074
+ t.plan(4)
1056
1075
 
1057
1076
  const fastify = Fastify()
1058
1077
  await fastify.register(compressPlugin, { threshold: 0 })
@@ -1069,12 +1088,13 @@ test('It should not compress :', async (t) => {
1069
1088
  }
1070
1089
  })
1071
1090
  t.equal(response.statusCode, 200)
1091
+ t.notOk(response.headers.vary)
1072
1092
  t.notOk(response.headers['content-encoding'])
1073
1093
  t.equal(response.payload, '')
1074
1094
  })
1075
1095
 
1076
1096
  t.test('when payload length is smaller than the `threshold` defined value', async (t) => {
1077
- t.plan(3)
1097
+ t.plan(4)
1078
1098
 
1079
1099
  const fastify = Fastify()
1080
1100
  await fastify.register(compressPlugin, { threshold: 128 })
@@ -1093,12 +1113,13 @@ test('It should not compress :', async (t) => {
1093
1113
  }
1094
1114
  })
1095
1115
  t.equal(response.statusCode, 200)
1116
+ t.notOk(response.headers.vary)
1096
1117
  t.notOk(response.headers['content-encoding'])
1097
1118
  t.equal(response.payload, 'a message')
1098
1119
  })
1099
1120
 
1100
1121
  t.test('when `customTypes` is set and does not match `Content-Type` reply header or `mime-db`', async (t) => {
1101
- t.plan(2)
1122
+ t.plan(3)
1102
1123
 
1103
1124
  const fastify = Fastify()
1104
1125
  await fastify.register(compressPlugin, { customTypes: /x-user-header$/u })
@@ -1116,12 +1137,13 @@ test('It should not compress :', async (t) => {
1116
1137
  'accept-encoding': 'gzip'
1117
1138
  }
1118
1139
  })
1140
+ t.notOk(response.headers.vary)
1119
1141
  t.notOk(response.headers['content-encoding'])
1120
1142
  t.equal(response.statusCode, 200)
1121
1143
  })
1122
1144
 
1123
1145
  t.test('when `X-No-Compression` request header is `true`', async (t) => {
1124
- t.plan(3)
1146
+ t.plan(4)
1125
1147
 
1126
1148
  const fastify = Fastify()
1127
1149
  await fastify.register(compressPlugin, { threshold: 0 })
@@ -1139,12 +1161,13 @@ test('It should not compress :', async (t) => {
1139
1161
  }
1140
1162
  })
1141
1163
  t.equal(response.statusCode, 200)
1164
+ t.notOk(response.headers.vary)
1142
1165
  t.notOk(response.headers['content-encoding'])
1143
1166
  t.same(JSON.parse(response.payload), json)
1144
1167
  })
1145
1168
 
1146
1169
  t.test('when `Content-Type` reply header is not set and the content is not detected as a compressible type', async (t) => {
1147
- t.plan(2)
1170
+ t.plan(3)
1148
1171
 
1149
1172
  const fastify = Fastify()
1150
1173
  await fastify.register(compressPlugin, { threshold: 0 })
@@ -1167,12 +1190,13 @@ test('It should not compress :', async (t) => {
1167
1190
  'accept-encoding': 'identity'
1168
1191
  }
1169
1192
  })
1193
+ t.notOk(response.headers.vary)
1170
1194
  t.notOk(response.headers['content-encoding'])
1171
1195
  t.equal(response.payload, JSON.stringify(json))
1172
1196
  })
1173
1197
 
1174
1198
  t.test('when `Content-Type` reply header is a mime type with undefined compressible values', async (t) => {
1175
- t.plan(3)
1199
+ t.plan(4)
1176
1200
 
1177
1201
  const fastify = Fastify()
1178
1202
  await fastify.register(compressPlugin, { threshold: 0 })
@@ -1191,12 +1215,13 @@ test('It should not compress :', async (t) => {
1191
1215
  }
1192
1216
  })
1193
1217
  t.equal(response.statusCode, 200)
1218
+ t.notOk(response.headers.vary)
1194
1219
  t.notOk(response.headers['content-encoding'])
1195
1220
  t.equal(response.payload, 'hello')
1196
1221
  })
1197
1222
 
1198
1223
  t.test('when `Content-Type` reply header value is `text/event-stream`', async (t) => {
1199
- t.plan(3)
1224
+ t.plan(4)
1200
1225
 
1201
1226
  const fastify = Fastify()
1202
1227
  await fastify.register(compressPlugin, { threshold: 0 })
@@ -1218,12 +1243,13 @@ test('It should not compress :', async (t) => {
1218
1243
  method: 'GET'
1219
1244
  })
1220
1245
  t.equal(response.statusCode, 200)
1246
+ t.notOk(response.headers.vary)
1221
1247
  t.notOk(response.headers['content-encoding'])
1222
1248
  t.same(response.payload, 'event: open\n\nevent: change\ndata: schema\n\n')
1223
1249
  })
1224
1250
 
1225
1251
  t.test('when `Content-Type` reply header value is an invalid type', async (t) => {
1226
- t.plan(3)
1252
+ t.plan(4)
1227
1253
 
1228
1254
  const fastify = Fastify()
1229
1255
  await fastify.register(compressPlugin, { threshold: 0 })
@@ -1242,6 +1268,7 @@ test('It should not compress :', async (t) => {
1242
1268
  }
1243
1269
  })
1244
1270
  t.equal(response.statusCode, 200)
1271
+ t.notOk(response.headers.vary)
1245
1272
  t.notOk(response.headers['content-encoding'])
1246
1273
  t.equal(response.payload, 'a message')
1247
1274
  })
@@ -1262,13 +1289,13 @@ test('It should not compress :', async (t) => {
1262
1289
  url: '/',
1263
1290
  method: 'GET'
1264
1291
  })
1265
- t.equal(response.headers.vary, 'accept-encoding')
1266
1292
  t.equal(response.statusCode, 200)
1293
+ t.notOk(response.headers.vary)
1267
1294
  t.notOk(response.headers['content-encoding'])
1268
1295
  })
1269
1296
 
1270
1297
  t.test('when `Accept-Encoding` request header is set to `identity`', async (t) => {
1271
- t.plan(2)
1298
+ t.plan(3)
1272
1299
 
1273
1300
  const fastify = Fastify()
1274
1301
  await fastify.register(compressPlugin, { global: true, threshold: 0 })
@@ -1285,6 +1312,7 @@ test('It should not compress :', async (t) => {
1285
1312
  }
1286
1313
  })
1287
1314
  const payload = JSON.parse(response.payload)
1315
+ t.notOk(response.headers.vary)
1288
1316
  t.notOk(response.headers['content-encoding'])
1289
1317
  t.same({ hello: 'world' }, payload)
1290
1318
  })
@@ -1332,13 +1360,13 @@ test('It should not compress :', async (t) => {
1332
1360
  }
1333
1361
  })
1334
1362
  const file = readFileSync('./package.json', 'utf8')
1335
- t.equal(response.headers.vary, 'accept-encoding')
1336
1363
  t.equal(response.statusCode, 200)
1364
+ t.notOk(response.headers.vary)
1337
1365
  t.equal(response.payload, file)
1338
1366
  })
1339
1367
 
1340
1368
  t.test('when `Accept-Encoding` request header is set to `identity and `inflateIfDeflated` is `true``', async (t) => {
1341
- t.plan(2)
1369
+ t.plan(3)
1342
1370
 
1343
1371
  const fastify = Fastify()
1344
1372
  await fastify.register(compressPlugin, { global: true, inflateIfDeflated: true, threshold: 0 })
@@ -1355,6 +1383,7 @@ test('It should not compress :', async (t) => {
1355
1383
  }
1356
1384
  })
1357
1385
  const payload = JSON.parse(response.payload)
1386
+ t.notOk(response.headers.vary)
1358
1387
  t.notOk(response.headers['content-encoding'])
1359
1388
  t.same({ hello: 'world' }, payload)
1360
1389
  })
@@ -1363,7 +1392,7 @@ test('It should not compress :', async (t) => {
1363
1392
 
1364
1393
  test('It should not double-compress :', async (t) => {
1365
1394
  t.test('when using `reply.compress()` to send an already deflated Stream', async (t) => {
1366
- t.plan(2)
1395
+ t.plan(3)
1367
1396
 
1368
1397
  const fastify = Fastify()
1369
1398
  await fastify.register(compressPlugin, { global: true })
@@ -1385,12 +1414,13 @@ test('It should not double-compress :', async (t) => {
1385
1414
  })
1386
1415
  const file = readFileSync('./package.json', 'utf8')
1387
1416
  const payload = zlib.inflateSync(response.rawPayload)
1417
+ t.equal(response.headers.vary, 'accept-encoding')
1388
1418
  t.equal(response.headers['content-encoding'], 'deflate')
1389
1419
  t.equal(payload.toString('utf-8'), file)
1390
1420
  })
1391
1421
 
1392
1422
  t.test('when using `reply.compress()` to send an already gzipped Stream', async (t) => {
1393
- t.plan(2)
1423
+ t.plan(3)
1394
1424
 
1395
1425
  const fastify = Fastify()
1396
1426
  await fastify.register(compressPlugin, { global: true })
@@ -1412,12 +1442,13 @@ test('It should not double-compress :', async (t) => {
1412
1442
  })
1413
1443
  const file = readFileSync('./package.json', 'utf8')
1414
1444
  const payload = zlib.gunzipSync(response.rawPayload)
1445
+ t.equal(response.headers.vary, 'accept-encoding')
1415
1446
  t.equal(response.headers['content-encoding'], 'gzip')
1416
1447
  t.equal(payload.toString('utf-8'), file)
1417
1448
  })
1418
1449
 
1419
1450
  t.test('when using `onSend` hook to send an already brotli compressed Stream', async (t) => {
1420
- t.plan(3)
1451
+ t.plan(4)
1421
1452
 
1422
1453
  const fastify = Fastify()
1423
1454
  await fastify.register(compressPlugin, { global: true, threshold: 0 })
@@ -1437,17 +1468,18 @@ test('It should not double-compress :', async (t) => {
1437
1468
  url: '/',
1438
1469
  method: 'GET',
1439
1470
  headers: {
1440
- 'accept-encoding': 'br,gzip,deflate'
1471
+ 'accept-encoding': 'br'
1441
1472
  }
1442
1473
  })
1443
1474
  const payload = zlib.brotliDecompressSync(response.rawPayload)
1475
+ t.notOk(response.headers.vary)
1444
1476
  t.equal(response.headers['content-encoding'], 'br')
1445
1477
  t.equal(response.headers['content-length'], response.rawPayload.length.toString())
1446
1478
  t.equal(payload.toString('utf-8'), file)
1447
1479
  })
1448
1480
 
1449
1481
  t.test('when using `onSend` hook to send an already deflated Stream', async (t) => {
1450
- t.plan(3)
1482
+ t.plan(4)
1451
1483
 
1452
1484
  const fastify = Fastify()
1453
1485
  await fastify.register(compressPlugin, { global: true, threshold: 0 })
@@ -1467,17 +1499,18 @@ test('It should not double-compress :', async (t) => {
1467
1499
  url: '/',
1468
1500
  method: 'GET',
1469
1501
  headers: {
1470
- 'accept-encoding': 'br,gzip,deflate'
1502
+ 'accept-encoding': 'deflate'
1471
1503
  }
1472
1504
  })
1473
1505
  const payload = zlib.inflateSync(response.rawPayload)
1506
+ t.notOk(response.headers.vary)
1474
1507
  t.equal(response.headers['content-encoding'], 'deflate')
1475
1508
  t.equal(response.headers['content-length'], response.rawPayload.length.toString())
1476
1509
  t.equal(payload.toString('utf-8'), file)
1477
1510
  })
1478
1511
 
1479
1512
  t.test('when using `onSend` hook to send an already gzipped Stream', async (t) => {
1480
- t.plan(3)
1513
+ t.plan(4)
1481
1514
 
1482
1515
  const fastify = Fastify()
1483
1516
  await fastify.register(compressPlugin, { global: true, threshold: 0 })
@@ -1501,6 +1534,7 @@ test('It should not double-compress :', async (t) => {
1501
1534
  }
1502
1535
  })
1503
1536
  const payload = zlib.gunzipSync(response.rawPayload)
1537
+ t.notOk(response.headers.vary)
1504
1538
  t.equal(response.headers['content-encoding'], 'gzip')
1505
1539
  t.equal(response.headers['content-length'], response.rawPayload.length.toString())
1506
1540
  t.equal(payload.toString('utf-8'), file)
@@ -1510,7 +1544,7 @@ test('It should not double-compress :', async (t) => {
1510
1544
  test('It should not compress Stream data and add a `Content-Encoding` reply header :', async (t) => {
1511
1545
  t.test('Using `onSend` hook if `Accept-Encoding` request header value is `identity`', async (t) => {
1512
1546
  t.test('when `inflateIfDeflated` is `true` and `encodings` is not set', async (t) => {
1513
- t.plan(3)
1547
+ t.plan(4)
1514
1548
 
1515
1549
  const fastify = Fastify()
1516
1550
  await fastify.register(compressPlugin, { global: true, inflateIfDeflated: true })
@@ -1530,12 +1564,13 @@ test('It should not compress Stream data and add a `Content-Encoding` reply head
1530
1564
  })
1531
1565
  const file = readFileSync('./package.json', 'utf8')
1532
1566
  t.equal(response.statusCode, 200)
1567
+ t.notOk(response.headers.vary)
1533
1568
  t.equal(response.headers['content-encoding'], 'identity')
1534
1569
  t.equal(file, response.payload)
1535
1570
  })
1536
1571
 
1537
1572
  t.test('when `inflateIfDeflated` is `true` and `encodings` is set', async (t) => {
1538
- t.plan(3)
1573
+ t.plan(4)
1539
1574
 
1540
1575
  const fastify = Fastify()
1541
1576
  await fastify.register(compressPlugin, {
@@ -1558,6 +1593,7 @@ test('It should not compress Stream data and add a `Content-Encoding` reply head
1558
1593
  })
1559
1594
  const file = readFileSync('./package.json', 'utf-8')
1560
1595
  t.equal(response.statusCode, 200)
1596
+ t.notOk(response.headers.vary)
1561
1597
  t.equal(response.headers['content-encoding'], 'identity')
1562
1598
  t.same(response.payload, file)
1563
1599
  })
@@ -1565,7 +1601,7 @@ test('It should not compress Stream data and add a `Content-Encoding` reply head
1565
1601
 
1566
1602
  t.test('Using `reply.compress()` if `Accept-Encoding` request header value is `identity`', async (t) => {
1567
1603
  t.test('when `inflateIfDeflated` is `true` and `encodings` is not set', async (t) => {
1568
- t.plan(3)
1604
+ t.plan(4)
1569
1605
 
1570
1606
  const fastify = Fastify()
1571
1607
  await fastify.register(compressPlugin, { global: true, inflateIfDeflated: true })
@@ -1585,12 +1621,13 @@ test('It should not compress Stream data and add a `Content-Encoding` reply head
1585
1621
  })
1586
1622
  const file = readFileSync('./package.json', 'utf8')
1587
1623
  t.equal(response.statusCode, 200)
1624
+ t.notOk(response.headers.vary)
1588
1625
  t.equal(response.headers['content-encoding'], 'identity')
1589
1626
  t.equal(file, response.payload)
1590
1627
  })
1591
1628
 
1592
1629
  t.test('when `inflateIfDeflated` is `true` and `encodings` is set', async (t) => {
1593
- t.plan(3)
1630
+ t.plan(4)
1594
1631
 
1595
1632
  const fastify = Fastify()
1596
1633
  await fastify.register(compressPlugin, {
@@ -1613,6 +1650,7 @@ test('It should not compress Stream data and add a `Content-Encoding` reply head
1613
1650
  })
1614
1651
  const file = readFileSync('./package.json', 'utf-8')
1615
1652
  t.equal(response.statusCode, 200)
1653
+ t.notOk(response.headers.vary)
1616
1654
  t.equal(response.headers['content-encoding'], 'identity')
1617
1655
  t.same(response.payload, file)
1618
1656
  })
@@ -1620,7 +1658,7 @@ test('It should not compress Stream data and add a `Content-Encoding` reply head
1620
1658
  })
1621
1659
 
1622
1660
  test('It should return a serialized payload when `inflateIfDeflated` is `true` and `X-No-Compression` request header is `true`', async (t) => {
1623
- t.plan(6)
1661
+ t.plan(8)
1624
1662
 
1625
1663
  const fastify = Fastify()
1626
1664
  await fastify.register(compressPlugin, {
@@ -1648,6 +1686,7 @@ test('It should return a serialized payload when `inflateIfDeflated` is `true` a
1648
1686
  }
1649
1687
  })
1650
1688
  t.equal(one.statusCode, 200)
1689
+ t.notOk(one.headers.vary)
1651
1690
  t.notOk(one.headers['content-encoding'])
1652
1691
  t.same(JSON.parse(one.payload), json)
1653
1692
 
@@ -1659,6 +1698,7 @@ test('It should return a serialized payload when `inflateIfDeflated` is `true` a
1659
1698
  }
1660
1699
  })
1661
1700
  t.equal(two.statusCode, 200)
1701
+ t.notOk(two.headers.vary)
1662
1702
  t.notOk(two.headers['content-encoding'])
1663
1703
  t.equal(two.payload, compressedBufferPayload.toString())
1664
1704
  })
@@ -1737,7 +1777,7 @@ test('It should log an existing error with stream onEnd handler', async (t) => {
1737
1777
 
1738
1778
  test('It should support stream1 :', async (t) => {
1739
1779
  t.test('when using `reply.compress()`', async (t) => {
1740
- t.plan(2)
1780
+ t.plan(3)
1741
1781
 
1742
1782
  const fastify = Fastify()
1743
1783
  await fastify.register(compressPlugin, { global: true, threshold: 0 })
@@ -1761,12 +1801,13 @@ test('It should support stream1 :', async (t) => {
1761
1801
  }
1762
1802
  })
1763
1803
  const payload = zlib.gunzipSync(response.rawPayload)
1804
+ t.equal(response.headers.vary, 'accept-encoding')
1764
1805
  t.equal(response.headers['content-encoding'], 'gzip')
1765
1806
  t.same(JSON.parse(payload.toString()), [{ hello: 'world' }, { a: 42 }])
1766
1807
  })
1767
1808
 
1768
1809
  t.test('when using `onSend` hook', async (t) => {
1769
- t.plan(2)
1810
+ t.plan(3)
1770
1811
 
1771
1812
  const fastify = Fastify()
1772
1813
  await fastify.register(compressPlugin, { global: true, threshold: 0 })
@@ -1790,6 +1831,7 @@ test('It should support stream1 :', async (t) => {
1790
1831
  }
1791
1832
  })
1792
1833
  const payload = zlib.gunzipSync(response.rawPayload)
1834
+ t.equal(response.headers.vary, 'accept-encoding')
1793
1835
  t.equal(response.headers['content-encoding'], 'gzip')
1794
1836
  t.same(JSON.parse(payload.toString()), [{ hello: 'world' }, { a: 42 }])
1795
1837
  })
@@ -1797,7 +1839,7 @@ test('It should support stream1 :', async (t) => {
1797
1839
 
1798
1840
  test('It should remove `Content-Length` header :', async (t) => {
1799
1841
  t.test('using `reply.compress()`', async (t) => {
1800
- t.plan(3)
1842
+ t.plan(4)
1801
1843
 
1802
1844
  const fastify = Fastify()
1803
1845
  await fastify.register(compressPlugin, { global: true })
@@ -1824,13 +1866,14 @@ test('It should remove `Content-Length` header :', async (t) => {
1824
1866
  })
1825
1867
  const file = readFileSync('./package.json', 'utf8')
1826
1868
  const payload = zlib.inflateSync(response.rawPayload)
1869
+ t.equal(response.headers.vary, 'accept-encoding')
1827
1870
  t.equal(response.headers['content-encoding'], 'deflate')
1828
1871
  t.notOk(response.headers['content-length'], 'no content length')
1829
1872
  t.equal(payload.toString('utf-8'), file)
1830
1873
  })
1831
1874
 
1832
1875
  t.test('using `reply.compress()` on a Stream when `inflateIfDeflated` is `true` and `X-No-Compression` request header is `true`', async (t) => {
1833
- t.plan(3)
1876
+ t.plan(4)
1834
1877
 
1835
1878
  const fastify = Fastify()
1836
1879
  await fastify.register(compressPlugin, { global: true, inflateIfDeflated: true })
@@ -1850,12 +1893,13 @@ test('It should remove `Content-Length` header :', async (t) => {
1850
1893
  })
1851
1894
  const file = readFileSync('./package.json', 'utf8')
1852
1895
  t.equal(response.statusCode, 200)
1896
+ t.notOk(response.headers.vary)
1853
1897
  t.notOk(response.headers['content-length'], 'no content length')
1854
1898
  t.equal(file, response.payload)
1855
1899
  })
1856
1900
 
1857
1901
  t.test('using `onSend` hook', async (t) => {
1858
- t.plan(3)
1902
+ t.plan(4)
1859
1903
 
1860
1904
  const fastify = Fastify()
1861
1905
  await fastify.register(compressPlugin, { global: true })
@@ -1882,13 +1926,14 @@ test('It should remove `Content-Length` header :', async (t) => {
1882
1926
  })
1883
1927
  const file = readFileSync('./package.json', 'utf8')
1884
1928
  const payload = zlib.inflateSync(response.rawPayload)
1929
+ t.equal(response.headers.vary, 'accept-encoding')
1885
1930
  t.equal(response.headers['content-encoding'], 'deflate')
1886
1931
  t.notOk(response.headers['content-length'], 'no content length')
1887
1932
  t.equal(payload.toString('utf-8'), file)
1888
1933
  })
1889
1934
 
1890
1935
  t.test('using `onSend` hook on a Stream When `inflateIfDeflated` is `true` and `X-No-Compression` request header is `true`', async (t) => {
1891
- t.plan(3)
1936
+ t.plan(4)
1892
1937
 
1893
1938
  const fastify = Fastify()
1894
1939
  await fastify.register(compressPlugin, { global: true, inflateIfDeflated: true })
@@ -1908,6 +1953,7 @@ test('It should remove `Content-Length` header :', async (t) => {
1908
1953
  })
1909
1954
  const file = readFileSync('./package.json', 'utf8')
1910
1955
  t.equal(response.statusCode, 200)
1956
+ t.notOk(response.headers.vary)
1911
1957
  t.notOk(response.headers['content-length'], 'no content length')
1912
1958
  t.equal(file, response.payload)
1913
1959
  })
@@ -1915,7 +1961,7 @@ test('It should remove `Content-Length` header :', async (t) => {
1915
1961
 
1916
1962
  test('When `removeContentLengthHeader` is `false`, it should not remove `Content-Length` header :', async (t) => {
1917
1963
  t.test('using `reply.compress()`', async (t) => {
1918
- t.plan(3)
1964
+ t.plan(4)
1919
1965
 
1920
1966
  const fastify = Fastify()
1921
1967
  await fastify.register(compressPlugin, { global: true, removeContentLengthHeader: false })
@@ -1942,13 +1988,14 @@ test('When `removeContentLengthHeader` is `false`, it should not remove `Content
1942
1988
  })
1943
1989
  const file = readFileSync('./package.json', 'utf8')
1944
1990
  const payload = zlib.inflateSync(response.rawPayload)
1991
+ t.equal(response.headers.vary, 'accept-encoding')
1945
1992
  t.equal(response.headers['content-encoding'], 'deflate')
1946
1993
  t.equal(response.headers['content-length'], payload.length.toString())
1947
1994
  t.equal(payload.toString('utf-8'), file)
1948
1995
  })
1949
1996
 
1950
1997
  t.test('using `onSend` hook', async (t) => {
1951
- t.plan(3)
1998
+ t.plan(4)
1952
1999
 
1953
2000
  const fastify = Fastify()
1954
2001
  await fastify.register(compressPlugin, { global: true, removeContentLengthHeader: false })
@@ -1975,6 +2022,7 @@ test('When `removeContentLengthHeader` is `false`, it should not remove `Content
1975
2022
  })
1976
2023
  const file = readFileSync('./package.json', 'utf8')
1977
2024
  const payload = zlib.inflateSync(response.rawPayload)
2025
+ t.equal(response.headers.vary, 'accept-encoding')
1978
2026
  t.equal(response.headers['content-encoding'], 'deflate')
1979
2027
  t.equal(response.headers['content-length'], payload.length.toString())
1980
2028
  t.equal(payload.toString('utf-8'), file)
@@ -2317,7 +2365,7 @@ test('When `Accept-Encoding` request header values are not supported and `onUnsu
2317
2365
 
2318
2366
  test('`Accept-Encoding` request header values :', async (t) => {
2319
2367
  t.test('can contain white space', async (t) => {
2320
- t.plan(2)
2368
+ t.plan(3)
2321
2369
 
2322
2370
  const fastify = Fastify()
2323
2371
  await fastify.register(compressPlugin, { threshold: 0 })
@@ -2336,12 +2384,13 @@ test('`Accept-Encoding` request header values :', async (t) => {
2336
2384
  }
2337
2385
  })
2338
2386
  const payload = zlib.gunzipSync(response.rawPayload)
2387
+ t.equal(response.headers.vary, 'accept-encoding')
2339
2388
  t.equal(response.headers['content-encoding'], 'gzip')
2340
2389
  t.equal(payload.toString('utf-8'), JSON.stringify(json))
2341
2390
  })
2342
2391
 
2343
2392
  t.test('can contain mixed uppercase and lowercase characters (e.g.: compressing a Stream)', async (t) => {
2344
- t.plan(2)
2393
+ t.plan(3)
2345
2394
 
2346
2395
  const fastify = Fastify()
2347
2396
  await fastify.register(compressPlugin, { global: true })
@@ -2361,6 +2410,7 @@ test('`Accept-Encoding` request header values :', async (t) => {
2361
2410
  })
2362
2411
  const file = readFileSync('./package.json', 'utf8')
2363
2412
  const payload = zlib.gunzipSync(response.rawPayload)
2413
+ t.equal(response.headers.vary, 'accept-encoding')
2364
2414
  t.equal(response.headers['content-encoding'], 'gzip')
2365
2415
  t.equal(payload.toString('utf-8'), file)
2366
2416
  })
@@ -2388,7 +2438,7 @@ test('`Accept-Encoding` request header values :', async (t) => {
2388
2438
  })
2389
2439
 
2390
2440
  t.test('should support `gzip` alias value `x-gzip`', async (t) => {
2391
- t.plan(2)
2441
+ t.plan(3)
2392
2442
 
2393
2443
  const fastify = Fastify()
2394
2444
  await fastify.register(compressPlugin, { global: true })
@@ -2410,12 +2460,13 @@ test('`Accept-Encoding` request header values :', async (t) => {
2410
2460
  })
2411
2461
  const file = readFileSync('./package.json', 'utf8')
2412
2462
  const payload = zlib.gunzipSync(response.rawPayload)
2463
+ t.equal(response.headers.vary, 'accept-encoding')
2413
2464
  t.equal(response.headers['content-encoding'], 'gzip')
2414
2465
  t.equal(payload.toString('utf-8'), file)
2415
2466
  })
2416
2467
 
2417
2468
  t.test('should support quality syntax', async (t) => {
2418
- t.plan(2)
2469
+ t.plan(3)
2419
2470
 
2420
2471
  const fastify = Fastify()
2421
2472
  await fastify.register(compressPlugin, { global: true })
@@ -2437,13 +2488,14 @@ test('`Accept-Encoding` request header values :', async (t) => {
2437
2488
  })
2438
2489
  const file = readFileSync('./package.json', 'utf8')
2439
2490
  const payload = zlib.inflateSync(response.rawPayload)
2491
+ t.equal(response.headers.vary, 'accept-encoding')
2440
2492
  t.equal(response.headers['content-encoding'], 'deflate')
2441
2493
  t.equal(payload.toString('utf-8'), file)
2442
2494
  })
2443
2495
  })
2444
2496
 
2445
2497
  test('It should compress data if `customTypes` is set and matches `Content-Type` reply header value', async (t) => {
2446
- t.plan(2)
2498
+ t.plan(3)
2447
2499
  const fastify = Fastify()
2448
2500
  await fastify.register(compressPlugin, { customTypes: /x-user-header$/u })
2449
2501
 
@@ -2462,12 +2514,13 @@ test('It should compress data if `customTypes` is set and matches `Content-Type`
2462
2514
  })
2463
2515
  const file = readFileSync('./package.json', 'utf8')
2464
2516
  const payload = zlib.gunzipSync(response.rawPayload)
2517
+ t.equal(response.headers.vary, 'accept-encoding')
2465
2518
  t.equal(response.headers['content-encoding'], 'gzip')
2466
2519
  t.equal(payload.toString('utf-8'), file)
2467
2520
  })
2468
2521
 
2469
2522
  test('It should compress data if `customTypes` is a function and returns true on the provided `Content-Type` reply header value', async (t) => {
2470
- t.plan(2)
2523
+ t.plan(3)
2471
2524
  const fastify = Fastify()
2472
2525
  await fastify.register(compressPlugin, { customTypes: value => value === 'application/x-user-header' })
2473
2526
 
@@ -2486,12 +2539,13 @@ test('It should compress data if `customTypes` is a function and returns true on
2486
2539
  })
2487
2540
  const file = readFileSync('./package.json', 'utf8')
2488
2541
  const payload = zlib.gunzipSync(response.rawPayload)
2542
+ t.equal(response.headers.vary, 'accept-encoding')
2489
2543
  t.equal(response.headers['content-encoding'], 'gzip')
2490
2544
  t.equal(payload.toString('utf-8'), file)
2491
2545
  })
2492
2546
 
2493
2547
  test('It should not apply `customTypes` option if the passed value is not a RegExp or Function', async (t) => {
2494
- t.plan(2)
2548
+ t.plan(3)
2495
2549
 
2496
2550
  const fastify = Fastify()
2497
2551
  await fastify.register(compressPlugin, { customTypes: 'x-user-header' })
@@ -2509,12 +2563,13 @@ test('It should not apply `customTypes` option if the passed value is not a RegE
2509
2563
  'accept-encoding': 'gzip'
2510
2564
  }
2511
2565
  })
2566
+ t.notOk(response.headers.vary)
2512
2567
  t.notOk(response.headers['content-encoding'])
2513
2568
  t.equal(response.statusCode, 200)
2514
2569
  })
2515
2570
 
2516
2571
  test('When `encodings` option is set, it should only use the registered value', async (t) => {
2517
- t.plan(2)
2572
+ t.plan(3)
2518
2573
 
2519
2574
  const fastify = Fastify()
2520
2575
  await fastify.register(compressPlugin, { encodings: ['deflate'] })
@@ -2530,45 +2585,80 @@ test('When `encodings` option is set, it should only use the registered value',
2530
2585
  'accept-encoding': 'br,gzip,deflate'
2531
2586
  }
2532
2587
  })
2588
+ t.equal(response.headers.vary, 'accept-encoding')
2533
2589
  t.equal(response.headers['content-encoding'], 'deflate')
2534
2590
  t.equal(response.statusCode, 200)
2535
2591
  })
2536
2592
 
2537
- test('It should send data compressed according to `brotliOptions`', async (t) => {
2538
- t.plan(2)
2539
- const brotliOptions = {
2540
- params: {
2541
- [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
2542
- [zlib.constants.BROTLI_PARAM_QUALITY]: 4
2593
+ test('It should send data compressed according to `brotliOptions` :', async (t) => {
2594
+ t.test('when using br encoding', async (t) => {
2595
+ t.plan(4)
2596
+ const brotliOptions = {
2597
+ params: {
2598
+ [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
2599
+ [zlib.constants.BROTLI_PARAM_QUALITY]: 8
2600
+ }
2543
2601
  }
2544
- }
2545
2602
 
2546
- const fastify = Fastify()
2547
- await fastify.register(compressPlugin, { global: true, brotliOptions })
2603
+ const fastify = Fastify()
2604
+ await fastify.register(compressPlugin, { global: true, brotliOptions })
2548
2605
 
2549
- fastify.get('/', (request, reply) => {
2550
- reply
2551
- .type('text/plain')
2552
- .compress(createReadStream('./package.json'))
2606
+ fastify.get('/', (request, reply) => {
2607
+ reply
2608
+ .type('text/plain')
2609
+ .compress(createReadStream('./package.json'))
2610
+ })
2611
+
2612
+ const response = await fastify.inject({
2613
+ url: '/',
2614
+ method: 'GET',
2615
+ headers: {
2616
+ 'accept-encoding': 'br'
2617
+ }
2618
+ })
2619
+
2620
+ const file = readFileSync('./package.json', 'utf8')
2621
+ const payload = zlib.brotliDecompressSync(response.rawPayload, brotliOptions)
2622
+ t.equal(response.headers.vary, 'accept-encoding')
2623
+ t.equal(response.headers['content-encoding'], 'br')
2624
+ t.equal(payload.toString('utf-8'), file)
2625
+
2626
+ const compressedPayload = zlib.brotliCompressSync(file, brotliOptions)
2627
+ t.same(response.rawPayload, compressedPayload)
2553
2628
  })
2554
2629
 
2555
- const response = await fastify.inject({
2556
- url: '/',
2557
- method: 'GET',
2558
- headers: {
2559
- 'accept-encoding': 'br'
2630
+ t.test('default BROTLI_PARAM_QUALITY to be 4', async (t) => {
2631
+ t.plan(1)
2632
+
2633
+ const fastify = Fastify()
2634
+ await fastify.register(compressPlugin, { global: true })
2635
+
2636
+ const file = readFileSync('./package.json', 'utf8')
2637
+ fastify.get('/', (request, reply) => {
2638
+ reply
2639
+ .type('text/plain')
2640
+ .compress(file)
2641
+ })
2642
+
2643
+ const response = await fastify.inject({
2644
+ url: '/',
2645
+ method: 'GET',
2646
+ headers: {
2647
+ 'accept-encoding': 'br'
2648
+ }
2649
+ })
2650
+
2651
+ const defaultBrotliOptions = {
2652
+ params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 4 }
2560
2653
  }
2654
+ const compressedPayload = zlib.brotliCompressSync(file, defaultBrotliOptions)
2655
+ t.same(response.rawPayload, compressedPayload)
2561
2656
  })
2562
-
2563
- const file = readFileSync('./package.json', 'utf8')
2564
- const payload = zlib.brotliDecompressSync(response.rawPayload, brotliOptions)
2565
- t.equal(response.headers['content-encoding'], 'br')
2566
- t.equal(payload.toString('utf-8'), file)
2567
2657
  })
2568
2658
 
2569
2659
  test('It should send data compressed according to `zlibOptions` :', async (t) => {
2570
2660
  t.test('when using deflate encoding', async (t) => {
2571
- t.plan(2)
2661
+ t.plan(3)
2572
2662
 
2573
2663
  const zlibOptions = {
2574
2664
  level: 1,
@@ -2595,12 +2685,13 @@ test('It should send data compressed according to `zlibOptions` :', async (t) =>
2595
2685
  }
2596
2686
  })
2597
2687
  const file = readFileSync('./package.json')
2688
+ t.equal(response.headers.vary, 'accept-encoding')
2598
2689
  t.equal(response.headers['content-encoding'], 'deflate')
2599
2690
  t.same(response.rawPayload, zlib.deflateSync(file, zlibOptions))
2600
2691
  })
2601
2692
 
2602
2693
  t.test('when using gzip encoding', async (t) => {
2603
- t.plan(2)
2694
+ t.plan(3)
2604
2695
 
2605
2696
  const zlibOptions = { level: 1 }
2606
2697
 
@@ -2624,6 +2715,7 @@ test('It should send data compressed according to `zlibOptions` :', async (t) =>
2624
2715
  }
2625
2716
  })
2626
2717
  const file = readFileSync('./package.json')
2718
+ t.equal(response.headers.vary, 'accept-encoding')
2627
2719
  t.equal(response.headers['content-encoding'], 'gzip')
2628
2720
  t.same(response.rawPayload, zlib.gzipSync(file, zlibOptions))
2629
2721
  })
@@ -2720,7 +2812,7 @@ test('It should not add `accept-encoding` to `Vary` reply header if already pres
2720
2812
  })
2721
2813
 
2722
2814
  test('It should follow the `Accept-Encoding` request header encoding order', async (t) => {
2723
- t.plan(2)
2815
+ t.plan(3)
2724
2816
 
2725
2817
  const fastify = Fastify()
2726
2818
  await fastify.register(compressPlugin, { global: true })
@@ -2740,12 +2832,13 @@ test('It should follow the `Accept-Encoding` request header encoding order', asy
2740
2832
  })
2741
2833
  const file = readFileSync('./package.json', 'utf8')
2742
2834
  const payload = zlib.brotliDecompressSync(response.rawPayload)
2835
+ t.equal(response.headers.vary, 'accept-encoding')
2743
2836
  t.equal(response.headers['content-encoding'], 'br')
2744
2837
  t.equal(payload.toString('utf-8'), file)
2745
2838
  })
2746
2839
 
2747
2840
  test('It should sort and follow custom `encodings` options', async (t) => {
2748
- t.plan(2)
2841
+ t.plan(3)
2749
2842
 
2750
2843
  const fastify = Fastify()
2751
2844
  await fastify.register(compressPlugin, {
@@ -2768,12 +2861,13 @@ test('It should sort and follow custom `encodings` options', async (t) => {
2768
2861
  })
2769
2862
  const file = readFileSync('./package.json', 'utf8')
2770
2863
  const payload = zlib.brotliDecompressSync(response.rawPayload)
2864
+ t.equal(response.headers.vary, 'accept-encoding')
2771
2865
  t.equal(response.headers['content-encoding'], 'br')
2772
2866
  t.equal(payload.toString('utf-8'), file)
2773
2867
  })
2774
2868
 
2775
2869
  test('It should sort and prefer the order of custom `encodings` options', async (t) => {
2776
- t.plan(2)
2870
+ t.plan(3)
2777
2871
 
2778
2872
  const fastify = Fastify()
2779
2873
  await fastify.register(compressPlugin, {
@@ -2797,12 +2891,13 @@ test('It should sort and prefer the order of custom `encodings` options', async
2797
2891
 
2798
2892
  const file = readFileSync('./package.json', 'utf8')
2799
2893
  const payload = zlib.gunzipSync(response.rawPayload)
2894
+ t.equal(response.headers.vary, 'accept-encoding')
2800
2895
  t.equal(response.headers['content-encoding'], 'gzip')
2801
2896
  t.equal(payload.toString('utf-8'), file)
2802
2897
  })
2803
2898
 
2804
2899
  test('It should sort and follow custom `requestEncodings` options', async (t) => {
2805
- t.plan(2)
2900
+ t.plan(3)
2806
2901
 
2807
2902
  const fastify = Fastify()
2808
2903
  await fastify.register(compressPlugin, {
@@ -2825,13 +2920,14 @@ test('It should sort and follow custom `requestEncodings` options', async (t) =>
2825
2920
  })
2826
2921
  const file = readFileSync('./package.json', 'utf8')
2827
2922
  const payload = zlib.brotliDecompressSync(response.rawPayload)
2923
+ t.equal(response.headers.vary, 'accept-encoding')
2828
2924
  t.equal(response.headers['content-encoding'], 'br')
2829
2925
  t.equal(payload.toString('utf-8'), file)
2830
2926
  })
2831
2927
 
2832
2928
  test('It should uncompress data when `Accept-Encoding` request header is missing :', async (t) => {
2833
2929
  t.test('using the fallback Node.js `zlib.createInflate()` method', async (t) => {
2834
- t.plan(3)
2930
+ t.plan(4)
2835
2931
 
2836
2932
  const fastify = Fastify()
2837
2933
  await fastify.register(compressPlugin, {
@@ -2851,12 +2947,13 @@ test('It should uncompress data when `Accept-Encoding` request header is missing
2851
2947
  method: 'GET'
2852
2948
  })
2853
2949
  t.equal(response.statusCode, 200)
2950
+ t.notOk(response.headers.vary)
2854
2951
  t.notOk(response.headers['content-encoding'])
2855
2952
  t.same(JSON.parse('' + response.payload), json)
2856
2953
  })
2857
2954
 
2858
2955
  t.test('using the fallback Node.js `zlib.createGunzip()` method', async (t) => {
2859
- t.plan(3)
2956
+ t.plan(4)
2860
2957
 
2861
2958
  const fastify = Fastify()
2862
2959
  await fastify.register(compressPlugin, {
@@ -2876,12 +2973,13 @@ test('It should uncompress data when `Accept-Encoding` request header is missing
2876
2973
  method: 'GET'
2877
2974
  })
2878
2975
  t.equal(response.statusCode, 200)
2976
+ t.notOk(response.headers.vary)
2879
2977
  t.notOk(response.headers['content-encoding'])
2880
2978
  t.same(JSON.parse('' + response.payload), json)
2881
2979
  })
2882
2980
 
2883
2981
  t.test('when the data is a deflated Buffer', async (t) => {
2884
- t.plan(3)
2982
+ t.plan(4)
2885
2983
 
2886
2984
  const fastify = Fastify()
2887
2985
  await fastify.register(compressPlugin, {
@@ -2900,12 +2998,13 @@ test('It should uncompress data when `Accept-Encoding` request header is missing
2900
2998
  method: 'GET'
2901
2999
  })
2902
3000
  t.equal(response.statusCode, 200)
3001
+ t.notOk(response.headers.vary)
2903
3002
  t.notOk(response.headers['content-encoding'])
2904
3003
  t.same(JSON.parse('' + response.payload), json)
2905
3004
  })
2906
3005
 
2907
3006
  t.test('when the data is a gzipped Buffer', async (t) => {
2908
- t.plan(3)
3007
+ t.plan(4)
2909
3008
 
2910
3009
  const fastify = Fastify()
2911
3010
  await fastify.register(compressPlugin, {
@@ -2924,12 +3023,13 @@ test('It should uncompress data when `Accept-Encoding` request header is missing
2924
3023
  method: 'GET'
2925
3024
  })
2926
3025
  t.equal(response.statusCode, 200)
3026
+ t.notOk(response.headers.vary)
2927
3027
  t.notOk(response.headers['content-encoding'])
2928
3028
  t.same(JSON.parse('' + response.payload), json)
2929
3029
  })
2930
3030
 
2931
3031
  t.test('when the data is a deflated Stream', async (t) => {
2932
- t.plan(3)
3032
+ t.plan(4)
2933
3033
 
2934
3034
  const fastify = Fastify()
2935
3035
  await fastify.register(compressPlugin, {
@@ -2951,12 +3051,13 @@ test('It should uncompress data when `Accept-Encoding` request header is missing
2951
3051
  })
2952
3052
  const file = readFileSync('./package.json', 'utf8')
2953
3053
  t.equal(response.statusCode, 200)
3054
+ t.notOk(response.headers.vary)
2954
3055
  t.notOk(response.headers['content-encoding'])
2955
3056
  t.equal(response.rawPayload.toString('utf-8'), file)
2956
3057
  })
2957
3058
 
2958
3059
  t.test('when the data is a gzipped Stream', async (t) => {
2959
- t.plan(3)
3060
+ t.plan(4)
2960
3061
 
2961
3062
  const fastify = Fastify()
2962
3063
  await fastify.register(compressPlugin, {
@@ -2978,12 +3079,13 @@ test('It should uncompress data when `Accept-Encoding` request header is missing
2978
3079
  })
2979
3080
  const file = readFileSync('./package.json', 'utf8')
2980
3081
  t.equal(response.statusCode, 200)
3082
+ t.notOk(response.headers.vary)
2981
3083
  t.notOk(response.headers['content-encoding'])
2982
3084
  t.equal(response.rawPayload.toString('utf-8'), file)
2983
3085
  })
2984
3086
 
2985
3087
  t.test('when the data has been compressed multiple times', async (t) => {
2986
- t.plan(3)
3088
+ t.plan(4)
2987
3089
 
2988
3090
  const fastify = Fastify()
2989
3091
  await fastify.register(compressPlugin, {
@@ -3006,6 +3108,7 @@ test('It should uncompress data when `Accept-Encoding` request header is missing
3006
3108
  method: 'GET'
3007
3109
  })
3008
3110
  t.equal(response.statusCode, 200)
3111
+ t.notOk(response.headers.vary)
3009
3112
  t.notOk(response.headers['content-encoding'])
3010
3113
  t.same(JSON.parse('' + response.payload), json)
3011
3114
  })
@@ -3038,7 +3141,7 @@ test('When `onUnsupportedEncoding` is set and the `Accept-Encoding` request head
3038
3141
  }
3039
3142
  })
3040
3143
  t.equal(response.statusCode, 406)
3041
- t.equal(response.headers.vary, 'accept-encoding')
3144
+ t.notOk(response.headers.vary)
3042
3145
  t.same(JSON.parse(response.payload), { hello: 'hello' })
3043
3146
  })
3044
3147
 
@@ -3139,7 +3242,7 @@ const defaultSupportedContentTypes = [
3139
3242
 
3140
3243
  for (const contentType of defaultSupportedContentTypes) {
3141
3244
  test(`It should compress data if content-type is supported by default, ${contentType}`, async (t) => {
3142
- t.plan(2)
3245
+ t.plan(3)
3143
3246
  const fastify = Fastify()
3144
3247
  await fastify.register(compressPlugin)
3145
3248
 
@@ -3158,6 +3261,7 @@ for (const contentType of defaultSupportedContentTypes) {
3158
3261
  })
3159
3262
  const file = readFileSync('./package.json', 'utf8')
3160
3263
  const payload = zlib.gunzipSync(response.rawPayload)
3264
+ t.equal(response.headers.vary, 'accept-encoding')
3161
3265
  t.equal(response.headers['content-encoding'], 'gzip')
3162
3266
  t.equal(payload.toString('utf-8'), file)
3163
3267
  })
@@ -3170,7 +3274,7 @@ const notByDefaultSupportedContentTypes = [
3170
3274
 
3171
3275
  for (const contentType of notByDefaultSupportedContentTypes) {
3172
3276
  test(`It should not compress data if content-type is not supported by default, ${contentType}`, async (t) => {
3173
- t.plan(2)
3277
+ t.plan(3)
3174
3278
  const fastify = Fastify()
3175
3279
  await fastify.register(compressPlugin)
3176
3280
 
@@ -3188,6 +3292,7 @@ for (const contentType of notByDefaultSupportedContentTypes) {
3188
3292
  }
3189
3293
  })
3190
3294
  const file = readFileSync('./package.json', 'utf8')
3295
+ t.notOk(response.headers.vary)
3191
3296
  t.notOk(response.headers['content-encoding'])
3192
3297
  t.equal(response.rawPayload.toString('utf-8'), file)
3193
3298
  })