Skip to content
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
23 changes: 16 additions & 7 deletions lib/handler/RetryHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,22 @@ class RetryHandler {
this.retryCount += 1

if (statusCode >= 300) {
this.abort(
new RequestRetryError('Request failed', statusCode, {
headers,
count: this.retryCount
})
)
return false
if (this.retryOpts.statusCodes.includes(statusCode) === false) {
return this.handler.onHeaders(
statusCode,
rawHeaders,
resume,
statusMessage
)
} else {
this.abort(
new RequestRetryError('Request failed', statusCode, {
headers,
count: this.retryCount
})
)
return false
}
}

// Checkpoint for resume from where we left it
Expand Down
69 changes: 69 additions & 0 deletions test/retry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,72 @@ tap.test('retrying a request with a body', t => {
)
})
})

tap.test('should not error if request is not meant to be retried', t => {
const server = createServer()
server.on('request', (req, res) => {
res.writeHead(400)
res.end('Bad request')
})

t.plan(3)

const dispatchOptions = {
retryOptions: {
method: 'GET',
path: '/',
headers: {
'content-type': 'application/json'
}
}
}

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
const chunks = []
const handler = new RetryHandler(dispatchOptions, {
dispatch: client.dispatch.bind(client),
handler: {
onConnect () {
t.pass()
},
onBodySent () {
t.pass()
},
onHeaders (status, _rawHeaders, resume, _statusMessage) {
t.equal(status, 400)
return true
},
onData (chunk) {
chunks.push(chunk)
return true
},
onComplete () {
t.equal(Buffer.concat(chunks).toString('utf-8'), 'Bad request')
},
onError (err) {
console.log({ err })
t.fail()
}
}
})

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

await once(server, 'close')
})

client.dispatch(
{
method: 'GET',
path: '/',
headers: {
'content-type': 'application/json'
}
},
handler
)
})
})