/
http.ts
121 lines (96 loc) 路 3.26 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
import is from '@sindresorhus/is';
import test from 'ava';
import got from '../source';
import withServer from './helpers/with-server';
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, `${server.url}/`);
t.is((await got('empty')).requestUrl, `${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(got(''), got.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: got.UnsupportedProtocolError,
message: 'Unsupported protocol "c:"'
});
});
test('gives buffer if `options.encoding` is null', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.end('ok');
});
const data = (await got({encoding: null})).body;
t.true(is.buffer(data));
});
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 has `requestUrl` property even if `url` is an object', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.end('ok');
});
t.is((await got({hostname: server.hostname, port: server.port})).requestUrl, `${server.url}/`);
t.is((await got({hostname: server.hostname, port: server.port, protocol: 'http:'})).requestUrl, `${server.url}/`);
});
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 = {
auth: 'foo:bar'
};
t.is((await got(options)).request.options.auth, options.auth);
});