forked from sindresorhus/got
/
normalize-arguments.ts
184 lines (147 loc) 路 3.96 KB
/
normalize-arguments.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
import {URL, URLSearchParams} from 'url';
import test from 'ava';
import got, {Options} from '../source/index.js';
test('should merge options replacing responseType', t => {
const responseType = 'json';
const options = new Options({
responseType,
}, undefined, got.defaults.options);
t.is(options.responseType, responseType);
});
test('no duplicated searchParams values', t => {
const options = new Options({
searchParams: 'string=true&noDuplication=true',
}, {
searchParams: new URLSearchParams({
instance: 'true',
noDuplication: 'true',
}),
});
// eslint-disable-next-line unicorn/prevent-abbreviations
const searchParams = options.searchParams as URLSearchParams;
t.is(searchParams.get('string'), 'true');
t.is(searchParams.get('instance'), 'true');
t.is(searchParams.getAll('noDuplication').length, 1);
});
test('should copy non-numerable properties', t => {
const options = {
json: {hello: '123'},
};
const merged = new Options(options, undefined, got.defaults.options);
const mergedTwice = new Options(undefined, undefined, merged);
t.is(mergedTwice.json, options.json);
});
test('should get username and password from the URL', t => {
const options = new Options({
url: 'http://user:pass@localhost:41285',
});
t.is(options.username, 'user');
t.is(options.password, 'pass');
});
test('should get username and password from the options', t => {
const options = new Options({
url: 'http://user:pass@localhost:41285',
username: 'user_OPT',
password: 'pass_OPT',
});
t.is(options.username, 'user_OPT');
t.is(options.password, 'pass_OPT');
});
test('should get username and password from the merged options', t => {
const options = new Options(
{
url: 'http://user:pass@localhost:41285',
},
{
username: 'user_OPT_MERGE',
password: 'pass_OPT_MERGE',
},
);
t.is(options.username, 'user_OPT_MERGE');
t.is(options.password, 'pass_OPT_MERGE');
});
test('null value in search params means empty', t => {
const options = new Options({
url: new URL('http://localhost'),
searchParams: {
foo: null,
},
});
t.is((options.url as URL).href, 'http://localhost/?foo=');
});
test('undefined value in search params means it does not exist', t => {
const options = new Options({
url: new URL('http://localhost'),
searchParams: {
foo: undefined,
},
});
t.is((options.url as URL).href, 'http://localhost/');
});
test('prefixUrl alone does not set url', t => {
const options = new Options({
prefixUrl: 'https://example.com',
});
t.is(options.url, undefined);
});
test('maxRetryAfter is calculated separately from request timeout', t => {
const options = new Options({
timeout: {
request: 1000,
},
retry: {
maxRetryAfter: undefined,
},
});
t.is(options.retry.maxRetryAfter, undefined);
options.merge({
timeout: {
request: 2000,
},
});
t.is(options.retry.maxRetryAfter, undefined);
options.merge({
retry: {
maxRetryAfter: 300,
},
});
t.is(options.retry.maxRetryAfter, 300);
});
test('extending responseType', t => {
const instance1 = got.extend({
prefixUrl: 'https://localhost',
responseType: 'json',
});
const instance2 = got.extend({
headers: {
'x-test': 'test',
},
});
const merged = instance1.extend(instance2);
t.is(merged.defaults.options.responseType, 'json');
});
test('searchParams - multiple values for one key', t => {
const searchParameters = new URLSearchParams();
searchParameters.append('a', '100');
searchParameters.append('a', '200');
searchParameters.append('a', '300');
const options = new Options({
searchParams: searchParameters,
});
t.deepEqual(
(options.searchParams as URLSearchParams).getAll('a'),
['100', '200', '300'],
);
});
if (globalThis.AbortSignal !== undefined) {
test('signal does not get frozen', t => {
const controller = new AbortController();
const {signal} = controller;
const options = new Options({
url: new URL('http://localhost'),
signal,
});
options.freeze();
t.is(Object.isFrozen(options.signal), false);
});
}