/
gzip.ts
117 lines (93 loc) 路 3.28 KB
/
gzip.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
import {promisify} from 'util';
import zlib = require('zlib');
import test from 'ava';
import getStream = require('get-stream');
import withServer from './helpers/with-server';
const testContent = 'Compressible response content.\n';
const testContentUncompressed = 'Uncompressed response content.\n';
let gzipData;
test.before('setup', async () => {
gzipData = await promisify(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 - 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');
});
const error = await t.throwsAsync(got(''), 'incorrect header check');
// @ts-ignore
t.is(error.options.path, '/');
t.is(error.name, 'ReadError');
});
// FIXME: This causes an unhandled rejection.
// eslint-disable-next-line ava/no-skip-test
test.skip('handles gzip error - stream', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('Content-Encoding', 'gzip');
response.end('Not gzipped content');
});
const error = await t.throws(() => {
got.stream('');
}, 'incorrect header check');
// @ts-ignore
t.is(error.options.path, '/');
t.is(error.name, 'ReadError');
});
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});
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('ignore missing data', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('Content-Encoding', 'gzip');
response.end(gzipData.slice(0, -1));
});
t.is((await got('')).body, testContent);
});
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);
});