-
-
Notifications
You must be signed in to change notification settings - Fork 921
/
http.js
104 lines (85 loc) 路 2.19 KB
/
http.js
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
import test from 'ava';
import got from '../';
import {createServer} from './helpers/server';
let s;
test.before('setup', async () => {
s = await createServer();
s.on('/', (req, res) => {
res.end('ok');
});
s.on('/empty', (req, res) => {
res.end();
});
s.on('/404', (req, res) => {
setTimeout(() => {
res.statusCode = 404;
res.end('not');
}, 10);
});
s.on('/?recent=true', (req, res) => {
res.end('recent');
});
await s.listen(s.port);
});
test('simple request', async t => {
t.is((await got(s.url)).body, 'ok');
});
test('protocol-less URLs', async t => {
t.is((await got(s.url.replace(/^http:\/\//, ''))).body, 'ok');
});
test('empty response', async t => {
t.is((await got(`${s.url}/empty`)).body, '');
});
test('requestUrl response', async t => {
t.is((await got(s.url)).requestUrl, `${s.url}/`);
t.is((await got(`${s.url}/empty`)).requestUrl, `${s.url}/empty`);
});
test('error with code', async t => {
try {
await got(`${s.url}/404`);
t.fail('Exception was not thrown');
} catch (err) {
t.is(err.statusCode, 404);
t.is(err.response.body, 'not');
}
});
test('buffer on encoding === null', async t => {
const data = (await got(s.url, {encoding: null})).body;
t.truthy(Buffer.isBuffer(data));
});
test('timeout option', async t => {
try {
await got(`${s.url}/404`, {
timeout: 1,
retries: 0
});
t.fail('Exception was not thrown');
} catch (err) {
t.is(err.code, 'ETIMEDOUT');
}
});
test('timeout option as object', async t => {
try {
await got(`${s.url}/404`, {
timeout: {connect: 1},
retries: 0
});
t.fail('Exception was not thrown');
} catch (err) {
t.is(err.code, 'ETIMEDOUT');
}
});
test('query option', async t => {
t.is((await got(s.url, {query: {recent: true}})).body, 'recent');
t.is((await got(s.url, {query: 'recent=true'})).body, 'recent');
});
test('requestUrl response when sending url as param', async t => {
t.is((await got(s.url, {hostname: s.host, port: s.port})).requestUrl, `${s.url}/`);
t.is((await got({hostname: s.host, port: s.port})).requestUrl, `${s.url}/`);
});
test('response contains url', async t => {
t.is((await got(s.url)).url, `${s.url}/`);
});
test.after('cleanup', async () => {
await s.close();
});