Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fetch parameters not being applied correctly #1870

Merged
merged 5 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ class Agent extends DispatcherBase {
}
}

options () {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems a little hacky, any thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would need a bit more support from all the available agents.
Otherwise, I'm good with it.

return this[kOptions]
}

get [kRunning] () {
let ret = 0
for (const ref of this[kClients].values()) {
Expand Down
4 changes: 2 additions & 2 deletions lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1951,8 +1951,8 @@ async function httpNetworkFetch (
body: fetchParams.controller.dispatcher.isMockActive ? request.body && request.body.source : body,
headers: request.headersList[kHeadersCaseInsensitive],
maxRedirections: 0,
bodyTimeout: 300_000,
headersTimeout: 300_000,
xconverge marked this conversation as resolved.
Show resolved Hide resolved
bodyTimeout: agent.options?.().bodyTimeout ?? 300_000,
headersTimeout: agent.options?.().headersTimeout ?? 300_000,
upgrade: request.mode === 'websocket' ? 'websocket' : undefined
},
{
Expand Down
123 changes: 123 additions & 0 deletions test/fetch/fetch-timeouts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
'use strict'

const { test } = require('tap')

const { fetch, errors, Agent } = require('../..')
const { createServer } = require('http')
const FakeTimers = require('@sinonjs/fake-timers')

test('Fetch should have a default timeout of 300 seconds triggered', (t) => {
const msToDelay = 300_000
t.setTimeout(undefined)
t.plan(1)

const clock = FakeTimers.install()
t.teardown(clock.uninstall.bind(clock))

const server = createServer((req, res) => {
setTimeout(() => {
res.end('hello')
}, msToDelay)
clock.tick(msToDelay + 1)
})
t.teardown(server.close.bind(server))

server.listen(0, () => {
fetch(`http://localhost:${server.address().port}`, {
path: '/',
method: 'GET',
dispatcher: new Agent({
connectTimeout: 0
})
})
.then(() => {
// This should not happen, a timeout error should occur
t.error(true)
})
.catch((err) => {
t.type(err.cause, errors.HeadersTimeoutError)
})

clock.tick(msToDelay - 1)
})
})

test('Fetch should have a default timeout of 300 seconds not triggered', (t) => {
const msToDelay = 299_000
t.setTimeout(undefined)
t.plan(1)

const clock = FakeTimers.install()
t.teardown(clock.uninstall.bind(clock))

const server = createServer((req, res) => {
setTimeout(() => {
res.end('hello')
}, msToDelay)
clock.tick(msToDelay + 1)
})
t.teardown(server.close.bind(server))

server.listen(0, () => {
fetch(`http://localhost:${server.address().port}`, {
path: '/',
method: 'GET',
dispatcher: new Agent({
connectTimeout: 0
})
})
.then((response) => response.text())
.then((response) => {
t.equal('hello', response)
t.end()
})
.catch((err) => {
// This should not happen, a timeout error should not occur
t.error(err)
})

clock.tick(msToDelay - 1)
})
})

test('Fetch very long request, timeout overridden so no error', (t) => {
const minutes = 6
const msToDelay = 1000 * 60 * minutes

t.setTimeout(undefined)
t.plan(1)

const clock = FakeTimers.install()
t.teardown(clock.uninstall.bind(clock))

const server = createServer((req, res) => {
setTimeout(() => {
res.end('hello')
}, msToDelay)
clock.tick(msToDelay + 1)
})
t.teardown(server.close.bind(server))

server.listen(0, () => {
fetch(`http://localhost:${server.address().port}`, {
path: '/',
method: 'GET',
dispatcher: new Agent({
headersTimeout: 0,
connectTimeout: 0,
bodyTimeout: 0
})
})
.then((response) => response.text())
.then((response) => {
t.equal('hello', response)
t.end()
})
.catch((err) => {
// This should not happen, a timeout error should not occur
t.error(err)
})

clock.tick(msToDelay - 1)
})
})
48 changes: 48 additions & 0 deletions test/fetch/request-long.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict'

const { test } = require('tap')

const { Client } = require('../..')
const { createServer } = require('http')
const FakeTimers = require('@sinonjs/fake-timers')

const minutes = 6
const msToDelay = 1000 * 60 * minutes

test('Long time for a single request', (t) => {
t.setTimeout(undefined)

t.plan(2)

const clock = FakeTimers.install()
t.teardown(clock.uninstall.bind(clock))

const server = createServer((req, res) => {
setTimeout(() => {
res.end('hello')
}, msToDelay)
clock.tick(msToDelay + 1)
})
t.teardown(server.close.bind(server))

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`, {
headersTimeout: 0,
connectTimeout: 0
})
t.teardown(client.destroy.bind(client))

client.request({ path: '/', method: 'GET' }, (err, response) => {
t.error(err)
const bufs = []
response.body.on('data', (buf) => {
bufs.push(buf)
})
response.body.on('end', () => {
t.equal('hello', Buffer.concat(bufs).toString('utf8'))
})
})

clock.tick(msToDelay - 1)
})
})
16 changes: 0 additions & 16 deletions test/node-fetch/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1660,20 +1660,4 @@ describe('node-fetch', () => {
expect(res.ok).to.be.false
})
})

// it('should not time out waiting for a response 60 seconds', function () {
// this.timeout(65_000)
// return fetch(`${base}timeout60s`).then(res => {
// expect(res.status).to.equal(200)
// expect(res.ok).to.be.true
// return res.text().then(result => {
// expect(result).to.equal('text')
// })
// })
// })

// it('should time out waiting for more than 300 seconds', function () {
// this.timeout(305_000)
// return expect(fetch(`${base}timeout300s`)).to.eventually.be.rejectedWith(TypeError)
// })
})
16 changes: 0 additions & 16 deletions test/node-fetch/utils/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,22 +227,6 @@ module.exports = class TestServer {
}, 1000)
}

if (p === '/timeout60s') {
setTimeout(() => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('text')
}, 60_000)
}

if (p === '/timeout300s') {
setTimeout(() => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('text')
}, 300_000)
}

if (p === '/slow') {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
Expand Down