/
create.ts
344 lines (275 loc) 路 8.29 KB
/
create.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
import {Agent as HttpAgent, IncomingMessage, request as httpRequest, RequestOptions} from 'http';
import {URL} from 'url';
import test from 'ava';
import is from '@sindresorhus/is';
import {Handler} from 'express';
import got, {
BeforeRequestHook,
Headers,
Hooks,
RequestFunction
} from '../source';
import withServer from './helpers/with-server';
const echoHeaders: Handler = (request, response) => {
request.resume();
response.end(JSON.stringify(request.headers));
};
test('preserves global defaults', withServer, async (t, server, got) => {
server.get('/', echoHeaders);
const globalHeaders = await got('').json();
const instanceHeaders = await got.extend()('').json();
t.deepEqual(instanceHeaders, globalHeaders);
});
test('supports instance defaults', withServer, async (t, server, got) => {
server.get('/', echoHeaders);
const instance = got.extend({
headers: {
'user-agent': 'custom-ua-string'
}
});
const headers = await instance('').json<Headers>();
t.is(headers['user-agent'], 'custom-ua-string');
});
test('supports invocation overrides', withServer, async (t, server, got) => {
server.get('/', echoHeaders);
const instance = got.extend({
headers: {
'user-agent': 'custom-ua-string'
}
});
const headers = await instance({
headers: {
'user-agent': 'different-ua-string'
}
}).json<Headers>();
t.is(headers['user-agent'], 'different-ua-string');
});
test('carries previous instance defaults', withServer, async (t, server, got) => {
server.get('/', echoHeaders);
const instanceA = got.extend({
headers: {
'x-foo': 'foo'
}
});
const instanceB = instanceA.extend({
headers: {
'x-bar': 'bar'
}
});
const headers = await instanceB('').json<Headers>();
t.is(headers['x-foo'], 'foo');
t.is(headers['x-bar'], 'bar');
});
test('custom headers (extend)', withServer, async (t, server, got) => {
server.get('/', echoHeaders);
const options = {headers: {unicorn: 'rainbow'}};
const instance = got.extend(options);
const headers = await instance('').json<Headers>();
t.is(headers.unicorn, 'rainbow');
});
test('extend overwrites arrays with a deep clone', t => {
const beforeRequest = [0];
const a = got.extend({hooks: {beforeRequest} as unknown as Hooks});
beforeRequest[0] = 1;
t.deepEqual(a.defaults.options.hooks.beforeRequest, [0] as unknown as BeforeRequestHook[]);
t.not(a.defaults.options.hooks.beforeRequest, beforeRequest as unknown as BeforeRequestHook[]);
});
test('extend keeps the old value if the new one is undefined', t => {
const a = got.extend({headers: undefined});
t.deepEqual(
a.defaults.options.headers,
got.defaults.options.headers
);
});
test('hooks are merged on got.extend()', t => {
const hooksA = [() => {}];
const hooksB = [() => {}];
const instanceA = got.extend({hooks: {beforeRequest: hooksA}});
const extended = instanceA.extend({hooks: {beforeRequest: hooksB}});
t.deepEqual(extended.defaults.options.hooks.beforeRequest, hooksA.concat(hooksB));
});
test('custom endpoint with custom headers (extend)', withServer, async (t, server) => {
server.all('/', echoHeaders);
const instance = got.extend({headers: {unicorn: 'rainbow'}, prefixUrl: server.url});
const headers = await instance('').json<Headers>();
t.is(headers.unicorn, 'rainbow');
t.not(headers['user-agent'], undefined);
});
test('no tampering with defaults', t => {
t.throws(() => {
got.defaults.options.prefixUrl = 'http://google.com';
});
t.is(got.defaults.options.prefixUrl, '');
});
test('can set defaults to `got.mergeOptions(...)`', t => {
const instance = got.extend({
mutableDefaults: true,
followRedirect: false
});
t.notThrows(() => {
instance.defaults.options = got.mergeOptions(instance.defaults.options, {
followRedirect: true
});
});
t.true(instance.defaults.options.followRedirect);
t.notThrows(() => {
instance.defaults.options = got.mergeOptions({});
});
t.is(instance.defaults.options.followRedirect, undefined);
});
test('can set mutable defaults using got.extend', t => {
const instance = got.extend({
mutableDefaults: true,
followRedirect: false
});
t.notThrows(() => {
instance.defaults.options.followRedirect = true;
});
t.true(instance.defaults.options.followRedirect);
});
test('only plain objects are freezed', withServer, async (t, server, got) => {
server.get('/', echoHeaders);
const instance = got.extend({
agent: {
http: new HttpAgent({keepAlive: true})
},
mutableDefaults: true
});
t.notThrows(() => {
(instance.defaults.options.agent as any).http.keepAlive = true;
});
});
test('defaults are cloned on instance creation', t => {
const options = {foo: 'bar', hooks: {beforeRequest: [() => {}]}};
const instance = got.extend(options);
t.notThrows(() => {
options.foo = 'foo';
delete options.hooks.beforeRequest[0];
});
// @ts-expect-error This IS correct
t.not(options.foo, instance.defaults.options.foo);
t.not(options.hooks.beforeRequest, instance.defaults.options.hooks.beforeRequest);
});
test('ability to pass a custom request method', withServer, async (t, server, got) => {
server.get('/', echoHeaders);
let isCalled = false;
const request: RequestFunction = (...args: [
string | URL | RequestOptions,
(RequestOptions | ((res: IncomingMessage) => void))?,
((res: IncomingMessage) => void)?
]) => {
isCalled = true;
// @ts-expect-error Overload error
return httpRequest(...args);
};
const instance = got.extend({request});
await instance('');
t.true(isCalled);
});
test('does not include the `request` option in normalized `http` options', withServer, async (t, server, got) => {
server.get('/', echoHeaders);
let isCalled = false;
const request: RequestFunction = (...args: [
string | URL | RequestOptions,
(RequestOptions | ((res: IncomingMessage) => void))?,
((res: IncomingMessage) => void)?
]) => {
isCalled = true;
t.false(Reflect.has(args[0] as RequestOptions, 'request'));
// @ts-expect-error Overload error
return httpRequest(...args);
};
const instance = got.extend({request});
await instance('');
t.true(isCalled);
});
test('should pass an options object into an initialization hook after .extend', withServer, async (t, server, got) => {
t.plan(1);
server.get('/', echoHeaders);
const instance = got.extend({
hooks: {
init: [
(options) => {
t.deepEqual(options, {});
}
]
}
});
await instance('');
});
test('hooks aren\'t overriden when merging options', withServer, async (t, server, got) => {
server.get('/', echoHeaders);
let isCalled = false;
const instance = got.extend({
hooks: {
beforeRequest: [
() => {
isCalled = true;
}
]
}
});
await instance({});
t.true(isCalled);
});
test('extend with custom handlers', withServer, async (t, server, got) => {
server.get('/', echoHeaders);
const instance = got.extend({
handlers: [
(options, next) => {
options.headers.unicorn = 'rainbow';
return next(options);
}
]
});
const headers = await instance('').json<Headers>();
t.is(headers.unicorn, 'rainbow');
});
test('extend with instances', t => {
const a = got.extend({prefixUrl: new URL('https://example.com/')});
const b = got.extend(a);
t.is(b.defaults.options.prefixUrl.toString(), 'https://example.com/');
});
test('extend with a chain', t => {
const a = got.extend({prefixUrl: 'https://example.com/'});
const b = got.extend(a, {headers: {foo: 'bar'}});
t.is(b.defaults.options.prefixUrl.toString(), 'https://example.com/');
t.is(b.defaults.options.headers.foo, 'bar');
});
test('async handlers', withServer, async (t, server, got) => {
server.get('/', echoHeaders);
const instance = got.extend({
handlers: [
async (options, next) => {
const result = await next(options);
// @ts-expect-error Manual tests
result.modified = true;
return result;
}
]
});
const promise = instance('');
t.true(is.function_(promise.cancel));
// @ts-expect-error Manual tests
t.true((await promise).modified);
});
test('async handlers can throw', async t => {
const message = 'meh';
const instance = got.extend({
handlers: [
async () => {
throw new Error(message);
}
]
});
await t.throwsAsync(instance('https://example.com'), {message});
});
test('setting dnsCache to true points to global cache', t => {
const a = got.extend({
dnsCache: true
});
const b = got.extend({
dnsCache: true
});
t.is(a.defaults.options.dnsCache, b.defaults.options.dnsCache);
});