Skip to content

Commit 6b36d49

Browse files
committed
fix redirect test in Node v16
Signed-off-by: Matteo Collina <[email protected]>
1 parent 648dd8f commit 6b36d49

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

test/redirect-request.js

+29-3
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,20 @@ const {
1313
startRedirectingWithQueryParams
1414
} = require('./utils/redirecting-servers')
1515
const { createReadable, createReadableStream } = require('./utils/stream')
16+
const Agent = undici.Agent
1617

1718
for (const factory of [
1819
(server, opts) => new undici.Agent(opts),
1920
(server, opts) => new undici.Pool(`http://${server}`, opts),
2021
(server, opts) => new undici.Client(`http://${server}`, opts)
2122
]) {
2223
const request = (t, server, opts, ...args) => {
23-
const dispatcher = factory(server, opts)
24+
opts = opts || {}
25+
const dispatcher = factory(server, {
26+
keepAliveMaxTimeout: 1000,
27+
keepAliveTimeout: 1000,
28+
...opts
29+
})
2430
t.teardown(() => dispatcher.close())
2531
return undici.request(args[0], { ...args[1], dispatcher }, args[2])
2632
}
@@ -371,10 +377,16 @@ t.test('should follow redirections when going cross origin', async t => {
371377
t.test('should handle errors (callback)', t => {
372378
t.plan(1)
373379

380+
const agent = new Agent({
381+
keepAliveTimeout: 1000,
382+
keepAliveMaxTimeout: 1000
383+
})
384+
374385
undici.request(
375386
'http://localhost:0',
376387
{
377-
maxRedirections: 10
388+
maxRedirections: 10,
389+
dispatcher: agent
378390
},
379391
error => {
380392
t.match(error.code, /EADDRNOTAVAIL|ECONNREFUSED/)
@@ -383,18 +395,27 @@ t.test('should handle errors (callback)', t => {
383395
})
384396

385397
t.test('should handle errors (promise)', async t => {
398+
const agent = new Agent({
399+
keepAliveTimeout: 1000,
400+
keepAliveMaxTimeout: 1000
401+
})
386402
try {
387-
await undici.request('http://localhost:0', { maxRedirections: 10 })
403+
await undici.request('http://localhost:0', { maxRedirections: 10, dispatcher: agent })
388404
t.fail('Did not throw')
389405
} catch (error) {
390406
t.match(error.code, /EADDRNOTAVAIL|ECONNREFUSED/)
391407
}
392408
})
393409

394410
t.test('removes authorization header on third party origin', async t => {
411+
const agent = new Agent({
412+
keepAliveTimeout: 1000,
413+
keepAliveMaxTimeout: 1000
414+
})
395415
const [server1] = await startRedirectingWithAuthorization(t, 'secret')
396416
const { body: bodyStream } = await undici.request(`http://${server1}`, {
397417
maxRedirections: 10,
418+
dispatcher: agent,
398419
headers: {
399420
authorization: 'secret'
400421
}
@@ -406,9 +427,14 @@ t.test('removes authorization header on third party origin', async t => {
406427
})
407428

408429
t.test('removes cookie header on third party origin', async t => {
430+
const agent = new Agent({
431+
keepAliveTimeout: 1000,
432+
keepAliveMaxTimeout: 1000
433+
})
409434
const [server1] = await startRedirectingWithCookie(t, 'a=b')
410435
const { body: bodyStream } = await undici.request(`http://${server1}`, {
411436
maxRedirections: 10,
437+
dispatcher: agent,
412438
headers: {
413439
cookie: 'a=b'
414440
}

test/utils/redirecting-servers.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ function close (server) {
1010
if (isNode20) {
1111
server.closeAllConnections()
1212
}
13-
server.close(resolve)
13+
server.close(function () {
14+
resolve()
15+
})
1416
})
1517
}
1618
}

0 commit comments

Comments
 (0)