summaryrefslogtreecommitdiffstats
path: root/test/socket-timeout.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-21 20:56:19 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-21 20:56:19 +0000
commit0b6210cd37b68b94252cb798598b12974a20e1c1 (patch)
treee371686554a877842d95aa94f100bee552ff2a8e /test/socket-timeout.js
parentInitial commit. (diff)
downloadnode-undici-0b6210cd37b68b94252cb798598b12974a20e1c1.tar.xz
node-undici-0b6210cd37b68b94252cb798598b12974a20e1c1.zip
Adding upstream version 5.28.2+dfsg1+~cs23.11.12.3.upstream/5.28.2+dfsg1+_cs23.11.12.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--test/socket-timeout.js100
1 files changed, 100 insertions, 0 deletions
diff --git a/test/socket-timeout.js b/test/socket-timeout.js
new file mode 100644
index 0000000..8019c74
--- /dev/null
+++ b/test/socket-timeout.js
@@ -0,0 +1,100 @@
+'use strict'
+
+const { test } = require('tap')
+const { Client, errors } = require('..')
+const timers = require('../lib/timers')
+const { createServer } = require('http')
+const FakeTimers = require('@sinonjs/fake-timers')
+
+test('timeout with pipelining 1', (t) => {
+ t.plan(9)
+
+ const server = createServer()
+
+ server.once('request', (req, res) => {
+ t.pass('first request received, we are letting this timeout on the client')
+
+ server.once('request', (req, res) => {
+ t.equal('/', req.url)
+ t.equal('GET', req.method)
+ res.setHeader('content-type', 'text/plain')
+ res.end('hello')
+ })
+ })
+ t.teardown(server.close.bind(server))
+
+ server.listen(0, () => {
+ const client = new Client(`http://localhost:${server.address().port}`, {
+ pipelining: 1,
+ headersTimeout: 500,
+ bodyTimeout: 500
+ })
+ t.teardown(client.close.bind(client))
+
+ client.request({
+ path: '/',
+ method: 'GET',
+ opaque: 'asd'
+ }, (err, data) => {
+ t.type(err, errors.HeadersTimeoutError) // we are expecting an error
+ t.equal(data.opaque, 'asd')
+ })
+
+ client.request({
+ path: '/',
+ method: 'GET'
+ }, (err, { statusCode, headers, body }) => {
+ t.error(err)
+ t.equal(statusCode, 200)
+ t.equal(headers['content-type'], 'text/plain')
+ const bufs = []
+ body.on('data', (buf) => {
+ bufs.push(buf)
+ })
+ body.on('end', () => {
+ t.equal('hello', Buffer.concat(bufs).toString('utf8'))
+ })
+ })
+ })
+})
+
+test('Disable socket timeout', (t) => {
+ t.plan(2)
+
+ const server = createServer()
+ const clock = FakeTimers.install()
+ t.teardown(clock.uninstall.bind(clock))
+
+ const orgTimers = { ...timers }
+ Object.assign(timers, { setTimeout, clearTimeout })
+ t.teardown(() => {
+ Object.assign(timers, orgTimers)
+ })
+
+ server.once('request', (req, res) => {
+ setTimeout(() => {
+ res.end('hello')
+ }, 31e3)
+ clock.tick(32e3)
+ })
+ t.teardown(server.close.bind(server))
+
+ server.listen(0, () => {
+ const client = new Client(`http://localhost:${server.address().port}`, {
+ bodyTimeout: 0,
+ headersTimeout: 0
+ })
+ t.teardown(client.close.bind(client))
+
+ client.request({ path: '/', method: 'GET' }, (err, result) => {
+ t.error(err)
+ const bufs = []
+ result.body.on('data', (buf) => {
+ bufs.push(buf)
+ })
+ result.body.on('end', () => {
+ t.equal('hello', Buffer.concat(bufs).toString('utf8'))
+ })
+ })
+ })
+})