-
Notifications
You must be signed in to change notification settings - Fork 575
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Avoid error for stream() being aborted (#2355)
Co-authored-by: BobNobrain <[email protected]>
- Loading branch information
1 parent
49254c3
commit daf349f
Showing
2 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict' | ||
|
||
const { test, skip } = require('tap') | ||
const { nodeMajor } = require('../lib/core/util') | ||
const { Writable } = require('stream') | ||
const { MockAgent, errors, stream } = require('..') | ||
|
||
if (nodeMajor < 16) { | ||
skip('only for node 16') | ||
process.exit(0) | ||
} | ||
|
||
test('stream() does not fail after request has been aborted', async (t) => { | ||
t.plan(1) | ||
|
||
const mockAgent = new MockAgent() | ||
|
||
mockAgent.disableNetConnect() | ||
mockAgent | ||
.get('http://localhost:3333') | ||
.intercept({ | ||
path: '/' | ||
}) | ||
.reply(200, 'ok') | ||
.delay(10) | ||
|
||
const parts = [] | ||
const ac = new AbortController() | ||
|
||
setTimeout(() => ac.abort('nevermind'), 5) | ||
|
||
try { | ||
await stream( | ||
'http://localhost:3333/', | ||
{ | ||
opaque: { parts }, | ||
signal: ac.signal, | ||
dispatcher: mockAgent | ||
}, | ||
({ opaque: { parts } }) => { | ||
return new Writable({ | ||
write (chunk, _encoding, callback) { | ||
parts.push(chunk) | ||
callback() | ||
} | ||
}) | ||
} | ||
) | ||
} catch (error) { | ||
console.log(error) | ||
t.equal(error instanceof errors.RequestAbortedError, true) | ||
} | ||
}) |