Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make call to onBodySent conditional in RetryHandler #2478

Merged
merged 3 commits into from
Nov 30, 2023
Merged
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
1 change: 1 addition & 0 deletions lib/api/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,4 @@ function request (opts, callback) {
}

module.exports = request
module.exports.RequestHandler = RequestHandler
2 changes: 1 addition & 1 deletion lib/handler/RetryHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class RetryHandler {
}

onBodySent (chunk) {
return this.handler.onBodySent(chunk)
if (this.handler.onBodySent) return this.handler.onBodySent(chunk)
}

static [kRetryHandlerDefaultRetry] (err, { state, opts }, cb) {
Expand Down
78 changes: 78 additions & 0 deletions test/retry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { once } = require('node:events')
const tap = require('tap')

const { RetryHandler, Client } = require('..')
const { RequestHandler } = require('../lib/api/api-request')

tap.test('Should retry status code', t => {
let counter = 0
Expand Down Expand Up @@ -542,3 +543,80 @@ tap.test('Should handle 206 partial content - bad-etag', t => {
})
})
})

tap.test('retrying a request with a body', t => {
let counter = 0
const server = createServer()
const dispatchOptions = {
retryOptions: {
retry: (err, { state, opts }, done) => {
counter++

if (
err.statusCode === 500 ||
err.message.includes('other side closed')
) {
setTimeout(done, 500)
return
}

return done(err)
}
},
method: 'POST',
path: '/',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({ hello: 'world' })
}

t.plan(1)

server.on('request', (req, res) => {
switch (counter) {
case 0:
req.destroy()
return
case 1:
res.writeHead(500)
res.end('failed')
return
case 2:
res.writeHead(200)
res.end('hello world!')
return
default:
t.fail()
}
})

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
const handler = new RetryHandler(dispatchOptions, {
dispatch: client.dispatch.bind(client),
handler: new RequestHandler(dispatchOptions, (err, data) => {
t.error(err)
})
})

t.teardown(async () => {
await client.close()
server.close()

await once(server, 'close')
})

client.dispatch(
{
method: 'POST',
path: '/',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({ hello: 'world' })
},
handler
)
})
})
Loading