/
arguments.js
241 lines (201 loc) 路 5.81 KB
/
arguments.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
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
/* eslint-disable node/no-deprecated-api */
import {URL, URLSearchParams, parse} from 'url';
import test from 'ava';
import pEvent from 'p-event';
import got from '../source';
import {createServer} from './helpers/server';
let s;
test.before('setup', async () => {
s = await createServer();
const echoUrl = (request, response) => {
response.end(request.url);
};
s.on('/', (request, response) => {
response.statusCode = 404;
response.end();
});
s.on('/test', echoUrl);
s.on('/?test=wow', echoUrl);
s.on('/test/foobar', echoUrl);
s.on('/?test=it鈥檚+ok', echoUrl);
s.on('/?test=http://example.com?foo=bar', echoUrl);
s.on('/stream', (request, response) => {
response.end('ok');
});
await s.listen(s.port);
});
test.after('cleanup', async () => {
await s.close();
});
test('url is required', async t => {
await t.throwsAsync(
got(),
{
message: 'Parameter `url` must be a string or object, not undefined'
}
);
});
test('url should be utf-8 encoded', async t => {
await t.throwsAsync(
got(`${s.url}/%D2%E0%EB%EB%E8%ED`),
{
message: 'URI malformed'
}
);
});
test('string url with query is preserved', async t => {
const path = '/?test=http://example.com?foo=bar';
const {body} = await got(`${s.url}${path}`);
t.is(body, path);
});
test('options are optional', async t => {
t.is((await got(`${s.url}/test`)).body, '/test');
});
test('methods are normalized', async t => {
const instance = got.create({
methods: got.defaults.methods,
options: got.defaults.options,
handler: (options, next) => {
if (options.method === options.method.toUpperCase()) {
t.pass();
} else {
t.fail();
}
return next(options);
}
});
await instance(`${s.url}/test`, {method: 'post'});
});
test('accepts url.parse object as first argument', async t => {
t.is((await got(parse(`${s.url}/test`))).body, '/test');
});
test('requestUrl with url.parse object as first argument', async t => {
t.is((await got(parse(`${s.url}/test`))).requestUrl, `${s.url}/test`);
});
test('overrides querystring from options', async t => {
const {body} = await got(
`${s.url}/?drop=this`,
{
query: {
test: 'wow'
},
cache: {
get(key) {
t.is(key, `cacheable-request:GET:${s.url}/?test=wow`);
},
set(key) {
t.is(key, `cacheable-request:GET:${s.url}/?test=wow`);
}
}
}
);
t.is(body, '/?test=wow');
});
test('escapes query parameter values', async t => {
const {body} = await got(`${s.url}`, {
query: {
test: 'it鈥檚 ok'
}
});
t.is(body, '/?test=it%E2%80%99s+ok');
});
test('the `query` option can be a URLSearchParams', async t => {
const query = new URLSearchParams({test: 'wow'});
const {body} = await got(s.url, {query});
t.is(body, '/?test=wow');
});
test('should ignore empty query object', async t => {
t.is((await got(`${s.url}/test`, {query: {}})).requestUrl, `${s.url}/test`);
});
test('should throw when body is set to object', async t => {
await t.throwsAsync(got(`${s.url}/`, {body: {}}), TypeError);
});
test('WHATWG URL support', async t => {
const wURL = new URL(`${s.url}/test`);
await t.notThrowsAsync(got(wURL));
});
test('should return streams when using stream option', async t => {
const data = await pEvent(got(`${s.url}/stream`, {stream: true}), 'data');
t.is(data.toString(), 'ok');
});
test('should ignore JSON option when using stream option', async t => {
const data = await pEvent(got(`${s.url}/stream`, {stream: true, json: true}), 'data');
t.is(data.toString(), 'ok');
});
test('accepts `url` as an option', async t => {
await t.notThrowsAsync(got({url: `${s.url}/test`}));
});
test('throws TypeError when `hooks` is not an object', async t => {
await t.throwsAsync(
() => got(s.url, {hooks: 'not object'}),
{
instanceOf: TypeError,
message: 'Parameter `hooks` must be an object, not string'
}
);
});
test('throws TypeError when known `hooks` value is not an array', async t => {
await t.throwsAsync(
() => got(s.url, {hooks: {beforeRequest: {}}}),
{
instanceOf: TypeError,
message: 'options.hooks.beforeRequest is not iterable'
}
);
});
test('throws TypeError when known `hooks` array item is not a function', async t => {
await t.throwsAsync(
() => got(s.url, {hooks: {beforeRequest: [{}]}}),
{
instanceOf: TypeError,
message: 'hook is not a function'
}
);
});
test('allows extra keys in `hooks`', async t => {
await t.notThrowsAsync(() => got(`${s.url}/test`, {hooks: {extra: {}}}));
});
test('baseUrl works', async t => {
const instanceA = got.extend({baseUrl: `${s.url}/test`});
const {body} = await instanceA('/foobar');
t.is(body, '/test/foobar');
});
test('accepts WHATWG URL as the baseUrl option', async t => {
const instanceA = got.extend({baseUrl: new URL(`${s.url}/test`)});
const {body} = await instanceA('/foobar');
t.is(body, '/test/foobar');
});
test('backslash in the end of `baseUrl` is optional', async t => {
const instanceA = got.extend({baseUrl: `${s.url}/test/`});
const {body} = await instanceA('/foobar');
t.is(body, '/test/foobar');
});
test('backslash in the beginning of `url` is optional when using baseUrl', async t => {
const instanceA = got.extend({baseUrl: `${s.url}/test`});
const {body} = await instanceA('foobar');
t.is(body, '/test/foobar');
});
test('throws when trying to modify baseUrl after options got normalized', async t => {
const instanceA = got.create({
methods: [],
options: {baseUrl: 'https://example.com'},
handler: options => {
options.baseUrl = 'https://google.com';
}
});
await t.throwsAsync(instanceA('/'), 'Failed to set baseUrl. Options are normalized already.');
});
test('throws if the query key is invalid', async t => {
await t.throwsAsync(() => got(s.url, {
query: {
[[]]: []
}
}), TypeError);
});
test('throws if the query value is invalid', async t => {
await t.throwsAsync(() => got(s.url, {
query: {
foo: []
}
}), TypeError);
});