-
Notifications
You must be signed in to change notification settings - Fork 114
/
options.compressed.test.ts
203 lines (189 loc) · 7.67 KB
/
options.compressed.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { strict as assert } from 'node:assert';
import { createWriteStream, createReadStream } from 'node:fs';
import { describe, it, beforeAll, afterAll, beforeEach, afterEach } from 'vitest';
import urllib from '../src';
import { startServer } from './fixtures/server';
import { readableToString, createTempfile } from './utils';
describe('options.compressed.test.ts', () => {
const keepAliveTimeout = 1000;
let close: any;
let _url: string;
let tmpfile: string;
let cleanup: any;
beforeAll(async () => {
const { closeServer, url } = await startServer({ keepAliveTimeout });
close = closeServer;
_url = url;
});
afterAll(async () => {
await close();
});
beforeEach(async () => {
const item = await createTempfile();
tmpfile = item.tmpfile;
cleanup = item.cleanup;
});
afterEach(async () => {
await cleanup();
});
it('should default compressed = false', async () => {
const response = await urllib.request(`${_url}brotli`, {
dataType: 'text',
});
assert.equal(response.status, 200);
assert.equal(response.headers['content-encoding'], 'br');
// console.log(response.headers);
const requestHeaders = JSON.parse(response.headers['x-request-headers'] as string);
assert(!requestHeaders['accept-encoding'],
`should not contains accept-encoding header: ${requestHeaders['accept-encoding']}`);
assert.match(response.data, /export async function startServer/);
});
it('should deflate content when server accept brotli', async () => {
const response = await urllib.request(`${_url}brotli`, {
dataType: 'text',
compressed: true,
});
assert.equal(response.status, 200);
assert.equal(response.headers['content-encoding'], 'br');
// console.log(response.headers);
const requestHeaders = JSON.parse(response.headers['x-request-headers'] as string);
assert.equal(requestHeaders['accept-encoding'], 'gzip, br');
assert.match(response.data, /export async function startServer/);
});
it('should gzip content when server accept gzip', async () => {
const response = await urllib.request(`${_url}gzip`, {
dataType: 'text',
compressed: true,
});
assert.equal(response.status, 200);
assert.equal(response.headers['content-encoding'], 'gzip');
// console.log(response.headers);
const requestHeaders = JSON.parse(response.headers['x-request-headers'] as string);
assert.equal(requestHeaders['accept-encoding'], 'gzip, br');
assert.match(response.data, /export async function startServer/);
});
it('should dataType=text with 304 status with content-encoding: gzip', async () => {
const response = await urllib.request(`${_url}304-with-gzip`, {
dataType: 'text',
compressed: true,
});
assert.equal(response.status, 304);
assert.equal(response.headers['content-encoding'], 'gzip');
// console.log(response.headers);
const requestHeaders = JSON.parse(response.headers['x-request-headers'] as string);
assert.equal(requestHeaders['accept-encoding'], 'gzip, br');
assert.equal(response.data, '');
});
it('should dataType=json with 304 status with content-encoding: gzip', async () => {
const response = await urllib.request(`${_url}304-with-gzip`, {
dataType: 'json',
compressed: true,
});
assert.equal(response.status, 304);
assert.equal(response.headers['content-encoding'], 'gzip');
// console.log(response.headers);
const requestHeaders = JSON.parse(response.headers['x-request-headers'] as string);
assert.equal(requestHeaders['accept-encoding'], 'gzip, br');
assert.equal(response.data, null);
});
it('should gzip work on dataType = stream', async () => {
const response = await urllib.request(`${_url}gzip`, {
dataType: 'stream',
compressed: true,
});
assert.equal(response.status, 200);
assert.equal(response.headers['content-encoding'], 'gzip');
// console.log(response.headers);
const requestHeaders = JSON.parse(response.headers['x-request-headers'] as string);
assert.equal(requestHeaders['accept-encoding'], 'gzip, br');
const text = await readableToString(response.res as any);
assert.match(text, /export async function startServer/);
});
it('should brotli work on dataType = stream', async () => {
const response = await urllib.request(`${_url}brotli`, {
dataType: 'stream',
compressed: true,
});
assert.equal(response.status, 200);
assert.equal(response.headers['content-encoding'], 'br');
// console.log(response.headers);
const requestHeaders = JSON.parse(response.headers['x-request-headers'] as string);
assert.equal(requestHeaders['accept-encoding'], 'gzip, br');
const text = await readableToString(response.res as any);
assert.match(text, /export async function startServer/);
});
it('should gzip work on writeStream', async () => {
const writeStream = createWriteStream(tmpfile);
const response = await urllib.request(`${_url}gzip`, {
compressed: true,
writeStream,
});
assert.equal(response.status, 200);
assert.equal(response.headers['content-encoding'], 'gzip');
const requestHeaders = JSON.parse(response.headers['x-request-headers'] as string);
assert.equal(requestHeaders['accept-encoding'], 'gzip, br');
const text = await readableToString(createReadStream(tmpfile));
assert.match(text, /export async function startServer/);
});
it('should brotli work on writeStream', async () => {
const writeStream = createWriteStream(tmpfile);
const response = await urllib.request(`${_url}brotli`, {
compressed: true,
writeStream,
});
assert.equal(response.status, 200);
assert.equal(response.headers['content-encoding'], 'br');
// console.log(response.headers);
const requestHeaders = JSON.parse(response.headers['x-request-headers'] as string);
assert.equal(requestHeaders['accept-encoding'], 'gzip, br');
const text = await readableToString(createReadStream(tmpfile));
assert.match(text, /export async function startServer/);
});
it('should keep accept-encoding raw', async () => {
const response = await urllib.request(`${_url}gzip`, {
dataType: 'text',
compressed: true,
headers: {
'accept-encoding': 'gzip',
},
});
assert.equal(response.status, 200);
assert.equal(response.headers['content-encoding'], 'gzip');
// console.log(response.headers);
const requestHeaders = JSON.parse(response.headers['x-request-headers'] as string);
assert.equal(requestHeaders['accept-encoding'], 'gzip');
assert.match(response.data, /export async function startServer/);
});
it('should throw error when gzip content invaild', async () => {
await assert.rejects(async () => {
await urllib.request(`${_url}error-gzip`, {
dataType: 'text',
compressed: true,
});
}, (err: any) => {
// console.error(err);
assert.equal(err.name, 'UnzipError');
assert.equal(err.message, 'incorrect header check');
assert.equal(err.code, 'Z_DATA_ERROR');
assert.equal(err.status, 200);
assert.equal(err.headers['content-encoding'], 'gzip');
return true;
});
});
it('should throw error when brotli content invaild', async () => {
await assert.rejects(async () => {
await urllib.request(`${_url}error-brotli`, {
dataType: 'text',
compressed: true,
});
}, (err: any) => {
// console.error(err);
assert.equal(err.name, 'UnzipError');
assert.equal(err.message, 'Decompression failed');
assert.equal(err.code, 'ERR_PADDING_1');
assert.equal(err.status, 200);
assert.equal(err.headers['content-encoding'], 'br');
return true;
});
});
});