Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
"@fastify/busboy": "3.2.0",
"@matteo.collina/tspl": "^0.2.0",
"@metcoder95/https-pem": "^1.0.0",
"@sinonjs/fake-timers": "^12.0.0",
"@sinonjs/fake-timers": "^15.0.0",
"@types/node": "^18.19.50",
"abort-controller": "^3.0.0",
"borp": "^0.20.0",
Expand Down
29 changes: 8 additions & 21 deletions test/client-keep-alive.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ const { createServer } = require('node:net')
const http = require('node:http')
const FakeTimers = require('@sinonjs/fake-timers')

const clock = FakeTimers.install({
shouldAdvanceTime: true,
toFake: ['Date'],
advanceTimeDelta: 1
})
after(() => clock.uninstall())

test('keep-alive header', async (t) => {
t = tspl(t, { plan: 2 })

Expand Down Expand Up @@ -46,9 +53,6 @@ test('keep-alive header', async (t) => {
test('keep-alive header 0', async (t) => {
t = tspl(t, { plan: 2 })

const clock = FakeTimers.install()
after(() => clock.uninstall())

const server = createServer((socket) => {
socket.write('HTTP/1.1 200 OK\r\n')
socket.write('Content-Length: 0\r\n')
Expand Down Expand Up @@ -101,9 +105,7 @@ test('keep-alive header 1', async (t) => {
}, (err, { body }) => {
t.ifError(err)
body.on('end', () => {
const timeout = setTimeout(() => {
t.fail()
}, 0)
const timeout = setTimeout(() => t.fail(), 0)
client.on('disconnect', () => {
t.ok(true, 'pass')
clearTimeout(timeout)
Expand Down Expand Up @@ -150,11 +152,6 @@ test('keep-alive header no postfix', async (t) => {
test('keep-alive not timeout', async (t) => {
t = tspl(t, { plan: 2 })

const clock = FakeTimers.install({
apis: ['setTimeout']
})
after(() => clock.uninstall())

const server = createServer((socket) => {
socket.write('HTTP/1.1 200 OK\r\n')
socket.write('Content-Length: 0\r\n')
Expand Down Expand Up @@ -192,11 +189,6 @@ test('keep-alive not timeout', async (t) => {
test('keep-alive threshold', async (t) => {
t = tspl(t, { plan: 2 })

const clock = FakeTimers.install({
apis: ['setTimeout']
})
after(() => clock.uninstall())

const server = createServer((socket) => {
socket.write('HTTP/1.1 200 OK\r\n')
socket.write('Content-Length: 0\r\n')
Expand Down Expand Up @@ -235,11 +227,6 @@ test('keep-alive threshold', async (t) => {
test('keep-alive max keepalive', async (t) => {
t = tspl(t, { plan: 2 })

const clock = FakeTimers.install({
apis: ['setTimeout']
})
after(() => clock.uninstall())

const server = createServer((socket) => {
socket.write('HTTP/1.1 200 OK\r\n')
socket.write('Content-Length: 0\r\n')
Expand Down
16 changes: 5 additions & 11 deletions test/client-reconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,24 @@ const { once } = require('node:events')
const { Client } = require('..')
const { createServer } = require('node:http')
const FakeTimers = require('@sinonjs/fake-timers')
const timers = require('../lib/util/timers')

test('multiple reconnect', async (t) => {
t = tspl(t, { plan: 5 })

let n = 0
const clock = FakeTimers.install()
after(() => clock.uninstall())

const orgTimers = { ...timers }
Object.assign(timers, { setTimeout, clearTimeout })
after(() => {
Object.assign(timers, orgTimers)
const clock = FakeTimers.install({
toFake: ['Date']
})
after(() => clock.uninstall())

const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
n === 0 ? res.destroy() : res.end('ok')
})
after(() => server.close())
await once(server.listen(0), 'listening')

server.listen(0)
await once(server, 'listening')
const client = new Client(`http://localhost:${server.address().port}`)
after(client.destroy.bind(client))
after(() => client.destroy())

client.request({ path: '/', method: 'GET' }, (err, data) => {
t.ok(err)
Expand Down
3 changes: 2 additions & 1 deletion test/interceptors/cache-revalidate-stale.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const FakeTimers = require('@sinonjs/fake-timers')

test('revalidates the request when the response is stale', async () => {
const clock = FakeTimers.install({
now: 1
now: 1,
toFake: ['Date']
})
after(() => clock.uninstall())

Expand Down
102 changes: 55 additions & 47 deletions test/socket-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ const { test, after } = require('node:test')
const { Client, errors } = require('..')
const { createServer } = require('node:http')
const FakeTimers = require('@sinonjs/fake-timers')
const { once } = require('node:events')

const clock = FakeTimers.install({
toFake: ['setTimeout', 'Date'],
shouldAdvanceTime: true,
now: 1
})
after(() => clock.uninstall())

test('timeout with pipelining 1', async (t) => {
t = tspl(t, { plan: 9 })
Expand All @@ -22,50 +30,51 @@ test('timeout with pipelining 1', async (t) => {
})
})
after(() => server.close())
await once(server.listen(0), 'listening')

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`, {
pipelining: 1,
headersTimeout: 500,
bodyTimeout: 500
})
after(() => client.close())

client.request({
path: '/',
method: 'GET',
opaque: 'asd'
}, (err, data) => {
t.ok(err instanceof errors.HeadersTimeoutError) // we are expecting an error
t.strictEqual(data.opaque, 'asd')
})
const client = new Client(`http://localhost:${server.address().port}`, {
pipelining: 1,
headersTimeout: 500,
bodyTimeout: 500
})
after(() => client.close())

client.request({
path: '/',
method: 'GET'
}, (err, { statusCode, headers, body }) => {
t.ifError(err)
t.strictEqual(statusCode, 200)
t.strictEqual(headers['content-type'], 'text/plain')
const bufs = []
body.on('data', (buf) => {
bufs.push(buf)
})
body.on('end', () => {
t.strictEqual('hello', Buffer.concat(bufs).toString('utf8'))
})
client.request({
path: '/',
method: 'GET',
opaque: 'asd'
}, (err, data) => {
t.ok(err instanceof errors.HeadersTimeoutError) // we are expecting an error
t.strictEqual(data.opaque, 'asd')
})

client.request({
path: '/',
method: 'GET'
}, (err, { statusCode, headers, body }) => {
t.ifError(err)
t.strictEqual(statusCode, 200)
t.strictEqual(headers['content-type'], 'text/plain')
const bufs = []
body.on('data', (buf) => {
bufs.push(buf)
})
body.on('end', () => {
t.strictEqual('hello', Buffer.concat(bufs).toString('utf8'))
})
})

await clock.nextAsync()
await clock.nextAsync()
await clock.nextAsync()

await t.completed
})

test('Disable socket timeout', async (t) => {
t = tspl(t, { plan: 2 })

const server = createServer({ joinDuplicateHeaders: true })
const clock = FakeTimers.install()
after(clock.uninstall.bind(clock))

server.once('request', (req, res) => {
setTimeout(() => {
Expand All @@ -75,22 +84,21 @@ test('Disable socket timeout', async (t) => {
})
after(() => server.close())

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`, {
bodyTimeout: 0,
headersTimeout: 0
await once(server.listen(0), 'listening')
const client = new Client(`http://localhost:${server.address().port}`, {
bodyTimeout: 0,
headersTimeout: 0
})
after(() => client.close())

client.request({ path: '/', method: 'GET' }, (err, result) => {
t.ifError(err)
const bufs = []
result.body.on('data', (buf) => {
bufs.push(buf)
})
after(() => client.close())

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

Expand Down
Loading