Skip to content

Commit c762b16

Browse files
committed
Improve error with invalid arguments to req.get()
fixes #2993
1 parent 5d642af commit c762b16

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ unreleased
44
* Add `options` argument to `req.range`
55
- Includes the `combine` option
66
* Fix Windows absolute path check using forward slashes
7+
* Improve error with invalid arguments to `req.get()`
78
* Improve performance for `res.json`/`res.jsonp` in most cases
89
* deps: accepts@~1.3.3
910
- Fix including type extensions in parameters in `Accept` parsing

lib/request.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ var req = exports = module.exports = {
5757

5858
req.get =
5959
req.header = function header(name) {
60+
if (!name) {
61+
throw new TypeError('name argument is required to req.get');
62+
}
63+
64+
if (typeof name !== 'string') {
65+
throw new TypeError('name must be a string to req.get');
66+
}
67+
6068
var lc = name.toLowerCase();
6169

6270
switch (lc) {

test/req.get.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,29 @@ describe('req', function(){
3131
.set('Referrer', 'http://foobar.com')
3232
.expect('http://foobar.com', done);
3333
})
34+
35+
it('should throw missing header name', function (done) {
36+
var app = express()
37+
38+
app.use(function (req, res) {
39+
res.end(req.get())
40+
})
41+
42+
request(app)
43+
.get('/')
44+
.expect(500, /TypeError: name argument is required to req.get/, done)
45+
})
46+
47+
it('should throw for non-string header name', function (done) {
48+
var app = express()
49+
50+
app.use(function (req, res) {
51+
res.end(req.get(42))
52+
})
53+
54+
request(app)
55+
.get('/')
56+
.expect(500, /TypeError: name must be a string to req.get/, done)
57+
})
3458
})
35-
})
59+
})

0 commit comments

Comments
 (0)