Fix case where router.use skipped requests routes did not
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fixes #3037
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 8b6dc6c · Browse files · 3 files·+43 added−5 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+43 added−5 removed
- tests:+36 added−0 removed
- other:+7 added−5 removed
Provenance
- Commit
51f5290- Tree
e1df340- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
8b6dc6c
3 files changed+43 added−5 removed
modified:lib/router/index.js+6 added-5 removed
278 unmodified lines279 });280 }281 282 function trim_prefix(layer, layerError, layerPath, path) {283- var c = path[layerPath.length];284- if (c && '/' !== c && '.' !== c) return next(layerError);285-286- // Trim off the part of the url that matches the route287- // middleware (.use stuff) needs to have the path stripped283 if (layerPath.length !== 0) {284+ // Validate path breaks on a path separator285+ var c = path[layerPath.length]286+ if (c && c !== '/' && c !== '.') return next(layerError)287+288+ // Trim off the part of the url that matches the route289+ // middleware (.use stuff) needs to have the path stripped290 debug('trim prefix (%s) from url %s', layerPath, req.url);291 removed = layerPath;292 req.url = protohost + req.url.substr(protohost.length + removed.length);293 363 unmodified linesmodified:test/Router.js+36 added
345 unmodified lines346 347 assert.equal(count, methods.length);348 done();349 })350+351+ it('should be called for any URL when "*"', function (done) {352+ var cb = after(4, done)353+ var router = new Router()354+355+ function no () {356+ throw new Error('should not be called')357+ }358+359+ router.all('*', function (req, res) {360+ res.end()361+ })362+363+ router.handle({ url: '/', method: 'GET' }, { end: cb }, no)364+ router.handle({ url: '/foo', method: 'GET' }, { end: cb }, no)365+ router.handle({ url: 'foo', method: 'GET' }, { end: cb }, no)366+ router.handle({ url: '*', method: 'GET' }, { end: cb }, no)367+ })368 })369 370 describe('.use', function() {371 it('should require arguments', function(){8 unmodified lines380 router.use.bind(router, '/', null).should.throw(/requires middleware function.*Null/)381 router.use.bind(router, '/', new Date()).should.throw(/requires middleware function.*Date/)382 })383 384+ it('should be called for any URL', function (done) {385+ var cb = after(4, done)386+ var router = new Router()387+388+ function no () {389+ throw new Error('should not be called')390+ }391+392+ router.use(function (req, res) {393+ res.end()394+ })395+396+ router.handle({ url: '/', method: 'GET' }, { end: cb }, no)397+ router.handle({ url: '/foo', method: 'GET' }, { end: cb }, no)398+ router.handle({ url: 'foo', method: 'GET' }, { end: cb }, no)399+ router.handle({ url: '*', method: 'GET' }, { end: cb }, no)400+ })401+402 it('should accept array of middleware', function(done){403 var count = 0;404 var router = new Router();405 146 unmodified linesmodified:History.md+1 added
1 unreleased2 ==========3 4 * Add debug message when loading view engine5+ * Fix case where `router.use` skipped requests routes did not6 * Remove usage of `res._headers` private field7 - Improves compatibility with Node.js 8 nightly8 * Skip routing when `req.url` is not set9 * Use `%o` in path debug to tell types apart3222 unmodified lines