-
-
Notifications
You must be signed in to change notification settings - Fork 916
/
create.js
168 lines (143 loc) 路 4.13 KB
/
create.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
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
import http from 'http';
import {URL} from 'url';
import test from 'ava';
import got from '../source';
import {createServer} from './helpers/server';
let s;
test.before('setup', async () => {
s = await createServer();
s.on('/', (request, response) => {
request.resume();
response.end(JSON.stringify(request.headers));
});
await s.listen(s.port);
});
test.after('cleanup', async () => {
await s.close();
});
test('preserve global defaults', async t => {
const globalHeaders = (await got(s.url, {json: true})).body;
const instanceHeaders = (await got.extend()(s.url, {json: true})).body;
t.deepEqual(instanceHeaders, globalHeaders);
});
test('support instance defaults', async t => {
const instance = got.extend({
headers: {
'user-agent': 'custom-ua-string'
}
});
const headers = (await instance(s.url, {json: true})).body;
t.is(headers['user-agent'], 'custom-ua-string');
});
test('support invocation overrides', async t => {
const instance = got.extend({
headers: {
'user-agent': 'custom-ua-string'
}
});
const headers = (await instance(s.url, {
json: true,
headers: {
'user-agent': 'different-ua-string'
}
})).body;
t.is(headers['user-agent'], 'different-ua-string');
});
test('curry previous instance defaults', async t => {
const instanceA = got.extend({
headers: {
'x-foo': 'foo'
}
});
const instanceB = instanceA.extend({
headers: {
'x-bar': 'bar'
}
});
const headers = (await instanceB(s.url, {json: true})).body;
t.is(headers['x-foo'], 'foo');
t.is(headers['x-bar'], 'bar');
});
test('custom headers (extend)', async t => {
const options = {headers: {unicorn: 'rainbow'}};
const instance = got.extend(options);
const headers = (await instance(`${s.url}/`, {
json: true
})).body;
t.is(headers.unicorn, 'rainbow');
});
test('extend overwrites arrays with a deep clone', t => {
const statusCodes = [408];
const a = got.extend({retry: {statusCodes}});
statusCodes[0] = 500;
t.deepEqual(a.defaults.options.retry.statusCodes, [408]);
t.not(a.defaults.options.retry.statusCodes, statusCodes);
});
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('extend merges URL instances', t => {
const a = got.extend({baseUrl: new URL('https://example.com')});
const b = a.extend({baseUrl: '/foo'});
t.is(b.defaults.options.baseUrl.toString(), 'https://example.com/foo');
});
test('create', async t => {
const instance = got.create({
options: {},
handler: (options, next) => {
options.headers.unicorn = 'rainbow';
return next(options);
}
});
const headers = (await instance(s.url, {
json: true
})).body;
t.is(headers.unicorn, 'rainbow');
t.is(headers['user-agent'], undefined);
});
test('custom endpoint with custom headers (extend)', async t => {
const instance = got.extend({headers: {unicorn: 'rainbow'}, baseUrl: s.url});
const headers = (await instance(`/`, {
json: true
})).body;
t.is(headers.unicorn, 'rainbow');
t.not(headers['user-agent'], undefined);
});
test('no tampering with defaults', t => {
const instance = got.create({
handler: got.defaults.handler,
options: got.mergeOptions(got.defaults.options, {
baseUrl: 'example'
})
});
const instance2 = instance.create({
handler: instance.defaults.handler,
options: instance.defaults.options
});
// Tamper Time
t.throws(() => {
instance.defaults.options.baseUrl = 'http://google.com';
});
t.is(instance.defaults.options.baseUrl, 'example');
t.is(instance2.defaults.options.baseUrl, 'example');
});
test('only plain objects are freezed', async t => {
const instance = got.extend({
agent: new http.Agent({keepAlive: true})
});
await t.notThrowsAsync(() => instance(s.url));
});
test('defaults are cloned on instance creation', t => {
const options = {foo: 'bar', hooks: {beforeRequest: [() => {}]}};
const instance = got.create({options});
t.notThrows(() => {
options.foo = 'foo';
delete options.hooks.beforeRequest[0];
});
t.not(options.foo, instance.defaults.options.foo);
t.not(options.hooks.beforeRequest, instance.defaults.options.hooks.beforeRequest);
});