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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
/* globals AbortController */
'use strict'
const { test, teardown } = require('tap')
const {
Request,
Headers,
fetch
} = require('../../')
const {
Blob: ThirdPartyBlob,
FormData: ThirdPartyFormData
} = require('formdata-node')
const hasSignalReason = 'reason' in AbortSignal.prototype
test('arg validation', async (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.toString()
}, TypeError)
t.throws(() => {
Request.prototype.referrer.toString()
}, TypeError)
t.throws(() => {
Request.prototype.referrerPolicy.toString()
}, TypeError)
t.throws(() => {
Request.prototype.mode.toString()
}, TypeError)
t.throws(() => {
Request.prototype.credentials.toString()
}, TypeError)
t.throws(() => {
Request.prototype.cache.toString()
}, TypeError)
t.throws(() => {
Request.prototype.redirect.toString()
}, TypeError)
t.throws(() => {
Request.prototype.integrity.toString()
}, TypeError)
t.throws(() => {
Request.prototype.keepalive.toString()
}, TypeError)
t.throws(() => {
Request.prototype.isReloadNavigation.toString()
}, TypeError)
t.throws(() => {
Request.prototype.isHistoryNavigation.toString()
}, TypeError)
t.throws(() => {
Request.prototype.signal.toString()
}, TypeError)
t.throws(() => {
// eslint-disable-next-line no-unused-expressions
Request.prototype.body
}, TypeError)
t.throws(() => {
// eslint-disable-next-line no-unused-expressions
Request.prototype.bodyUsed
}, TypeError)
t.throws(() => {
Request.prototype.clone.call(null)
}, TypeError)
t.doesNotThrow(() => {
Request.prototype[Symbol.toStringTag].charAt(0)
})
for (const method of [
'text',
'json',
'arrayBuffer',
'blob',
'formData'
]) {
await t.rejects(async () => {
await new Request('http://localhost')[method].call({
blob () {
return {
text () {
return Promise.resolve('emulating this')
}
}
}
})
}, TypeError)
}
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.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, 'null')
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('gwak')
const req = new Request('http://asd', { signal: ac.signal })
t.equal(req.signal.aborted, true)
if (hasSignalReason) {
t.equal(req.signal.reason, 'gwak')
}
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', () => {
if (hasSignalReason) {
t.equal(req.signal.reason, 'gwak')
} else {
t.pass()
}
}, { once: true })
ac.abort('gwak')
})
test('pre aborted signal cloned', t => {
const ac = new AbortController()
ac.abort('gwak')
const req = new Request('http://asd', { signal: ac.signal }).clone()
t.equal(req.signal.aborted, true)
if (hasSignalReason) {
t.equal(req.signal.reason, 'gwak')
}
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', () => {
if (hasSignalReason) {
t.equal(req.signal.reason, 'gwak')
} else {
t.pass()
}
}, { once: true })
ac.abort('gwak')
})
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()
})
test('invalid RequestInit values', (t) => {
/* eslint-disable no-new */
t.throws(() => {
new Request('http://l', { mode: 'CoRs' })
}, TypeError, 'not exact case = error')
t.throws(() => {
new Request('http://l', { mode: 'random' })
}, TypeError)
t.throws(() => {
new Request('http://l', { credentials: 'OMIt' })
}, TypeError, 'not exact case = error')
t.throws(() => {
new Request('http://l', { credentials: 'random' })
}, TypeError)
t.throws(() => {
new Request('http://l', { cache: 'DeFaULt' })
}, TypeError, 'not exact case = error')
t.throws(() => {
new Request('http://l', { cache: 'random' })
}, TypeError)
t.throws(() => {
new Request('http://l', { redirect: 'FOllOW' })
}, TypeError, 'not exact case = error')
t.throws(() => {
new Request('http://l', { redirect: 'random' })
}, TypeError)
/* eslint-enable no-new */
t.end()
})
test('RequestInit.signal option', async (t) => {
t.throws(() => {
// eslint-disable-next-line no-new
new Request('http://asd', {
signal: true
})
}, TypeError)
await t.rejects(fetch('http://asd', {
signal: false
}), TypeError)
})
test('constructing Request with third party Blob body', async (t) => {
const blob = new ThirdPartyBlob(['text'])
const req = new Request('http://asd', {
method: 'POST',
body: blob
})
t.equal(await req.text(), 'text')
})
test('constructing Request with third party FormData body', async (t) => {
const form = new ThirdPartyFormData()
form.set('key', 'value')
const req = new Request('http://asd', {
method: 'POST',
body: form
})
const contentType = req.headers.get('content-type').split('=')
t.equal(contentType[0], 'multipart/form-data; boundary')
t.ok((await req.text()).startsWith(`--${contentType[1]}`))
})
// https://github.com/nodejs/undici/issues/2050
test('set-cookie headers get cleared when passing a Request as first param', (t) => {
const req1 = new Request('http://localhost', {
headers: {
'set-cookie': 'a=1'
}
})
t.same([...req1.headers], [['set-cookie', 'a=1']])
const req2 = new Request(req1, { headers: {} })
t.same([...req2.headers], [])
t.same(req2.headers.getSetCookie(), [])
t.end()
})
// https://github.com/nodejs/undici/issues/2124
test('request.referrer', (t) => {
for (const referrer of ['about://client', 'about://client:1234']) {
const request = new Request('http://a', { referrer })
t.equal(request.referrer, 'about:client')
}
t.end()
})
// https://github.com/nodejs/undici/issues/2445
test('Clone the set-cookie header when Request is passed as the first parameter and no header is passed.', (t) => {
t.plan(2)
const request = new Request('http://localhost', { headers: { 'set-cookie': 'A' } })
const request2 = new Request(request)
request2.headers.append('set-cookie', 'B')
t.equal(request.headers.getSetCookie().join(', '), request.headers.get('set-cookie'))
t.equal(request2.headers.getSetCookie().join(', '), request2.headers.get('set-cookie'))
})
// Tests for optimization introduced in https://github.com/nodejs/undici/pull/2456
test('keys to object prototypes method', (t) => {
t.plan(1)
const request = new Request('http://localhost', { method: 'hasOwnProperty' })
t.ok(typeof request.method === 'string')
})
// https://github.com/nodejs/undici/issues/2465
test('Issue#2465', async (t) => {
t.plan(1)
const request = new Request('http://localhost', { body: new SharedArrayBuffer(0), method: 'POST' })
t.equal(await request.text(), '[object SharedArrayBuffer]')
})
teardown(() => process.exit())
|