Skip to content

Commit

Permalink
Improve error with invalid arguments to req.get()
Browse files Browse the repository at this point in the history
fixes #2993
  • Loading branch information
dougwilson committed Jun 3, 2016
1 parent 5d642af commit c762b16
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ unreleased
* Add `options` argument to `req.range`
- Includes the `combine` option
* Fix Windows absolute path check using forward slashes
* Improve error with invalid arguments to `req.get()`
* Improve performance for `res.json`/`res.jsonp` in most cases
* deps: accepts@~1.3.3
- Fix including type extensions in parameters in `Accept` parsing
Expand Down
8 changes: 8 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ var req = exports = module.exports = {

req.get =
req.header = function header(name) {
if (!name) {
throw new TypeError('name argument is required to req.get');
}

if (typeof name !== 'string') {
throw new TypeError('name must be a string to req.get');
}

var lc = name.toLowerCase();

switch (lc) {
Expand Down
26 changes: 25 additions & 1 deletion test/req.get.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,29 @@ describe('req', function(){
.set('Referrer', 'http://foobar.com')
.expect('http://foobar.com', done);
})

it('should throw missing header name', function (done) {
var app = express()

app.use(function (req, res) {
res.end(req.get())
})

request(app)
.get('/')
.expect(500, /TypeError: name argument is required to req.get/, done)
})

it('should throw for non-string header name', function (done) {
var app = express()

app.use(function (req, res) {
res.end(req.get(42))
})

request(app)
.get('/')
.expect(500, /TypeError: name must be a string to req.get/, done)
})
})
})
})

0 comments on commit c762b16

Please sign in to comment.