Skip to content

Commit

Permalink
Fix fetch parameters not being applied correctly (nodejs#1870)
Browse files Browse the repository at this point in the history
* Fix default fetch parameters

* Preserve existing behavior with 300 second timeout if not defined

* Add test for 300 second timeout as default

* Cleanup old unused tests

* Simplify how fetch utilizes timeouts from agent
  • Loading branch information
xconverge authored and crysmags committed Feb 27, 2024
1 parent a1322fd commit 1e4508d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 34 deletions.
2 changes: 0 additions & 2 deletions lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1951,8 +1951,6 @@ 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,
upgrade: request.mode === 'websocket' ? 'websocket' : undefined
},
{
Expand Down
49 changes: 49 additions & 0 deletions test/fetch/fetch-timeouts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict'

const { test } = require('tap')

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

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)
})
})
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

0 comments on commit 1e4508d

Please sign in to comment.