Skip to content

Commit

Permalink
test: add specific test for fix
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed Mar 19, 2024
1 parent d70b291 commit 2ceb341
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions test/retry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,104 @@ const { RequestHandler } = require('../lib/api/api-request')
test('Should retry status code', async t => {
t = tspl(t, { plan: 4 })

let counter = 0
const chunks = []
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: 'GET',
path: '/',
headers: {
'content-type': 'application/json'
}
}

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: {
onConnect () {
t.ok(true, 'pass')
},
onBodySent () {
t.ok(true, 'pass')
},
onHeaders (status, _rawHeaders, resume, _statusMessage) {
t.strictEqual(status, 200)
return true
},
onData (chunk) {
chunks.push(chunk)
return true
},
onComplete () {
t.strictEqual(Buffer.concat(chunks).toString('utf-8'), 'hello world!')
t.strictEqual(counter, 2)
},
onError () {
t.fail()
}
}
})

after(async () => {
await client.close()
server.close()

await once(server, 'close')
})

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

await t.completed
})

test('Should account for network and response errors', async t => {
t = tspl(t, { plan: 4 })

let counter = 0
const chunks = []
const server = createServer()
Expand Down

0 comments on commit 2ceb341

Please sign in to comment.