Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(socket): propagate errors from destroy method (#1675)
Calling `destroy` on the the response, with an error, not correctly propagates through the socket and then the request object. This aligns with Node's native functionality. Fixes: #1669
- Loading branch information
1 parent
2e56fb0
commit de9c40b
Showing
3 changed files
with
49 additions
and
2 deletions.
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
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,43 @@ | ||
'use strict' | ||
|
||
const nock = require('..') | ||
const http = require('http') | ||
const { test } = require('tap') | ||
|
||
require('./cleanup_after_each')() | ||
|
||
test('req.destroy should emit error event if called with error', t => { | ||
nock('http://example.test') | ||
.get('/') | ||
.reply(404) | ||
|
||
http | ||
.get('http://example.test/', res => { | ||
if (res.statusCode !== 200) { | ||
res.destroy(new Error('Response error')) | ||
} | ||
}) | ||
.once('error', err => { | ||
t.type(err, Error) | ||
t.equal(err.message, 'Response error') | ||
t.end() | ||
}) | ||
}) | ||
|
||
test('req.destroy should not emit error event if called without error', t => { | ||
nock('http://example.test') | ||
.get('/') | ||
.reply(403) | ||
|
||
http | ||
.get('http://example.test/', res => { | ||
if (res.statusCode === 403) { | ||
res.destroy() | ||
} | ||
|
||
t.end() | ||
}) | ||
.once('error', () => { | ||
t.fail('should not emit error') | ||
}) | ||
}) |