Fix routing regression when altering req.method
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent b4b2efe · Browse files · 3 files·+40 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+40 added−6 removed
- tests:+31 added−1 removed
- other:+9 added−5 removed
Provenance
- Commit
23a49ff- Tree
bc432a7- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
b4b2efe
3 files changed+40 added−6 removed
modified:lib/router/index.js+4 added-5 removed
113 unmodified lines114 var self = this;115 116 debug('dispatching %s %s', req.method, req.url);117 118- var method = req.method.toLowerCase();119-118 var search = 1 + req.url.indexOf('?');119 var pathlength = search ? search - 1 : req.url.length;120 var fqdn = 1 + req.url.substr(0, pathlength).indexOf('://');121 var protohost = fqdn ? req.url.substr(0, req.url.indexOf('/', 2 + fqdn)) : '';17 unmodified lines139 // setup next layer140 req.next = next;141 142 // for options requests, respond with a default if nothing else responds145- if (method === 'options') {143+ if (req.method === 'OPTIONS') {144 done = wrap(done, function(old, err) {145 if (err || options.length === 0) return old(err);146 147 var body = options.join(',');40 unmodified lines188 if (err) {189 return next(err);190 }191 192+ var method = req.method;193 var has_method = route._handles_method(method);194 195 // build up automatic options response197- if (!has_method && method === 'options') {196+ if (!has_method && method === 'OPTIONS') {197 options.push.apply(options, route._options());198 }199 200 // don't even bother202- if (!has_method && method !== 'head') {201+ if (!has_method && method !== 'HEAD') {202 return next();203 }204 205 // we can now dispatch to the route313 unmodified linesmodified:test/app.router.js+31 added-1 removed
1 2+var after = require('after');3 var express = require('../')4 , request = require('supertest')5 , assert = require('assert')6 , methods = require('methods');25 unmodified lines32 .expect('x-user-id', '1')33 .expect(200, '1', done);34 })35 35- describe('methods supported', function(){36+ describe('methods', function(){37 methods.concat('del').forEach(function(method){38 if (method === 'connect') return;39 40 it('should include ' + method.toUpperCase(), function(done){17 unmodified lines58 var app = express();59 app[method].bind(app, '/', 3).should.throw(/Number/);60 })61 });62+63+ it('should re-route when method is altered', function (done) {64+ var app = express();65+ var cb = after(3, done);66+67+ app.use(function (req, res, next) {68+ if (req.method !== 'POST') return next();69+ req.method = 'DELETE';70+ res.setHeader('X-Method-Altered', '1');71+ next();72+ });73+74+ app.delete('/', function (req, res) {75+ res.end('deleted everything');76+ });77+78+ request(app)79+ .get('/')80+ .expect(404, 'Cannot GET /\n', cb);81+82+ request(app)83+ .delete('/')84+ .expect(200, 'deleted everything', cb);85+86+ request(app)87+ .post('/')88+ .expect('X-Method-Altered', '1')89+ .expect(200, 'deleted everything', cb);90+ });91 })92 93 describe('decode querystring', function(){94 it('should decode correct params', function(done){726 unmodified linesmodified:History.md+5 added
1+unreleased2+==========3+4+ * fix routing regression when altering `req.method`5+6 4.5.0 / 2014-07-047 ==================8 9 * add deprecation message to non-plural `req.accepts*`1738 unmodified lines