Improve error with invalid arguments to req.get()

fixes #2993
This commit is contained in:
Douglas Christopher Wilson 2016-06-03 00:07:34 -04:00
parent 5d642af8c3
commit c762b16f62
3 changed files with 34 additions and 1 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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)
})
})
})
})