Skip to content

Commit

Permalink
Fix emitted 416 error missing headers property
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Mar 2, 2022
1 parent 53f0ab4 commit 24b4af2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==========

* Fix emitted 416 error missing headers property
* deps: destroy@1.1.1

0.17.2 / 2021-12-11
Expand Down
22 changes: 19 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,7 @@ SendStream.prototype.maxage = deprecate.function(function maxage (maxAge) {
SendStream.prototype.error = function error (status, err) {
// emit if listeners instead of responding
if (hasListeners(this, 'error')) {
return this.emit('error', createError(status, err, {
expose: false
}))
return this.emit('error', createHttpError(status, err))
}

var res = this.res
Expand Down Expand Up @@ -974,6 +972,24 @@ function createHtmlDocument (title, body) {
'</html>\n'
}

/**
* Create a HttpError object from simple arguments.
*
* @param {number} status
* @param {Error|object} err
* @private
*/

function createHttpError (status, err) {
if (!err) {
return createError(status)
}

return err instanceof Error
? createError(status, err, { expose: false })
: createError(status, err)
}

/**
* decodeURIComponent.
*
Expand Down
18 changes: 18 additions & 0 deletions test/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,24 @@ describe('send(file).pipe(res)', function () {
.expect('Content-Range', 'bytes */9')
.expect(416, done)
})

it('should emit error 416 with content-range header', function (done) {
var server = http.createServer(function (req, res) {
send(req, req.url, { root: fixtures })
.on('error', function (err) {
res.setHeader('X-Content-Range', err.headers['Content-Range'])
res.statusCode = err.statusCode
res.end(err.message)
})
.pipe(res)
})

request(server)
.get('/nums.txt')
.set('Range', 'bytes=9-50')
.expect('X-Content-Range', 'bytes */9')
.expect(416, done)
})
})

describe('when syntactically invalid', function () {
Expand Down

0 comments on commit 24b4af2

Please sign in to comment.