Skip to content

Commit

Permalink
fix: fix custom request instantiation (#191)
Browse files Browse the repository at this point in the history
Custom requests should preserve the stream attributes of the request,
namely the `_readableState` property inherited from `Readable`.
  • Loading branch information
wilkmaia committed May 4, 2022
1 parent cdfeeb5 commit 292f75e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ class MockSocket extends EventEmitter {
* @param {any} [options.payload]
*/
function CustomRequest (options) {
return new _CustomLMRRequest()
return new _CustomLMRRequest(this)

function _CustomLMRRequest () {
Request.call(this, {
function _CustomLMRRequest (obj) {
Request.call(obj, {
...options,
Request: undefined
})
Object.assign(this, obj)

for (const fn of Object.keys(Request.prototype)) {
this.constructor.prototype[fn] = Request.prototype[fn]
Expand Down
13 changes: 13 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ test('request inherits from custom class', (t) => {
})
})

test('request with custom class preserves stream data', (t) => {
t.plan(2)
const dispatch = function (req, res) {
t.ok(req._readableState)
res.writeHead(200)
res.end()
}

inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello', Request: http.IncomingMessage }, (err, res) => {
t.error(err)
})
})

test('assert Request option has a valid prototype', (t) => {
t.plan(2)
const dispatch = function (req, res) {
Expand Down

0 comments on commit 292f75e

Please sign in to comment.