Skip to content

Commit a38ca0f

Browse files
committed
test(mock): cover async body dispatch errors
Signed-off-by: marko1olo <barsukdana@gmail.com>
1 parent d1306b1 commit a38ca0f

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

test/mock-agent.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,57 @@ describe('MockAgent - dispatch', () => {
363363
t.assert.deepStrictEqual(events, ['start', 'sent'])
364364
})
365365

366+
test('should report async iterable request body errors', async (t) => {
367+
const baseUrl = 'http://localhost:9999'
368+
const expected = new Error('fail')
369+
const events = []
370+
371+
const mockAgent = new MockAgent()
372+
after(() => mockAgent.close())
373+
374+
const mockPool = mockAgent.get(baseUrl)
375+
mockPool.intercept({
376+
path: '/foo',
377+
method: 'POST'
378+
}).reply(200, 'hello')
379+
380+
async function * body () {
381+
yield Buffer.from('he')
382+
throw expected
383+
}
384+
385+
await new Promise((resolve, reject) => {
386+
mockAgent.dispatch({
387+
origin: baseUrl,
388+
path: '/foo',
389+
method: 'POST',
390+
body: body()
391+
}, {
392+
onBodySent (chunk) {
393+
events.push(`body:${chunk.toString()}`)
394+
},
395+
onRequestSent () {
396+
reject(new Error('request should not be marked sent'))
397+
},
398+
onResponseStart () {
399+
reject(new Error('response should not start'))
400+
},
401+
onResponseData () {},
402+
onResponseEnd () {},
403+
onResponseError (_controller, error) {
404+
try {
405+
t.assert.strictEqual(error, expected)
406+
resolve()
407+
} catch (assertionError) {
408+
reject(assertionError)
409+
}
410+
}
411+
})
412+
})
413+
414+
t.assert.deepStrictEqual(events, ['body:he'])
415+
})
416+
366417
test('should replay async iterable request bodies to reply callbacks after lifecycle hooks', async (t) => {
367418
const baseUrl = 'http://localhost:9999'
368419
const events = []

0 commit comments

Comments
 (0)