-
-
Notifications
You must be signed in to change notification settings - Fork 916
/
arguments.js
194 lines (162 loc) 路 4.97 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
import {URL, URLSearchParams} 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();
s.on('/', (request, response) => {
response.statusCode = 404;
response.end();
});
s.on('/test', (request, response) => {
response.end(request.url);
});
s.on('/?test=wow', (request, response) => {
response.end(request.url);
});
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 => {
const error = await t.throwsAsync(got());
t.regex(error.message, /Parameter `url` must be a string or object, not undefined/);
});
test('url should be utf-8 encoded', async t => {
const error = await t.throwsAsync(got(`${s.url}/%D2%E0%EB%EB%E8%ED`));
t.regex(error.message, /Parameter `url` must contain valid UTF-8 character sequences/);
});
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({
hostname: s.host,
port: s.port,
path: '/test'
})).body, '/test');
});
test('requestUrl with url.parse object as first argument', async t => {
t.is((await got({
hostname: s.host,
port: s.port,
path: '/test'
})).requestUrl, `${s.url}/test`);
});
test('overrides querystring from opts', async t => {
const response = 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(response.body, '/?test=wow');
});
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 with auth in url string', async t => {
const error = await t.throwsAsync(got('https://test:45d3ps453@account.myservice.com/api/token'));
t.regex(error.message, /Basic authentication must be done with the `auth` option/);
});
test('does not throw with auth in url object', async t => {
await t.notThrowsAsync(got({
auth: 'foo:bar',
hostname: s.host,
port: s.port,
path: '/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('throws TypeError when `url` is passed as an option', async t => {
await t.throwsAsync(got('', {url: 'example.com'}), {instanceOf: TypeError});
await t.throwsAsync(got({url: 'example.com'}), {instanceOf: TypeError});
});
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: 'Parameter `hooks.beforeRequest` must be an array, not Object'
}
);
});
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: 'Parameter `hooks.beforeRequest[0]` must be a function, not Object'
}
);
});
test('allows extra keys in `hooks`', async t => {
await t.notThrowsAsync(() => got(`${s.url}/test`, {hooks: {extra: {}}}));
});
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.');
});