package-rebuilds/pkgs/node-undici/node-undici-5.28.4+dfsg1+~cs23.12.11/test/socket-timeout.js
Fabian Grünbichler afee63929c add node-undici for libgit2 1.8.1
Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
2024-09-10 11:23:24 +02:00

101 lines
2.4 KiB
JavaScript

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