Repository navigation
Expand file tree
/
Copy pathgzip.ts
More file actions
141 lines (114 loc) · 4.01 KB
/
Copy pathgzip.ts
File metadata and controls
141 lines (114 loc) · 4.01 KB
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
import {Buffer} from 'buffer';
import {promisify} from 'util';
import zlib from 'zlib';
import test from 'ava';
import getStream from 'get-stream';
import {ReadError, type HTTPError} from '../source/index.js';
import withServer from './helpers/with-server.js';
const testContent = 'Compressible response content.\n';
const testContentUncompressed = 'Uncompressed response content.\n';
let gzipData: Buffer;
test.before('setup', async () => {
gzipData = await promisify<string, Buffer>(zlib.gzip)(testContent);
});
test('decompress content', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('Content-Encoding', 'gzip');
response.end(gzipData);
});
t.is((await got('')).body, testContent);
});
test('decompress content on error', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('Content-Encoding', 'gzip');
response.status(404);
response.end(gzipData);
});
const error = await t.throwsAsync<HTTPError>(got(''));
t.is(error?.response.body, testContent);
});
test('decompress content - stream', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('Content-Encoding', 'gzip');
response.end(gzipData);
});
t.is((await getStream(got.stream(''))), testContent);
});
test('handles gzip error', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('Content-Encoding', 'gzip');
response.end('Not gzipped content');
});
await t.throwsAsync(got(''), {
name: 'ReadError',
message: 'incorrect header check',
});
});
test('no unhandled `Premature close` error', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('Content-Encoding', 'gzip');
response.write('Not gzipped content');
});
await t.throwsAsync(got(''), {
name: 'ReadError',
// `The server aborted pending request` on Node.js 15 or later.
message: /incorrect header check|The server aborted pending request/,
});
});
test('handles gzip error - stream', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('Content-Encoding', 'gzip');
response.end('Not gzipped content');
});
await
6CC2
t.throwsAsync(getStream(got.stream('')), {
name: 'ReadError',
message: 'incorrect header check',
});
});
test('decompress option opts out of decompressing', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('Content-Encoding', 'gzip');
response.end(gzipData);
});
const {body} = await got({decompress: false, responseType: 'buffer'});
t.is(Buffer.compare(body, gzipData), 0);
});
test('decompress option doesn\'t alter encoding of uncompressed responses', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.end(testContentUncompressed);
});
const {body} = await got({decompress: false});
t.is(body, testContentUncompressed);
});
test('preserves `headers` property', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('Content-Encoding', 'gzip');
response.end(gzipData);
});
t.truthy((await got('')).headers);
});
test('does not break HEAD responses', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.end();
});
t.is((await got.head('')).body, '');
});
test('does not ignore missing data', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('Content-Encoding', 'gzip');
response.end(gzipData.slice(0, -1));
});
await t.throwsAsync(got(''), {
instanceOf: ReadError,
message: 'unexpected end of file',
});
});
test('response has `url` and `requestUrl` properties', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('Content-Encoding', 'gzip');
response.end(gzipData);
});
const response = await got('');
t.truthy(response.url);
t.truthy(response.requestUrl);
});