/
request.js
341 lines (285 loc) · 7.36 KB
/
request.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
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
/* globals AbortController */
'use strict'
const { test } = require('tap')
const {
Request,
Headers
} = require('../../')
const { kState } = require('../../lib/fetch/symbols.js')
const { URLSearchParams } = require('url')
test('arg validation', (t) => {
// constructor
t.throws(() => {
// eslint-disable-next-line
new Request()
}, TypeError)
t.throws(() => {
// eslint-disable-next-line
new Request('http://asd', 0)
}, TypeError)
t.throws(() => {
const url = new URL('http://asd')
url.password = 'asd'
// eslint-disable-next-line
new Request(url)
}, TypeError)
t.throws(() => {
const url = new URL('http://asd')
url.username = 'asd'
// eslint-disable-next-line
new Request(url)
}, TypeError)
t.doesNotThrow(() => {
// eslint-disable-next-line
new Request('http://asd', undefined)
}, TypeError)
t.throws(() => {
// eslint-disable-next-line
new Request('http://asd', {
window: {}
})
}, TypeError)
t.throws(() => {
// eslint-disable-next-line
new Request('http://asd', {
window: 1
})
}, TypeError)
t.throws(() => {
// eslint-disable-next-line
new Request('http://asd', {
mode: 'navigate'
})
})
t.throws(() => {
// eslint-disable-next-line
new Request('http://asd', {
referrerPolicy: 'agjhagna'
})
}, TypeError)
t.throws(() => {
// eslint-disable-next-line
new Request('http://asd', {
mode: 'agjhagna'
})
}, TypeError)
t.throws(() => {
// eslint-disable-next-line
new Request('http://asd', {
credentials: 'agjhagna'
})
}, TypeError)
t.throws(() => {
// eslint-disable-next-line
new Request('http://asd', {
cache: 'agjhagna'
})
}, TypeError)
t.throws(() => {
// eslint-disable-next-line
new Request('http://asd', {
method: 'agjhagnaöööö'
})
}, TypeError)
t.throws(() => {
// eslint-disable-next-line
new Request('http://asd', {
method: 'TRACE'
})
}, TypeError)
t.throws(() => {
Request.prototype.destination.call(null)
}, TypeError)
t.throws(() => {
Request.prototype.referrer.call(null)
}, TypeError)
t.throws(() => {
Request.prototype.referrerPolicy.call(null)
}, TypeError)
t.throws(() => {
Request.prototype.mode.call(null)
}, TypeError)
t.throws(() => {
Request.prototype.credentials.call(null)
}, TypeError)
t.throws(() => {
Request.prototype.cache.call(null)
}, TypeError)
t.throws(() => {
Request.prototype.redirect.call(null)
}, TypeError)
t.throws(() => {
Request.prototype.integrity.call(null)
}, TypeError)
t.throws(() => {
Request.prototype.keepalive.call(null)
}, TypeError)
t.throws(() => {
Request.prototype.isReloadNavigation.call(null)
}, TypeError)
t.throws(() => {
Request.prototype.isHistoryNavigation.call(null)
}, TypeError)
t.throws(() => {
Request.prototype.signal.call(null)
}, TypeError)
t.throws(() => {
Request.prototype.clone.call(null)
}, TypeError)
t.doesNotThrow(() => {
Request.prototype[Symbol.toStringTag].charAt(0)
})
t.end()
})
test('undefined window', t => {
t.doesNotThrow(() => new Request('http://asd', { window: undefined }))
t.end()
})
test('undefined body', t => {
const req = new Request('http://asd', { body: undefined })
t.equal(req[kState].body, null)
t.end()
})
test('undefined method', t => {
const req = new Request('http://asd', { method: undefined })
t.equal(req.method, 'GET')
t.end()
})
test('undefined headers', t => {
const req = new Request('http://asd', { headers: undefined })
t.equal([...req.headers.entries()].length, 0)
t.end()
})
test('undefined referrer', t => {
const req = new Request('http://asd', { referrer: undefined })
t.equal(req.referrer, 'about:client')
t.end()
})
test('undefined referrerPolicy', t => {
const req = new Request('http://asd', { referrerPolicy: undefined })
t.equal(req.referrerPolicy, '')
t.end()
})
test('undefined mode', t => {
const req = new Request('http://asd', { mode: undefined })
t.equal(req.mode, 'cors')
t.end()
})
test('undefined credentials', t => {
const req = new Request('http://asd', { credentials: undefined })
t.equal(req.credentials, 'same-origin')
t.end()
})
test('undefined cache', t => {
const req = new Request('http://asd', { cache: undefined })
t.equal(req.cache, 'default')
t.end()
})
test('undefined redirect', t => {
const req = new Request('http://asd', { redirect: undefined })
t.equal(req.redirect, 'follow')
t.end()
})
test('undefined keepalive', t => {
const req = new Request('http://asd', { keepalive: undefined })
t.equal(req.keepalive, false)
t.end()
})
test('undefined integrity', t => {
const req = new Request('http://asd', { integrity: undefined })
t.equal(req.integrity, '')
t.end()
})
test('null integrity', t => {
const req = new Request('http://asd', { integrity: null })
t.equal(req.integrity, '')
t.end()
})
test('undefined signal', t => {
const req = new Request('http://asd', { signal: undefined })
t.equal(req.signal.aborted, false)
t.end()
})
test('pre aborted signal', t => {
const ac = new AbortController()
ac.abort()
const req = new Request('http://asd', { signal: ac.signal })
t.equal(req.signal.aborted, true)
t.end()
})
test('post aborted signal', t => {
t.plan(2)
const ac = new AbortController()
const req = new Request('http://asd', { signal: ac.signal })
t.equal(req.signal.aborted, false)
ac.signal.addEventListener('abort', () => {
t.pass()
})
ac.abort()
})
test('pre aborted signal cloned', t => {
const ac = new AbortController()
ac.abort()
const req = new Request('http://asd', { signal: ac.signal }).clone()
t.equal(req.signal.aborted, true)
t.end()
})
test('URLSearchParams body with Headers object - issue #1407', async (t) => {
const body = new URLSearchParams({
abc: 123
})
const request = new Request(
'http://localhost',
{
method: 'POST',
body,
headers: {
Authorization: 'test'
}
}
)
t.equal(request.headers.get('content-type'), 'application/x-www-form-urlencoded;charset=UTF-8')
t.equal(request.headers.get('authorization'), 'test')
t.equal(await request.text(), 'abc=123')
})
test('post aborted signal cloned', t => {
t.plan(2)
const ac = new AbortController()
const req = new Request('http://asd', { signal: ac.signal }).clone()
t.equal(req.signal.aborted, false)
ac.signal.addEventListener('abort', () => {
t.pass()
})
ac.abort()
})
test('Passing headers in init', (t) => {
// https://github.com/nodejs/undici/issues/1400
t.test('Headers instance', (t) => {
const req = new Request('http://localhost', {
headers: new Headers({ key: 'value' })
})
t.equal(req.headers.get('key'), 'value')
t.end()
})
t.test('key:value object', (t) => {
const req = new Request('http://localhost', {
headers: { key: 'value' }
})
t.equal(req.headers.get('key'), 'value')
t.end()
})
t.test('[key, value][]', (t) => {
const req = new Request('http://localhost', {
headers: [['key', 'value']]
})
t.equal(req.headers.get('key'), 'value')
t.end()
})
t.end()
})
test('Symbol.toStringTag', (t) => {
const req = new Request('http://localhost')
t.equal(req[Symbol.toStringTag], 'Request')
t.equal(Request.prototype[Symbol.toStringTag], 'Request')
t.end()
})