/
http.ts
417 lines (323 loc) 路 10.4 KB
/
http.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import process from 'process';
import {Buffer} from 'buffer';
import {STATUS_CODES, Agent} from 'http';
import os from 'os';
import {isIPv4, isIPv6, isIP} from 'net';
import test from 'ava';
import {Handler} from 'express';
import nock from 'nock';
import getStream from 'get-stream';
import {pEvent} from 'p-event';
import got, {HTTPError, ReadError, RequestError} from '../source/index.js';
import withServer from './helpers/with-server.js';
// eslint-disable-next-line @typescript-eslint/naming-convention
const IPv6supported = Object.values(os.networkInterfaces()).some(iface => iface?.some(addr => !addr.internal && addr.family === 'IPv6'));
// eslint-disable-next-line @typescript-eslint/naming-convention
const testIPv6 = (IPv6supported && process.env.TRAVIS_DIST !== 'bionic' && process.env.TRAVIS_DIST !== 'focal') ? test : test.skip;
const echoIp: Handler = (request, response) => {
const address = request.connection.remoteAddress;
if (address === undefined) {
response.end();
return;
}
// IPv4 address mapped to IPv6
response.end(address === '::ffff:127.0.0.1' ? '127.0.0.1' : address);
};
const echoBody: Handler = async (request, response) => {
response.end(await getStream(request));
};
test('simple request', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.end('ok');
});
t.is((await got('')).body, 'ok');
});
test('empty response', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.end();
});
t.is((await got('')).body, '');
});
test('response has `requestUrl` property', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.end('ok');
});
server.get('/empty', (_request, response) => {
response.end();
});
t.is((await got('')).requestUrl.toString(), `${server.url}/`);
t.is((await got('empty')).requestUrl.toString(), `${server.url}/empty`);
});
test('http errors have `response` property', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.statusCode = 404;
response.end('not');
});
const error = await t.throwsAsync<HTTPError>(got(''), {instanceOf: HTTPError});
t.is(error.response.statusCode, 404);
t.is(error.response.body, 'not');
});
test('status code 304 doesn\'t throw', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.statusCode = 304;
response.end();
});
const promise = got('');
await t.notThrowsAsync(promise);
const {statusCode, body} = await promise;
t.is(statusCode, 304);
t.is(body, '');
});
test('doesn\'t throw if `options.throwHttpErrors` is false', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.statusCode = 404;
response.end('not');
});
t.is((await got({throwHttpErrors: false})).body, 'not');
});
test('invalid protocol throws', async t => {
await t.throwsAsync(got('c:/nope.com').json(), {
instanceOf: RequestError,
message: 'Unsupported protocol: c:',
code: 'ERR_UNSUPPORTED_PROTOCOL',
});
});
test('custom `options.encoding`', withServer, async (t, server, got) => {
const string = 'ok';
server.get('/', (_request, response) => {
response.end(string);
});
const data = (await got({encoding: 'base64'})).body;
t.is(data, Buffer.from(string).toString('base64'));
});
test('`options.encoding` doesn\'t affect streams', withServer, async (t, server, got) => {
const string = 'ok';
server.get('/', (_request, response) => {
response.end(string);
});
const data = await getStream(got.stream({encoding: 'base64'}));
t.is(data, string);
});
test('`got.stream(...).setEncoding(...)` works', withServer, async (t, server, got) => {
const string = 'ok';
server.get('/', (_request, response) => {
response.end(string);
});
const data = await getStream(got.stream('').setEncoding('base64'));
t.is(data, Buffer.from(string).toString('base64'));
});
test('`searchParams` option', withServer, async (t, server, got) => {
server.get('/', (request, response) => {
t.is(request.query.recent, 'true');
response.end('recent');
});
t.is((await got({searchParams: {recent: true}})).body, 'recent');
t.is((await got({searchParams: 'recent=true'})).body, 'recent');
});
test('response contains url', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.end('ok');
});
t.is((await got('')).url, `${server.url}/`);
});
test('response contains got options', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.end('ok');
});
{
const options = {
username: 'foo',
password: 'bar',
};
const {options: normalizedOptions} = (await got(options)).request;
t.is(normalizedOptions.username, options.username);
t.is(normalizedOptions.password, options.password);
}
{
const options = {
username: 'foo',
};
const {options: normalizedOptions} = (await got(options)).request;
t.is(normalizedOptions.username, options.username);
t.is(normalizedOptions.password, '');
}
{
const options = {
password: 'bar',
};
const {options: normalizedOptions} = (await got(options)).request;
t.is(normalizedOptions.username, '');
t.is(normalizedOptions.password, options.password);
}
});
test('socket destroyed by the server throws ECONNRESET', withServer, async (t, server, got) => {
server.get('/', request => {
request.socket.destroy();
});
await t.throwsAsync(got('', {retry: {limit: 0}}), {
code: 'ECONNRESET',
});
});
test('the response contains timings property', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.end('ok');
});
const {timings} = await got('');
t.truthy(timings);
t.true(timings.phases.total! >= 0);
});
test('throws an error if the server aborted the request', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.writeHead(200, {
'content-type': 'text/plain',
});
response.write('chunk 1');
setImmediate(() => {
response.write('chunk 2');
setImmediate(() => {
response.destroy();
});
});
});
const error = await t.throwsAsync<ReadError>(got(''), {
message: 'The server aborted pending request',
code: 'ECONNRESET',
});
t.truthy(error.response.retryCount);
});
test('statusMessage fallback', async t => {
nock('http://statusMessageFallback').get('/').reply(503);
const {statusMessage} = await got('http://statusMessageFallback', {
throwHttpErrors: false,
retry: {limit: 0},
});
t.is(statusMessage, STATUS_CODES[503]);
});
test('does not destroy completed requests', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('content-encoding', 'gzip');
response.end('');
});
const options = {
agent: {
http: new Agent({keepAlive: true}),
},
retry: {
limit: 0,
},
};
const stream = got.stream(options);
stream.resume();
const endPromise = pEvent(stream, 'end');
const socket = await pEvent(stream, 'socket');
const closeListener = () => {
t.fail('Socket has been destroyed');
};
socket.once('close', closeListener);
await new Promise(resolve => {
setTimeout(resolve, 10);
});
socket.off('close', closeListener);
await endPromise;
options.agent.http.destroy();
t.pass();
});
testIPv6('IPv6 request', withServer, async (t, server) => {
server.get('/ok', echoIp);
const response = await got(`http://[::1]:${server.port}/ok`);
t.is(response.body, '::1');
});
test('DNS auto', withServer, async (t, server, got) => {
server.get('/ok', echoIp);
const response = await got('ok', {
dnsLookupIpVersion: undefined,
});
const version = isIP(response.body);
t.true(version === 4 || version === 6);
});
test('DNS IPv4', withServer, async (t, server, got) => {
server.get('/ok', echoIp);
const response = await got('ok', {
dnsLookupIpVersion: 4,
});
t.true(isIPv4(response.body));
});
// Travis CI Ubuntu Focal VM does not resolve IPv6 hostnames
testIPv6('DNS IPv6', withServer, async (t, server, got) => {
server.get('/ok', echoIp);
const response = await got('ok', {
dnsLookupIpVersion: 6,
});
t.true(isIPv6(response.body));
});
test('invalid `dnsLookupIpVersion`', withServer, async (t, server, got) => {
server.get('/ok', echoIp);
await t.throwsAsync(got('ok', {
dnsLookupIpVersion: 'test',
} as any));
});
test('deprecated `family` option', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.end('ok');
});
await t.throwsAsync(got({
// @ts-expect-error Legacy option
family: 4,
}), {
message: 'Unexpected option: family',
});
});
test('JSON request custom stringifier', withServer, async (t, server, got) => {
server.post('/', echoBody);
const payload = {a: 'b'};
const customStringify = (object: any) => JSON.stringify({...object, c: 'd'});
t.deepEqual((await got.post({
stringifyJson: customStringify,
json: payload,
})).body, customStringify(payload));
});
test('ClientRequest can throw before promise resolves', async t => {
await t.throwsAsync(got('http://example.com', {
dnsLookup: ((_hostname: string, _options: unknown, callback: (error: null, hostname: string, family: number) => void) => { // eslint-disable-line @typescript-eslint/ban-types
queueMicrotask(() => {
callback(null, 'fe80::0000:0000:0000:0000', 6);
});
}) as any,
}), {
message: /EINVAL|EHOSTUNREACH|ETIMEDOUT/,
});
});
test('status code 200 has response ok is true', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.statusCode = 200;
response.end();
});
const promise = got('');
await t.notThrowsAsync(promise);
const {statusCode, body, ok} = await promise;
t.true(ok);
t.is(statusCode, 200);
t.is(body, '');
});
test('status code 404 has response ok is false if error is not thrown', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.statusCode = 404;
response.end();
});
const promise = got('', {throwHttpErrors: false});
await t.notThrowsAsync(promise);
const {statusCode, body, ok} = await promise;
t.is(ok, false);
t.is(statusCode, 404);
t.is(body, '');
});
test('status code 404 has error response ok is false if error is thrown', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.statusCode = 404;
response.end('not');
});
const error = await t.throwsAsync<HTTPError>(got(''), {instanceOf: HTTPError});
t.is(error.response.statusCode, 404);
t.is(error.response.ok, false);
t.is(error.response.body, 'not');
});