Skip to content

Commit

Permalink
test: add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed Dec 31, 2024
1 parent b6d553a commit 403181f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
33 changes: 33 additions & 0 deletions test/fixtures/interceptors/retry-event-loop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

const { createServer } = require('node:http')
const { once } = require('node:events')
const {
Client,
interceptors: { retry }
} = require('../../..')

const server = createServer()

server.on('request', (req, res) => {
res.writeHead(418, { 'Content-Type': 'text/plain' })
res.end('teapot')
})

server.listen(0)
once(server, 'listening').then(() => {
const client = new Client(
`http://localhost:${server.address().port}`
).compose(
retry({
maxTimeout: 1000,
maxRetries: 3,
statusCodes: [418]
})
)

return client.request({
method: 'GET',
path: '/'
})
})
14 changes: 14 additions & 0 deletions test/interceptors/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { tspl } = require('@matteo.collina/tspl')
const { test, after } = require('node:test')
const { createServer } = require('node:http')
const { once } = require('node:events')
const { spawnSync } = require('node:child_process')

const { Client, interceptors } = require('../..')
const { retry, redirect, dns } = interceptors
Expand Down Expand Up @@ -559,3 +560,16 @@ test('should not error if request is not meant to be retried', async t => {
t.equal(response.statusCode, 400)
t.equal(await response.body.text(), 'Bad request')
})

test('#3975 - keep event loop ticking', async t => {
const suite = tspl(t, { plan: 3 })

const res = spawnSync('node', ['./test/fixtures/interceptors/retry-event-loop.js'], {
stdio: 'pipe'
})

const output = res.stderr.toString()
suite.ok(output.includes("code: 'UND_ERR_REQ_RETRY'"))
suite.ok(output.includes('RequestRetryError: Request failed'))
suite.ok(output.includes('statusCode: 418'))
})

0 comments on commit 403181f

Please sign in to comment.