/
error.js
56 lines (48 loc) 路 1.17 KB
/
error.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
import test from 'ava';
import got from '../';
import {createServer} from './_server';
let s;
test.before('setup', async () => {
s = await createServer();
s.on('/', (req, res) => {
res.statusCode = 404;
res.end('not');
});
await s.listen(s.port);
});
test('properties', async t => {
try {
await got(s.url);
t.fail('Exception was not thrown');
} catch (err) {
t.ok(err);
t.ok(err.response);
t.ok(!err.propertyIsEnumerable('response'));
t.ok(!err.hasOwnProperty('code'));
t.is(err.message, 'Response code 404 (Not Found)');
t.is(err.host, `${s.host}:${s.port}`);
t.is(err.method, 'GET');
}
});
test('dns message', async t => {
try {
await got('.com', {retries: 0});
t.fail('Exception was not thrown');
} catch (err) {
t.ok(err);
t.regexTest(/getaddrinfo ENOTFOUND/, err.message);
t.is(err.host, '.com');
t.is(err.method, 'GET');
}
});
test('options.body error message', async t => {
try {
await got(s.url, {body: () => {}});
t.fail('Exception was not thrown');
} catch (err) {
t.regexTest(/options.body must be a ReadableStream, string, Buffer or plain Object/, err.message);
}
});
test.after('cleanup', async () => {
await s.close();
});