11import * as zlib from 'zlib' ;
2- import * as brotliPromise from 'brotli-wasm' ;
3- import { ZstdCodec , ZstdStreaming } from 'zstd-codec' ;
42
5- import * as semver from 'semver' ;
6-
7- import { expect , BROKEN_WASM_BUFFER_ISSUE } from './test-utils' ;
3+ import { expect , nodeOnly } from './test-utils' ;
84import { buildBodyReader } from '../src/util/request-utils' ;
95
10- const zstd : Promise < ZstdStreaming > = new Promise ( ( resolve ) =>
11- ZstdCodec . run ( ( binding ) => {
12- resolve ( new binding . Streaming ( ) )
13- } )
14- ) ;
15-
16- describe ( "buildBodyReader" , ( ) => {
6+ nodeOnly ( ( ) => {
7+ describe ( "buildBodyReader" , ( ) => {
178
18- let brotli : typeof import ( 'brotli-wasm' ) ;
19- beforeEach ( async function ( ) {
20- this . timeout ( 5000 ) ; // Brotli can be slow to load initially
21- brotli = await brotliPromise ;
22- } ) ;
23-
24- describe ( ".text" , ( ) => {
25- it ( 'returns the raw text for unspecified requests' , async ( ) => {
26- const body = buildBodyReader ( Buffer . from ( 'hello world' ) , { } ) ;
27- expect ( await body . getText ( ) ) . to . equal ( 'hello world' ) ;
28- } ) ;
29-
30- it ( 'returns the raw text for identity requests' , async ( ) => {
31- const body = buildBodyReader ( Buffer . from ( 'hello world' ) , {
32- 'content-encoding' : 'identity'
9+ describe ( ".text" , ( ) => {
10+ it ( 'returns the raw text for unspecified requests' , async ( ) => {
11+ const body = buildBodyReader ( Buffer . from ( 'hello world' ) , { } ) ;
12+ expect ( await body . getText ( ) ) . to . equal ( 'hello world' ) ;
3313 } ) ;
34- expect ( await body . getText ( ) ) . to . equal ( 'hello world' ) ;
35- } ) ;
3614
37- it ( 'is undefined for unknown encodings' , async ( ) => {
38- const body = buildBodyReader ( Buffer . from ( 'hello world' ) , {
39- 'content-encoding' : 'randomized'
15+ it ( 'returns the raw text for identity requests' , async ( ) => {
16+ const body = buildBodyReader ( Buffer . from ( 'hello world' ) , {
17+ 'content-encoding' : 'identity'
18+ } ) ;
19+ expect ( await body . getText ( ) ) . to . equal ( 'hello world' ) ;
4020 } ) ;
41- expect ( await body . getText ( ) ) . to . equal ( undefined ) ;
42- } ) ;
4321
44- it ( 'can decode gzip bodies' , async ( ) => {
45- const content = Buffer . from ( zlib . gzipSync ( 'Gzip response' ) ) ;
46- const body = buildBodyReader ( content , {
47- 'content-encoding' : 'gzip'
22+ it ( 'is undefined for unknown encodings' , async ( ) => {
23+ const body = buildBodyReader ( Buffer . from ( 'hello world' ) , {
24+ 'content-encoding' : 'randomized'
25+ } ) ;
26+ expect ( await body . getText ( ) ) . to . equal ( undefined ) ;
4827 } ) ;
49- expect ( await body . getText ( ) ) . to . equal ( 'Gzip response' ) ;
50- } ) ;
5128
52- it ( 'can decode zlib deflate bodies' , async ( ) => {
53- const content = Buffer . from ( zlib . deflateSync ( 'Deflate response' ) ) ;
54- const body = buildBodyReader ( content , {
55- 'content-encoding' : 'deflate'
29+ it ( 'can decode gzip bodies' , async ( ) => {
30+ const content = zlib . gzipSync ( 'Gzip response' ) ;
31+ const body = buildBodyReader ( content , {
32+ 'content-encoding' : 'gzip'
33+ } ) ;
34+ expect ( await body . getText ( ) ) . to . equal ( 'Gzip response' ) ;
5635 } ) ;
57- expect ( await body . getText ( ) ) . to . equal ( 'Deflate response' ) ;
58- } ) ;
5936
60- it ( 'can decode raw deflate bodies' , async ( ) => {
61- const content = Buffer . from ( zlib . deflateRawSync ( 'Raw deflate response' ) ) ;
62- const body = buildBodyReader ( content , {
63- 'content-encoding' : 'deflate'
37+ it ( 'can decode zlib deflate bodies' , async ( ) => {
38+ const content = zlib . deflateSync ( 'Deflate response' ) ;
39+ const body = buildBodyReader ( content , {
40+ 'content-encoding' : 'deflate'
41+ } ) ;
42+ expect ( await body . getText ( ) ) . to . equal ( 'Deflate response' ) ;
6443 } ) ;
65- expect ( await body . getText ( ) ) . to . equal ( 'Raw deflate response' ) ;
66- } ) ;
6744
68- it ( 'can decode brotli bodies' , async ( ) => {
69- const content = Buffer . from (
70- await brotli . compress ( Buffer . from ( 'Brotli brotli brotli brotli brotli' , 'utf8' ) )
71- ) ;
72- const body = buildBodyReader ( content , {
73- 'content-encoding' : 'br'
45+ it ( 'can decode raw deflate bodies' , async ( ) => {
46+ const content = zlib . deflateRawSync ( 'Raw deflate response' ) ;
47+ const body = buildBodyReader ( content , {
48+ 'content-encoding' : 'deflate'
49+ } ) ;
50+ expect ( await body . getText ( ) ) . to . equal ( 'Raw deflate response' ) ;
7451 } ) ;
75- expect ( await body . getText ( ) ) . to . equal ( 'Brotli brotli brotli brotli brotli' ) ;
76- } ) ;
7752
78- it ( 'can decode zstandard bodies' , async function ( ) {
79- if ( semver . satisfies ( process . version , BROKEN_WASM_BUFFER_ISSUE ) ) this . skip ( ) ;
80- this . timeout ( 5000 ) ; // Zstd can be slow to load (inside the body reader, not just here)
53+ it ( 'can decode brotli bodies' , async function ( ) {
54+ if ( ! zlib . brotliCompressSync ) this . skip ( ) ;
8155
82- const content = Buffer . from ( ( await zstd ) . compress ( Buffer . from ( 'hello zstd zstd zstd world' ) ) ) ;
83- const body = buildBodyReader ( content , {
84- 'content-encoding' : 'zstd'
56+ const content = zlib . brotliCompressSync ( 'Brotli brotli brotli brotli brotli' ) ;
57+ const body = buildBodyReader ( content , {
58+ 'content-encoding' : 'br'
59+ } ) ;
60+ expect ( await body . getText ( ) ) . to . equal ( 'Brotli brotli brotli brotli brotli' ) ;
8561 } ) ;
86- expect ( await body . getText ( ) ) . to . equal ( 'hello zstd zstd zstd world' ) ;
87- } ) ;
8862
89- it ( 'can decode bodies with multiple encodings ' , async ( ) => {
90- const content = zlib . gzipSync (
91- Buffer . from ( await brotli . compress (
92- Buffer . from ( 'First brotli, then gzip, now this' , 'utf8' )
93- ) )
94- ) ;
95- const body = buildBodyReader ( content , {
96- 'content-encoding' : 'br, identity, gzip, identity'
63+ it ( 'can decode zstandard bodies ' , async function ( ) {
64+ if ( ! zlib . zstdCompressSync ) this . skip ( ) ;
65+
66+ const content = zlib . zstdCompressSync ( 'hello zstd zstd zstd world' ) ;
67+ const body = buildBodyReader ( content , {
68+ 'content-encoding' : 'zstd'
69+ } ) ;
70+ expect ( await body . getText ( ) ) . to . equal ( 'hello zstd zstd zstd world' ) ;
9771 } ) ;
9872
99- expect ( await body . getText ( ) ) . to . equal ( 'First brotli, then gzip, now this' ) ;
73+ it ( 'can decode bodies with multiple encodings' , async function ( ) {
74+ if ( ! zlib . brotliCompressSync ) this . skip ( ) ;
75+
76+ const content = zlib . gzipSync (
77+ zlib . brotliCompressSync (
78+ 'First brotli, then gzip, now this'
79+ )
80+ ) ;
81+ const body = buildBodyReader ( content , {
82+ 'content-encoding' : 'br, identity, gzip, identity'
83+ } ) ;
84+
85+ expect ( await body . getText ( ) ) . to . equal ( 'First brotli, then gzip, now this' ) ;
86+ } ) ;
10087 } ) ;
88+
10189 } ) ;
10290} ) ;
0 commit comments