Skip to content

Commit

Permalink
Fix routing requests without method
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Feb 23, 2023
1 parent 9bc1742 commit 74beeac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 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 routing requests without method
* deps: body-parser@1.20.2
- Fix strict json error message on Node.js 19+
- deps: content-type@~1.0.5
Expand Down
9 changes: 7 additions & 2 deletions lib/router/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ Route.prototype._handles_method = function _handles_method(method) {
return true;
}

var name = method.toLowerCase();
// normalize name
var name = typeof method === 'string'
? method.toLowerCase()
: method

if (name === 'head' && !this.methods['head']) {
name = 'get';
Expand Down Expand Up @@ -103,8 +106,10 @@ Route.prototype.dispatch = function dispatch(req, res, done) {
if (stack.length === 0) {
return done();
}
var method = typeof req.method === 'string'
? req.method.toLowerCase()
: req.method

var method = req.method.toLowerCase();
if (method === 'head' && !this.methods['head']) {
method = 'get';
}
Expand Down
27 changes: 27 additions & 0 deletions test/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,33 @@ describe('Router', function(){
router.handle({ method: 'GET' }, {}, done)
})

it('handle missing method', function (done) {
var all = false
var router = new Router()
var route = router.route('/foo')
var use = false

route.post(function (req, res, next) { next(new Error('should not run')) })
route.all(function (req, res, next) {
all = true
next()
})
route.get(function (req, res, next) { next(new Error('should not run')) })

router.get('/foo', function (req, res, next) { next(new Error('should not run')) })
router.use(function (req, res, next) {
use = true
next()
})

router.handle({ url: '/foo' }, {}, function (err) {
if (err) return done(err)
assert.ok(all)
assert.ok(use)
done()
})
})

it('should not stack overflow with many registered routes', function(done){
this.timeout(5000) // long-running test

Expand Down

0 comments on commit 74beeac

Please sign in to comment.