/
error.ts
203 lines (172 loc) 路 5.38 KB
/
error.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
import {promisify} from 'util';
import http = require('http');
import stream = require('stream');
import test from 'ava';
import proxyquire = require('proxyquire');
import got, {GotError} from '../source';
import withServer from './helpers/with-server';
const pStreamPipeline = promisify(stream.pipeline);
test('properties', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.statusCode = 404;
response.end('not');
});
const url = new URL(server.url);
const error = await t.throwsAsync(got(''));
t.truthy(error);
t.truthy(error.response);
t.truthy(error.options);
t.false({}.propertyIsEnumerable.call(error, 'options'));
t.false({}.propertyIsEnumerable.call(error, 'response'));
t.false({}.hasOwnProperty.call(error, 'code'));
t.is(error.message, 'Response code 404 (Not Found)');
t.is(error.options.host, `${url.hostname}:${url.port}`);
t.is(error.options.method, 'GET');
t.is(error.options.protocol, 'http:');
t.is(error.options.url, error.options.requestUrl);
t.is(error.response.headers.connection, 'close');
t.is(error.response.body, 'not');
});
test('catches dns errors', async t => {
const error = await t.throwsAsync<GotError>(got('http://doesntexist', {retry: 0}));
t.truthy(error);
t.regex(error.message, /getaddrinfo ENOTFOUND/);
t.is(error.options.host, 'doesntexist');
t.is(error.options.method, 'GET');
});
test('`options.body` form error message', async t => {
// @ts-ignore Manual tests
await t.throwsAsync(got.post('https://example.com', {body: Buffer.from('test'), form: ''}), {
message: 'The `body` option cannot be used with the `json` option or `form` option'
});
});
test('no plain object restriction on json body', withServer, async (t, server, got) => {
server.post('/body', async (request, response) => {
await pStreamPipeline(request, response);
});
function CustomObject() {
this.a = 123;
}
const body = await got.post('body', {json: new CustomObject()}).json();
t.deepEqual(body, {a: 123});
});
test('default status message', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.statusCode = 400;
response.end('body');
});
const error = await t.throwsAsync(got(''));
// @ts-ignore
t.is(error.response.statusCode, 400);
// @ts-ignore
t.is(error.response.statusMessage, 'Bad Request');
});
test('custom status message', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.statusCode = 400;
response.statusMessage = 'Something Exploded';
response.end('body');
});
const error = await t.throwsAsync(got(''));
// @ts-ignore
t.is(error.response.statusCode, 400);
// @ts-ignore
t.is(error.response.statusMessage, 'Something Exploded');
});
test('custom body', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.statusCode = 404;
response.end('not');
});
const error = await t.throwsAsync(got(''));
// @ts-ignore
t.is(error.response.statusCode, 404);
// @ts-ignore
t.is(error.response.body, 'not');
});
test('contains Got options', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.statusCode = 404;
response.end();
});
const options = {
auth: 'foo:bar'
};
const error = await t.throwsAsync(got(options));
// @ts-ignore
t.is(error.options.auth, options.auth);
});
test('empty status message is overriden by the default one', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.writeHead(400, '');
response.end('body');
});
const error = await t.throwsAsync(got(''));
// @ts-ignore
t.is(error.response.statusCode, 400);
// @ts-ignore
t.is(error.response.statusMessage, http.STATUS_CODES[400]);
});
test('`http.request` error', async t => {
await t.throwsAsync(got('https://example.com', {
request: () => {
throw new TypeError('The header content contains invalid characters');
}
}), {
instanceOf: got.RequestError,
message: 'The header content contains invalid characters'
});
});
test('`http.request` pipe error', async t => {
const message = 'snap!';
await t.throwsAsync(got('https://example.com', {
// @ts-ignore Manual tests
request: () => {
return {
end: () => {
throw new Error(message);
},
on: () => {},
once: () => {},
emit: () => {}
};
},
throwHttpErrors: false
}), {
instanceOf: got.RequestError,
message
});
});
test('`http.request` error through CacheableRequest', async t => {
await t.throwsAsync(got('https://example.com', {
request: () => {
throw new TypeError('The header content contains invalid characters');
},
cache: new Map()
}), {
instanceOf: got.RequestError,
message: 'The header content contains invalid characters'
});
});
test('catches error in mimicResponse', withServer, async (t, server) => {
server.get('/', (_request, response) => {
response.end('ok');
});
const mimicResponse = () => {
throw new Error('Error in mimic-response');
};
mimicResponse['@global'] = true;
const proxiedGot = proxyquire('../source', {
'mimic-response': mimicResponse
});
// @ts-ignore
await t.throwsAsync(proxiedGot(server.url), {message: 'Error in mimic-response'});
});
test('errors are thrown directly when options.stream is true', t => {
t.throws(() => {
// @ts-ignore Manual tests
got('https://example.com', {stream: true, hooks: false});
}, {
message: 'Parameter `hooks` must be an object, not boolean'
});
});