From 06370341b0c9517eb3fc120ee5f4b7d394b04fc7 Mon Sep 17 00:00:00 2001 From: Wes Todd Date: Sun, 5 Jan 2020 09:04:10 -0800 Subject: [PATCH 01/11] build: disable package-lock.json in .npmrc --- .npmrc | 1 + 1 file changed, 1 insertion(+) create mode 100644 .npmrc diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false From a0f0c5533b0cf31e35cd47d9f85f2e15d057f4c8 Mon Sep 17 00:00:00 2001 From: Wes Todd Date: Sun, 5 Jan 2020 09:05:51 -0800 Subject: [PATCH 02/11] deps: mocha@7.0.0 --- HISTORY.md | 5 +++++ package.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 72ac292..ebe37c8 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,8 @@ +unreleased +========== + + * deps: mocha@7.0.0 + 2.0.0-alpha.1 / 2018-07-27 ========================== diff --git a/package.json b/package.json index 41328ab..baafa60 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "eslint": "3.19.0", "eslint-plugin-markdown": "1.0.0-beta.6", "finalhandler": "1.1.1", - "mocha": "3.5.3", + "mocha": "7.0.0", "nyc": "10.3.2", "supertest": "1.2.0" }, From 2d84b21d6f84421df4d17912b011226e0ee8df20 Mon Sep 17 00:00:00 2001 From: Wes Todd Date: Sun, 5 Jan 2020 09:24:47 -0800 Subject: [PATCH 03/11] deps: supertest@4.0.2 --- HISTORY.md | 1 + package.json | 2 +- test/route.js | 2 +- test/router.js | 4 ++-- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index ebe37c8..2acdc27 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,6 +2,7 @@ unreleased ========== * deps: mocha@7.0.0 + * deps: supertest@4.0.2 2.0.0-alpha.1 / 2018-07-27 ========================== diff --git a/package.json b/package.json index baafa60..00d6a05 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "finalhandler": "1.1.1", "mocha": "7.0.0", "nyc": "10.3.2", - "supertest": "1.2.0" + "supertest": "4.0.2" }, "files": [ "lib/", diff --git a/test/route.js b/test/route.js index d680272..a221178 100644 --- a/test/route.js +++ b/test/route.js @@ -194,7 +194,7 @@ describe('Router', function () { var body = method !== 'head' ? 'hello, world' - : '' + : undefined describe('.' + method + '(...fn)', function () { it('should respond to a ' + method.toUpperCase() + ' request', function (done) { diff --git a/test/router.js b/test/router.js index 0bb89a4..25f08d8 100644 --- a/test/router.js +++ b/test/router.js @@ -54,7 +54,7 @@ describe('Router', function () { var body = method !== 'head' ? 'hello, world' - : '' + : undefined request(server) [method]('/') @@ -262,7 +262,7 @@ describe('Router', function () { var body = method !== 'head' ? 'hello, world' - : '' + : undefined describe('.' + method + '(path, ...fn)', function () { it('should be chainable', function () { From 6e57d667d658f2165d8192264c8d5f18261edebe Mon Sep 17 00:00:00 2001 From: Wes Todd Date: Sun, 5 Jan 2020 09:28:10 -0800 Subject: [PATCH 04/11] deps: nyc@15.0.0 --- HISTORY.md | 1 + package.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 2acdc27..97735ad 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,6 +3,7 @@ unreleased * deps: mocha@7.0.0 * deps: supertest@4.0.2 + * deps: nyc@15.0.0 2.0.0-alpha.1 / 2018-07-27 ========================== diff --git a/package.json b/package.json index 00d6a05..7a25845 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "eslint-plugin-markdown": "1.0.0-beta.6", "finalhandler": "1.1.1", "mocha": "7.0.0", - "nyc": "10.3.2", + "nyc": "15.0.0", "supertest": "4.0.2" }, "files": [ From 27e77bff5f07fc7b36a90eedcfc7b7af60c41184 Mon Sep 17 00:00:00 2001 From: Wes Todd Date: Wed, 17 Jul 2019 21:58:24 -0700 Subject: [PATCH 05/11] deps: setprototypeof@1.2.0 --- HISTORY.md | 1 + package.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 97735ad..2d82855 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -4,6 +4,7 @@ unreleased * deps: mocha@7.0.0 * deps: supertest@4.0.2 * deps: nyc@15.0.0 + * deps: setprototypeof@1.2.0 2.0.0-alpha.1 / 2018-07-27 ========================== diff --git a/package.json b/package.json index 7a25845..a8cd48d 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "methods": "~1.1.2", "parseurl": "~1.3.2", "path-to-regexp": "0.1.7", - "setprototypeof": "1.1.0", + "setprototypeof": "1.2.0", "utils-merge": "1.0.1" }, "devDependencies": { From 8d15e8d5679850108242872ffb7adad86f0ed472 Mon Sep 17 00:00:00 2001 From: Wes Todd Date: Fri, 13 Oct 2017 17:02:56 -0500 Subject: [PATCH 06/11] dep: path-to-regexp@3.0.0 --- HISTORY.md | 1 + README.md | 58 ++++++++++++++++++++++++++++++++++++++++++++++ lib/layer.js | 18 ++++++++++++-- package.json | 2 +- test/req.params.js | 4 ++-- test/route.js | 20 ++++++++-------- 6 files changed, 88 insertions(+), 15 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 2d82855..b5b7b19 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -5,6 +5,7 @@ unreleased * deps: supertest@4.0.2 * deps: nyc@15.0.0 * deps: setprototypeof@1.2.0 + * deps: path-to-regexp@3.0.0 2.0.0-alpha.1 / 2018-07-27 ========================== diff --git a/README.md b/README.md index 6c5bddf..90f662a 100644 --- a/README.md +++ b/README.md @@ -382,6 +382,64 @@ router.route('/pet/:id') server.listen(8080) ``` +## Migrating to 2.x from 1.x + +The main change is the update to `path-to-regexp@2.0.0`, which has a few breaking changes: + +#### No longer a direct conversion to a RegExp with sugar on top. + +It's a path matcher with named and unnamed matching groups. It's unlikely you previously abused this feature, +it's rare and you could always use a RegExp instead. An example of this would be: + +```javascript +// Used to work +router.get('/\\d+') + +// Now requires matching group +router.get('/(\\d+)') +``` + +#### All matching RegExp special characters can be used in a matching group. + +Other RegExp features are not supported - no nested matching groups, non-capturing groups or look aheads +There is really only one common change is needing replacing any routes with `*` to `(.*)`. Some examples: + +- `/:user(*)` becomes `/:user(.*)` +- `/:user/*` becomes `/:user/(.*)` +- `/foo/*/bar` becomes `/foo/(.*)/bar` + +#### Parameters have suffixes that augment meaning - `*`, `+` and `?`. E.g. `/:user*` + +Needs more info. + +#### Named params with regex no longer define positionally. + +One other small change (hopefully low impact), is that named parameters with regular expressions no longer result in positional +values in the `params` object. An example is: + +```javascript +router.get('/:foo(.*)') + +// old GET /bar +console.log(req.params) // {0: 'bar', 'foo': 'bar'} + +// new GET /bar +console.log(req.params) // {'foo': 'bar'} +``` + +#### Partial matching, prefer escaping delimiter + +The update to `path-to-regexp@3` includes a change to how partial matches are handled, +now you should escape the delimiter before a partial match segment. For example: + +```javascript +// old +router.get('/user(s)?/:user/:op') + +// new +router.get('\\/user(s)?/:user/:op') +``` + ## License [MIT](LICENSE) diff --git a/lib/layer.js b/lib/layer.js index c012165..e6d8576 100644 --- a/lib/layer.js +++ b/lib/layer.js @@ -28,14 +28,28 @@ var hasOwnProperty = Object.prototype.hasOwnProperty module.exports = Layer -function Layer(path, options, fn) { +function Layer(p, options, fn) { if (!(this instanceof Layer)) { - return new Layer(path, options, fn) + return new Layer(p, options, fn) } debug('new %o', path) var opts = options || {} + // If not in strict allow both with or without trailing slash + var path = p + if (!opts.strict) { + if (!Array.isArray(path) && path !== '/' && path[path.length - 1] === '/') { + path = path.substr(0, path.length - 1) + } else { + for (var i = 0; i < path.length; i++) { + if (path[i] !== '/' && path[i][path[i].length - 1] === '/') { + path[i] = path[i].substr(0, path[i].length - 1) + } + } + } + } + this.handle = fn this.name = fn.name || '' this.params = undefined diff --git a/package.json b/package.json index a8cd48d..04e09d3 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "debug": "3.1.0", "methods": "~1.1.2", "parseurl": "~1.3.2", - "path-to-regexp": "0.1.7", + "path-to-regexp": "^3.0.0", "setprototypeof": "1.2.0", "utils-merge": "1.0.1" }, diff --git a/test/req.params.js b/test/req.params.js index c7dbba2..57c82bf 100644 --- a/test/req.params.js +++ b/test/req.params.js @@ -137,7 +137,7 @@ describe('req.params', function () { }) }) - router.get('/*', hitParams(1)) + router.get('/(.*)', hitParams(1)) request(server) .get('/buzz') @@ -156,7 +156,7 @@ describe('req.params', function () { }) }) - router.get('/*', hitParams(1)) + router.get('/(.*)', hitParams(1)) request(server) .get('/bar') diff --git a/test/route.js b/test/route.js index a221178..a39e6b3 100644 --- a/test/route.js +++ b/test/route.js @@ -645,7 +645,7 @@ describe('Router', function () { it('should work following a partial capture group', function (done) { var cb = after(2, done) var router = new Router() - var route = router.route('/user(s)?/:user/:op') + var route = router.route('\\/user(s)?/:user/:op') var server = createServer(router) route.all(sendParams) @@ -716,7 +716,7 @@ describe('Router', function () { it('should capture everything with pre- and post-fixes', function (done) { var router = new Router() - var route = router.route('/foo/*/bar') + var route = router.route('/foo/(.*)/bar') var server = createServer(router) route.all(sendParams) @@ -728,7 +728,7 @@ describe('Router', function () { it('should capture greedly', function (done) { var router = new Router() - var route = router.route('/foo/*/bar') + var route = router.route('/foo/(.*)/bar') var server = createServer(router) route.all(sendParams) @@ -740,7 +740,7 @@ describe('Router', function () { it('should be an optional capture', function (done) { var router = new Router() - var route = router.route('/foo*') + var route = router.route('/foo(.*)') var server = createServer(router) route.all(sendParams) @@ -753,7 +753,7 @@ describe('Router', function () { it('should require preceeding /', function (done) { var cb = after(2, done) var router = new Router() - var route = router.route('/foo/*') + var route = router.route('/foo/(.*)') var server = createServer(router) route.all(sendParams) @@ -770,23 +770,23 @@ describe('Router', function () { it('should work in a named parameter', function (done) { var cb = after(2, done) var router = new Router() - var route = router.route('/:foo(*)') + var route = router.route('/:foo(.*)') var server = createServer(router) route.all(sendParams) request(server) .get('/bar') - .expect(200, {'0': 'bar', 'foo': 'bar'}, cb) + .expect(200, {'foo': 'bar'}, cb) request(server) .get('/fizz/buzz') - .expect(200, {'0': 'fizz/buzz', 'foo': 'fizz/buzz'}, cb) + .expect(200, {'foo': 'fizz/buzz'}, cb) }) it('should work before a named parameter', function (done) { var router = new Router() - var route = router.route('/*/user/:id') + var route = router.route('/(.*)/user/:id') var server = createServer(router) route.all(sendParams) @@ -799,7 +799,7 @@ describe('Router', function () { it('should work within arrays', function (done) { var cb = after(3, done) var router = new Router() - var route = router.route(['/user/:id', '/foo/*', '/:action']) + var route = router.route(['/user/:id', '/foo/(.*)', '/:action']) var server = createServer(router) route.all(sendParams) From 705257e6c7c0bd4b5ae90250a9a16d0e35a3463c Mon Sep 17 00:00:00 2001 From: Wes Todd Date: Sun, 5 Jan 2020 09:49:46 -0800 Subject: [PATCH 07/11] build: drop node <10 and add 12 & 13 --- .travis.yml | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index c92003f..6d760ea 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,17 +1,8 @@ language: node_js node_js: - - "0.10" - - "0.12" - - "1.8" - - "2.5" - - "3.3" - - "4.9" - - "5.12" - - "6.14" - - "7.10" - - "8.11" - - "9.11" - "10.6" + - "12.14" + - "13.5" sudo: false cache: directories: From 1e6e24f57d138ef96e2fd01b576d058faf2ffafd Mon Sep 17 00:00:00 2001 From: Wes Todd Date: Sun, 5 Jan 2020 09:59:59 -0800 Subject: [PATCH 08/11] deps: eslint@6.8.0 --- HISTORY.md | 1 + package.json | 2 +- test/auto-head.js | 16 +- test/auto-options.js | 32 +-- test/fqdn-url.js | 22 +- test/param.js | 70 +++--- test/req.params.js | 50 ++-- test/route.js | 276 +++++++++++----------- test/router.js | 546 +++++++++++++++++++++---------------------- 9 files changed, 508 insertions(+), 507 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index b5b7b19..85c1ed0 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -6,6 +6,7 @@ unreleased * deps: nyc@15.0.0 * deps: setprototypeof@1.2.0 * deps: path-to-regexp@3.0.0 + * deps: eslint@6.8.0 2.0.0-alpha.1 / 2018-07-27 ========================== diff --git a/package.json b/package.json index 04e09d3..68fb956 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ }, "devDependencies": { "after": "0.8.2", - "eslint": "3.19.0", + "eslint": "6.8.0", "eslint-plugin-markdown": "1.0.0-beta.6", "finalhandler": "1.1.1", "mocha": "7.0.0", diff --git a/test/auto-head.js b/test/auto-head.js index f64a0fa..bb63145 100644 --- a/test/auto-head.js +++ b/test/auto-head.js @@ -13,10 +13,10 @@ describe('HEAD', function () { router.get('/users', sethit(1), saw) request(server) - .head('/users') - .expect('Content-Type', 'text/plain') - .expect('x-fn-1', 'hit') - .expect(200, done) + .head('/users') + .expect('Content-Type', 'text/plain') + .expect('x-fn-1', 'hit') + .expect(200, done) }) it('should invoke head if prior to get', function (done) { @@ -27,10 +27,10 @@ describe('HEAD', function () { router.get('/users', sethit(2), saw) request(server) - .head('/users') - .expect('Content-Type', 'text/plain') - .expect('x-fn-1', 'hit') - .expect(200, done) + .head('/users') + .expect('Content-Type', 'text/plain') + .expect('x-fn-1', 'hit') + .expect(200, done) }) }) diff --git a/test/auto-options.js b/test/auto-options.js index cd7a0b4..ac90959 100644 --- a/test/auto-options.js +++ b/test/auto-options.js @@ -16,9 +16,9 @@ describe('OPTIONS', function () { router.put('/users', saw) request(server) - .options('/users') - .expect('Allow', 'GET, HEAD, POST, PUT') - .expect(200, 'GET, HEAD, POST, PUT', done) + .options('/users') + .expect('Allow', 'GET, HEAD, POST, PUT') + .expect(200, 'GET, HEAD, POST, PUT', done) }) it('should not contain methods multiple times', function (done) { @@ -31,9 +31,9 @@ describe('OPTIONS', function () { router.get('/users', saw) request(server) - .options('/users') - .expect('GET, HEAD, PUT') - .expect('Allow', 'GET, HEAD, PUT', done) + .options('/users') + .expect('GET, HEAD, PUT') + .expect('Allow', 'GET, HEAD, PUT', done) }) it('should not include "all" routes', function (done) { @@ -46,10 +46,10 @@ describe('OPTIONS', function () { router.all('/users', sethit(1)) request(server) - .options('/users') - .expect('x-fn-1', 'hit') - .expect('Allow', 'GET, HEAD, PUT') - .expect(200, 'GET, HEAD, PUT', done) + .options('/users') + .expect('x-fn-1', 'hit') + .expect('Allow', 'GET, HEAD, PUT') + .expect(200, 'GET, HEAD, PUT', done) }) it('should not respond if no matching path', function (done) { @@ -59,8 +59,8 @@ describe('OPTIONS', function () { router.get('/users', saw) request(server) - .options('/') - .expect(404, done) + .options('/') + .expect(404, done) }) it('should do nothing with explicit options route', function (done) { @@ -71,8 +71,8 @@ describe('OPTIONS', function () { router.options('/users', saw) request(server) - .options('/users') - .expect(200, 'saw OPTIONS /users', done) + .options('/users') + .expect(200, 'saw OPTIONS /users', done) }) describe('when error occurs in respone handler', function () { @@ -88,8 +88,8 @@ describe('OPTIONS', function () { router.get('/users', saw) request(server) - .options('/users') - .expect(200, 'true', done) + .options('/users') + .expect(200, 'true', done) }) }) }) diff --git a/test/fqdn-url.js b/test/fqdn-url.js index 5411500..7fc0b71 100644 --- a/test/fqdn-url.js +++ b/test/fqdn-url.js @@ -13,8 +13,8 @@ describe('FQDN url', function () { router.use(saw) rawrequest(server) - .get('http://example.com/foo') - .expect(200, 'saw GET http://example.com/foo', done) + .get('http://example.com/foo') + .expect(200, 'saw GET http://example.com/foo', done) }) it('should strip/restore FQDN req.url', function (done) { @@ -25,9 +25,9 @@ describe('FQDN url', function () { router.use(saw) rawrequest(server) - .get('http://example.com/blog/post/1') - .expect('x-saw-1', 'GET http://example.com/post/1') - .expect(200, 'saw GET http://example.com/blog/post/1', done) + .get('http://example.com/blog/post/1') + .expect('x-saw-1', 'GET http://example.com/post/1') + .expect(200, 'saw GET http://example.com/blog/post/1', done) }) it('should ignore FQDN in search', function (done) { @@ -38,9 +38,9 @@ describe('FQDN url', function () { router.use(saw) rawrequest(server) - .get('/proxy?url=http://example.com/blog/post/1') - .expect('x-saw-1', 'GET /?url=http://example.com/blog/post/1') - .expect(200, 'saw GET /proxy?url=http://example.com/blog/post/1', done) + .get('/proxy?url=http://example.com/blog/post/1') + .expect('x-saw-1', 'GET /?url=http://example.com/blog/post/1') + .expect(200, 'saw GET /proxy?url=http://example.com/blog/post/1', done) }) it('should ignore FQDN in path', function (done) { @@ -51,9 +51,9 @@ describe('FQDN url', function () { router.use(saw) rawrequest(server) - .get('/proxy/http://example.com/blog/post/1') - .expect('x-saw-1', 'GET /http://example.com/blog/post/1') - .expect(200, 'saw GET /proxy/http://example.com/blog/post/1', done) + .get('/proxy/http://example.com/blog/post/1') + .expect('x-saw-1', 'GET /http://example.com/blog/post/1') + .expect(200, 'saw GET /proxy/http://example.com/blog/post/1', done) }) }) diff --git a/test/param.js b/test/param.js index e2a5aad..7a31a3c 100644 --- a/test/param.js +++ b/test/param.js @@ -48,12 +48,12 @@ describe('Router', function () { }) request(server) - .get('/user/2') - .expect(200, 'get user 2', cb) + .get('/user/2') + .expect(200, 'get user 2', cb) request(server) - .get('/user/bob') - .expect(200, 'get user NaN', cb) + .get('/user/bob') + .expect(200, 'get user NaN', cb) }) it('should allow chaining', function (done) { @@ -76,8 +76,8 @@ describe('Router', function () { }) request(server) - .get('/user/2') - .expect(200, 'get user 2 (2)', done) + .get('/user/2') + .expect(200, 'get user 2 (2)', done) }) it('should automatically decode path value', function (done) { @@ -95,8 +95,8 @@ describe('Router', function () { }) request(server) - .get('/user/%22bob%2Frobert%22') - .expect('get user "bob/robert"', done) + .get('/user/%22bob%2Frobert%22') + .expect('get user "bob/robert"', done) }) it('should 400 on invalid path value', function (done) { @@ -114,8 +114,8 @@ describe('Router', function () { }) request(server) - .get('/user/%bob') - .expect(400, /URIError: Failed to decode param/, done) + .get('/user/%bob') + .expect(400, /URIError: Failed to decode param/, done) }) it('should only invoke fn when necessary', function (done) { @@ -136,13 +136,13 @@ describe('Router', function () { router.put('/user/:id', saw) request(server) - .get('/user/bob') - .expect(500, /Error: boom/, cb) + .get('/user/bob') + .expect(500, /Error: boom/, cb) request(server) - .put('/user/bob') - .expect('x-id', 'bob') - .expect(200, 'saw PUT /user/bob', cb) + .put('/user/bob') + .expect('x-id', 'bob') + .expect(200, 'saw PUT /user/bob', cb) }) it('should only invoke fn once per request', function (done) { @@ -163,8 +163,8 @@ describe('Router', function () { }) request(server) - .get('/user/bob') - .expect('get user bob 1 times', done) + .get('/user/bob') + .expect('get user bob 1 times', done) }) it('should keep changes to req.params value', function (done) { @@ -187,8 +187,8 @@ describe('Router', function () { }) request(server) - .get('/user/01') - .expect('get user 1 1 times', done) + .get('/user/01') + .expect('get user 1 1 times', done) }) it('should invoke fn if path value differs', function (done) { @@ -210,8 +210,8 @@ describe('Router', function () { }) request(server) - .get('/user/bob') - .expect('get user bob 2 times: user, bob', done) + .get('/user/bob') + .expect('get user bob 2 times: user, bob', done) }) it('should catch exception in fn', function (done) { @@ -228,8 +228,8 @@ describe('Router', function () { }) request(server) - .get('/user/bob') - .expect(500, /Error: boom/, done) + .get('/user/bob') + .expect(500, /Error: boom/, done) }) it('should catch exception in chained fn', function (done) { @@ -250,8 +250,8 @@ describe('Router', function () { }) request(server) - .get('/user/bob') - .expect(500, /Error: boom/, done) + .get('/user/bob') + .expect(500, /Error: boom/, done) }) describe('next("route")', function () { @@ -283,16 +283,16 @@ describe('Router', function () { }) request(server) - .get('/user/2') - .expect(200, 'get user 2', cb) + .get('/user/2') + .expect(200, 'get user 2', cb) request(server) - .get('/user/bob') - .expect(404, cb) + .get('/user/bob') + .expect(404, cb) request(server) - .get('/user/new') - .expect(400, 'cannot get a new user', cb) + .get('/user/new') + .expect(400, 'cannot get a new user', cb) }) it('should invoke fn if path value differs', function (done) { @@ -314,10 +314,10 @@ describe('Router', function () { }) request(server) - .get('/user/bob') - .expect(shouldNotHitHandle(1)) - .expect(shouldHitHandle(2)) - .expect('get user bob 2 times: user, bob', done) + .get('/user/bob') + .expect(shouldNotHitHandle(1)) + .expect(shouldHitHandle(2)) + .expect('get user bob 2 times: user, bob', done) }) }) }) diff --git a/test/req.params.js b/test/req.params.js index 57c82bf..9a8ade9 100644 --- a/test/req.params.js +++ b/test/req.params.js @@ -13,8 +13,8 @@ describe('req.params', function () { router.get('/', sawParams) request(server) - .get('/') - .expect(200, '{}', done) + .get('/') + .expect(200, '{}', done) }) it('should not exist outside the router', function (done) { @@ -29,9 +29,9 @@ describe('req.params', function () { router.get('/', hitParams(1)) request(server) - .get('/') - .expect('x-params-1', '{}') - .expect(200, '', done) + .get('/') + .expect('x-params-1', '{}') + .expect(200, '', done) }) it('should overwrite value outside the router', function (done) { @@ -44,8 +44,8 @@ describe('req.params', function () { router.get('/', sawParams) request(server) - .get('/') - .expect(200, '{}', done) + .get('/') + .expect(200, '{}', done) }) it('should restore previous value outside the router', function (done) { @@ -62,9 +62,9 @@ describe('req.params', function () { router.get('/', hitParams(1)) request(server) - .get('/') - .expect('x-params-1', '{}') - .expect(200, '{"foo":"bar"}', done) + .get('/') + .expect('x-params-1', '{}') + .expect(200, '{"foo":"bar"}', done) }) describe('when "mergeParams: true"', function () { @@ -82,9 +82,9 @@ describe('req.params', function () { router.get('/:fizz', hitParams(1)) request(server) - .get('/buzz') - .expect('x-params-1', '{"foo":"bar","fizz":"buzz"}') - .expect(200, '{"foo":"bar"}', done) + .get('/buzz') + .expect('x-params-1', '{"foo":"bar","fizz":"buzz"}') + .expect(200, '{"foo":"bar"}', done) }) it('should ignore non-object outsite object', function (done) { @@ -101,9 +101,9 @@ describe('req.params', function () { router.get('/:fizz', hitParams(1)) request(server) - .get('/buzz') - .expect('x-params-1', '{"fizz":"buzz"}') - .expect(200, '42', done) + .get('/buzz') + .expect('x-params-1', '{"fizz":"buzz"}') + .expect(200, '42', done) }) it('should overwrite outside keys that are the same', function (done) { @@ -120,9 +120,9 @@ describe('req.params', function () { router.get('/:foo', hitParams(1)) request(server) - .get('/buzz') - .expect('x-params-1', '{"foo":"buzz"}') - .expect(200, '{"foo":"bar"}', done) + .get('/buzz') + .expect('x-params-1', '{"foo":"buzz"}') + .expect(200, '{"foo":"bar"}', done) }) describe('with numeric properties in req.params', function () { @@ -140,9 +140,9 @@ describe('req.params', function () { router.get('/(.*)', hitParams(1)) request(server) - .get('/buzz') - .expect('x-params-1', '{"0":"foo","1":"bar","2":"buzz"}') - .expect(200, '{"0":"foo","1":"bar"}', done) + .get('/buzz') + .expect('x-params-1', '{"0":"foo","1":"bar","2":"buzz"}') + .expect(200, '{"0":"foo","1":"bar"}', done) }) it('should merge with same numeric properties', function (done) { @@ -159,9 +159,9 @@ describe('req.params', function () { router.get('/(.*)', hitParams(1)) request(server) - .get('/bar') - .expect('x-params-1', '{"0":"foo","1":"bar"}') - .expect(200, '{"0":"foo"}', done) + .get('/bar') + .expect('x-params-1', '{"0":"foo","1":"bar"}') + .expect(200, '{"0":"foo"}', done) }) }) }) diff --git a/test/route.js b/test/route.js index a39e6b3..d48780e 100644 --- a/test/route.js +++ b/test/route.js @@ -31,16 +31,16 @@ describe('Router', function () { route.post(saw) request(server) - .get('/foo') - .expect(200, 'saw GET /foo', cb) + .get('/foo') + .expect(200, 'saw GET /foo', cb) request(server) - .post('/foo') - .expect(200, 'saw POST /foo', cb) + .post('/foo') + .expect(200, 'saw POST /foo', cb) request(server) - .put('/foo') - .expect(404, cb) + .put('/foo') + .expect(404, cb) }) it('should stack', function (done) { @@ -56,21 +56,21 @@ describe('Router', function () { router.use(saw) request(server) - .get('/foo') - .expect('x-fn-2', 'hit') - .expect('x-fn-3', 'hit') - .expect(200, 'saw GET /foo', cb) + .get('/foo') + .expect('x-fn-2', 'hit') + .expect('x-fn-3', 'hit') + .expect(200, 'saw GET /foo', cb) request(server) - .post('/foo') - .expect('x-fn-1', 'hit') - .expect('x-fn-2', 'hit') - .expect(200, 'saw POST /foo', cb) + .post('/foo') + .expect('x-fn-1', 'hit') + .expect('x-fn-2', 'hit') + .expect(200, 'saw POST /foo', cb) request(server) - .put('/foo') - .expect('x-fn-2', 'hit') - .expect(200, 'saw PUT /foo', cb) + .put('/foo') + .expect('x-fn-2', 'hit') + .expect(200, 'saw PUT /foo', cb) }) it('should not error on empty route', function (done) { @@ -80,12 +80,12 @@ describe('Router', function () { var server = createServer(router) request(server) - .get('/foo') - .expect(404, cb) + .get('/foo') + .expect(404, cb) request(server) - .head('/foo') - .expect(404, cb) + .head('/foo') + .expect(404, cb) }) it('should not invoke singular error route', function (done) { @@ -98,8 +98,8 @@ describe('Router', function () { }) request(server) - .get('/foo') - .expect(404, done) + .get('/foo') + .expect(404, done) }) describe('.all(...fn)', function () { @@ -130,16 +130,16 @@ describe('Router', function () { route.all(saw) request(server) - .get('/foo') - .expect(200, 'saw GET /foo', cb) + .get('/foo') + .expect(200, 'saw GET /foo', cb) request(server) - .post('/foo') - .expect(200, 'saw POST /foo', cb) + .post('/foo') + .expect(200, 'saw POST /foo', cb) request(server) - .put('/foo') - .expect(200, 'saw PUT /foo', cb) + .put('/foo') + .expect(200, 'saw PUT /foo', cb) }) it('should accept multiple arguments', function (done) { @@ -150,10 +150,10 @@ describe('Router', function () { route.all(createHitHandle(1), createHitHandle(2), helloWorld) request(server) - .get('/foo') - .expect('x-fn-1', 'hit') - .expect('x-fn-2', 'hit') - .expect(200, 'hello, world', done) + .get('/foo') + .expect('x-fn-1', 'hit') + .expect('x-fn-2', 'hit') + .expect(200, 'hello, world', done) }) it('should accept single array of handlers', function (done) { @@ -164,10 +164,10 @@ describe('Router', function () { route.all([createHitHandle(1), createHitHandle(2), helloWorld]) request(server) - .get('/foo') - .expect('x-fn-1', 'hit') - .expect('x-fn-2', 'hit') - .expect(200, 'hello, world', done) + .get('/foo') + .expect('x-fn-1', 'hit') + .expect('x-fn-2', 'hit') + .expect(200, 'hello, world', done) }) it('should accept nested arrays of handlers', function (done) { @@ -178,11 +178,11 @@ describe('Router', function () { route.all([[createHitHandle(1), createHitHandle(2)], createHitHandle(3)], helloWorld) request(server) - .get('/foo') - .expect('x-fn-1', 'hit') - .expect('x-fn-2', 'hit') - .expect('x-fn-3', 'hit') - .expect(200, 'hello, world', done) + .get('/foo') + .expect('x-fn-1', 'hit') + .expect('x-fn-2', 'hit') + .expect('x-fn-3', 'hit') + .expect(200, 'hello, world', done) }) }) @@ -205,8 +205,8 @@ describe('Router', function () { route[method](helloWorld) request(server) - [method]('/') - .expect(200, body, done) + [method]('/') + .expect(200, body, done) }) it('should reject no arguments', function () { @@ -235,10 +235,10 @@ describe('Router', function () { route[method](createHitHandle(1), createHitHandle(2), helloWorld) request(server) - [method]('/foo') - .expect('x-fn-1', 'hit') - .expect('x-fn-2', 'hit') - .expect(200, body, done) + [method]('/foo') + .expect('x-fn-1', 'hit') + .expect('x-fn-2', 'hit') + .expect(200, body, done) }) it('should accept single array of handlers', function (done) { @@ -249,10 +249,10 @@ describe('Router', function () { route[method]([createHitHandle(1), createHitHandle(2), helloWorld]) request(server) - [method]('/foo') - .expect('x-fn-1', 'hit') - .expect('x-fn-2', 'hit') - .expect(200, body, done) + [method]('/foo') + .expect('x-fn-1', 'hit') + .expect('x-fn-2', 'hit') + .expect(200, body, done) }) it('should accept nested arrays of handlers', function (done) { @@ -263,11 +263,11 @@ describe('Router', function () { route[method]([[createHitHandle(1), createHitHandle(2)], createHitHandle(3)], helloWorld) request(server) - [method]('/foo') - .expect('x-fn-1', 'hit') - .expect('x-fn-2', 'hit') - .expect('x-fn-3', 'hit') - .expect(200, body, done) + [method]('/foo') + .expect('x-fn-1', 'hit') + .expect('x-fn-2', 'hit') + .expect('x-fn-3', 'hit') + .expect(200, body, done) }) }) }) @@ -290,8 +290,8 @@ describe('Router', function () { }) request(server) - .get('/foo') - .expect(500, 'caught: boom!', done) + .get('/foo') + .expect(500, 'caught: boom!', done) }) it('should handle errors thrown', function (done) { @@ -311,8 +311,8 @@ describe('Router', function () { }) request(server) - .get('/foo') - .expect(500, 'caught: boom!', done) + .get('/foo') + .expect(500, 'caught: boom!', done) }) it('should handle errors thrown in error handlers', function (done) { @@ -334,8 +334,8 @@ describe('Router', function () { }) request(server) - .get('/foo') - .expect(500, 'caught: oh, no!', done) + .get('/foo') + .expect(500, 'caught: oh, no!', done) }) }) @@ -353,9 +353,9 @@ describe('Router', function () { router.use(saw) request(server) - .get('/foo') - .expect('x-next', 'route') - .expect(200, 'saw GET /foo', done) + .get('/foo') + .expect('x-next', 'route') + .expect(200, 'saw GET /foo', done) }) it('should invoke next route', function (done) { @@ -371,9 +371,9 @@ describe('Router', function () { router.route('/foo').all(saw) request(server) - .get('/foo') - .expect('x-next', 'route') - .expect(200, 'saw GET /foo', done) + .get('/foo') + .expect('x-next', 'route') + .expect(200, 'saw GET /foo', done) }) it('should skip next handlers in route', function (done) { @@ -391,11 +391,11 @@ describe('Router', function () { router.use(saw) request(server) - .get('/foo') - .expect(shouldHitHandle(1)) - .expect('x-next', 'route') - .expect(shouldNotHitHandle(2)) - .expect(200, 'saw GET /foo', done) + .get('/foo') + .expect(shouldHitHandle(1)) + .expect('x-next', 'route') + .expect(shouldNotHitHandle(2)) + .expect(200, 'saw GET /foo', done) }) it('should not invoke error handlers', function (done) { @@ -414,9 +414,9 @@ describe('Router', function () { }) request(server) - .get('/foo') - .expect('x-next', 'route') - .expect(404, done) + .get('/foo') + .expect('x-next', 'route') + .expect(404, done) }) }) @@ -436,10 +436,10 @@ describe('Router', function () { router.use(saw) request(server) - .get('/foo') - .expect('x-next', 'router') - .expect(shouldNotHitHandle(1)) - .expect(404, done) + .get('/foo') + .expect('x-next', 'router') + .expect(shouldNotHitHandle(1)) + .expect(404, done) }) it('should not invoke error handlers', function (done) { @@ -463,9 +463,9 @@ describe('Router', function () { }) request(server) - .get('/foo') - .expect('x-next', 'router') - .expect(404, done) + .get('/foo') + .expect('x-next', 'router') + .expect(404, done) }) }) @@ -487,8 +487,8 @@ describe('Router', function () { }) request(server) - .get('/foo') - .expect(500, 'caught: boom!', done) + .get('/foo') + .expect(500, 'caught: boom!', done) }) it('should pass rejected promise without value', function (done) { @@ -508,8 +508,8 @@ describe('Router', function () { }) request(server) - .get('/foo') - .expect(500, 'caught: Rejected promise', done) + .get('/foo') + .expect(500, 'caught: Rejected promise', done) }) it('should ignore resolved promise', function (done) { @@ -527,8 +527,8 @@ describe('Router', function () { }) request(server) - .get('/foo') - .expect(200, 'saw GET /foo', done) + .get('/foo') + .expect(200, 'saw GET /foo', done) }) describe('error handling', function () { @@ -551,8 +551,8 @@ describe('Router', function () { }) request(server) - .get('/foo') - .expect(500, 'caught again: caught: boom!', done) + .get('/foo') + .expect(500, 'caught again: caught: boom!', done) }) it('should pass rejected promise without value', function (done) { @@ -574,8 +574,8 @@ describe('Router', function () { }) request(server) - .get('/foo') - .expect(500, 'caught again: Rejected promise', done) + .get('/foo') + .expect(500, 'caught again: Rejected promise', done) }) it('should ignore resolved promise', function (done) { @@ -598,8 +598,8 @@ describe('Router', function () { }) request(server) - .get('/foo') - .expect(500, 'caught: boom!', done) + .get('/foo') + .expect(500, 'caught: boom!', done) }) }) }) @@ -614,8 +614,8 @@ describe('Router', function () { route.all(sendParams) request(server) - .get('/bar') - .expect(200, {'foo': 'bar'}, done) + .get('/bar') + .expect(200, {'foo': 'bar'}, done) }) it('should match single path segment', function (done) { @@ -626,8 +626,8 @@ describe('Router', function () { route.all(sendParams) request(server) - .get('/bar/bar') - .expect(404, done) + .get('/bar/bar') + .expect(404, done) }) it('should work multiple times', function (done) { @@ -638,8 +638,8 @@ describe('Router', function () { route.all(sendParams) request(server) - .get('/fizz/buzz') - .expect(200, {'foo': 'fizz', 'bar': 'buzz'}, done) + .get('/fizz/buzz') + .expect(200, {'foo': 'fizz', 'bar': 'buzz'}, done) }) it('should work following a partial capture group', function (done) { @@ -651,12 +651,12 @@ describe('Router', function () { route.all(sendParams) request(server) - .get('/user/tj/edit') - .expect(200, {'user': 'tj', 'op': 'edit'}, cb) + .get('/user/tj/edit') + .expect(200, {'user': 'tj', 'op': 'edit'}, cb) request(server) - .get('/users/tj/edit') - .expect(200, {'0': 's', 'user': 'tj', 'op': 'edit'}, cb) + .get('/users/tj/edit') + .expect(200, {'0': 's', 'user': 'tj', 'op': 'edit'}, cb) }) it('should work inside literal paranthesis', function (done) { @@ -667,8 +667,8 @@ describe('Router', function () { route.all(sendParams) request(server) - .get('/tj(edit)') - .expect(200, {'user': 'tj', 'op': 'edit'}, done) + .get('/tj(edit)') + .expect(200, {'user': 'tj', 'op': 'edit'}, done) }) it('should work within arrays', function (done) { @@ -680,12 +680,12 @@ describe('Router', function () { route.all(sendParams) request(server) - .get('/user/tj/poke') - .expect(200, {'user': 'tj'}, cb) + .get('/user/tj/poke') + .expect(200, {'user': 'tj'}, cb) request(server) - .get('/user/tj/pokes') - .expect(200, {'user': 'tj'}, cb) + .get('/user/tj/pokes') + .expect(200, {'user': 'tj'}, cb) }) }) @@ -698,8 +698,8 @@ describe('Router', function () { route.all(sendParams) request(server) - .get('/foo/bar/baz') - .expect(200, {'0': '/foo/bar/baz'}, done) + .get('/foo/bar/baz') + .expect(200, {'0': '/foo/bar/baz'}, done) }) it('should decode the capture', function (done) { @@ -710,8 +710,8 @@ describe('Router', function () { route.all(sendParams) request(server) - .get('/foo/%20/baz') - .expect(200, {'0': '/foo/ /baz'}, done) + .get('/foo/%20/baz') + .expect(200, {'0': '/foo/ /baz'}, done) }) it('should capture everything with pre- and post-fixes', function (done) { @@ -722,8 +722,8 @@ describe('Router', function () { route.all(sendParams) request(server) - .get('/foo/1/2/3/bar') - .expect(200, {'0': '1/2/3'}, done) + .get('/foo/1/2/3/bar') + .expect(200, {'0': '1/2/3'}, done) }) it('should capture greedly', function (done) { @@ -734,8 +734,8 @@ describe('Router', function () { route.all(sendParams) request(server) - .get('/foo/bar/bar/bar') - .expect(200, {'0': 'bar/bar'}, done) + .get('/foo/bar/bar/bar') + .expect(200, {'0': 'bar/bar'}, done) }) it('should be an optional capture', function (done) { @@ -746,8 +746,8 @@ describe('Router', function () { route.all(sendParams) request(server) - .get('/foo') - .expect(200, {'0': ''}, done) + .get('/foo') + .expect(200, {'0': ''}, done) }) it('should require preceeding /', function (done) { @@ -759,12 +759,12 @@ describe('Router', function () { route.all(sendParams) request(server) - .get('/foo') - .expect(404, cb) + .get('/foo') + .expect(404, cb) request(server) - .get('/foo/') - .expect(200, cb) + .get('/foo/') + .expect(200, cb) }) it('should work in a named parameter', function (done) { @@ -776,12 +776,12 @@ describe('Router', function () { route.all(sendParams) request(server) - .get('/bar') - .expect(200, {'foo': 'bar'}, cb) + .get('/bar') + .expect(200, {'foo': 'bar'}, cb) request(server) - .get('/fizz/buzz') - .expect(200, {'foo': 'fizz/buzz'}, cb) + .get('/fizz/buzz') + .expect(200, {'foo': 'fizz/buzz'}, cb) }) it('should work before a named parameter', function (done) { @@ -792,8 +792,8 @@ describe('Router', function () { route.all(sendParams) request(server) - .get('/poke/user/42') - .expect(200, {'0': 'poke', 'id': '42'}, done) + .get('/poke/user/42') + .expect(200, {'0': 'poke', 'id': '42'}, done) }) it('should work within arrays', function (done) { @@ -805,16 +805,16 @@ describe('Router', function () { route.all(sendParams) request(server) - .get('/user/42') - .expect(200, {'id': '42'}, cb) + .get('/user/42') + .expect(200, {'id': '42'}, cb) request(server) - .get('/foo/bar') - .expect(200, {'0': 'bar'}, cb) + .get('/foo/bar') + .expect(200, {'0': 'bar'}, cb) request(server) - .get('/poke') - .expect(200, {'action': 'poke'}, cb) + .get('/poke') + .expect(200, {'action': 'poke'}, cb) }) }) }) diff --git a/test/router.js b/test/router.js index 25f08d8..691f65b 100644 --- a/test/router.js +++ b/test/router.js @@ -57,8 +57,8 @@ describe('Router', function () { : undefined request(server) - [method]('/') - .expect(200, body, cb) + [method]('/') + .expect(200, body, cb) }) }) @@ -70,16 +70,16 @@ describe('Router', function () { router.all(['/foo', '/bar'], saw) request(server) - .get('/') - .expect(404, cb) + .get('/') + .expect(404, cb) request(server) - .get('/foo') - .expect(200, 'saw GET /foo', cb) + .get('/foo') + .expect(200, 'saw GET /foo', cb) request(server) - .get('/bar') - .expect(200, 'saw GET /bar', cb) + .get('/bar') + .expect(200, 'saw GET /bar', cb) }) it('should support regexp path', function (done) { @@ -90,16 +90,16 @@ describe('Router', function () { router.all(/^\/[a-z]oo$/, saw) request(server) - .get('/') - .expect(404, cb) + .get('/') + .expect(404, cb) request(server) - .get('/foo') - .expect(200, 'saw GET /foo', cb) + .get('/foo') + .expect(200, 'saw GET /foo', cb) request(server) - .get('/zoo') - .expect(200, 'saw GET /zoo', cb) + .get('/zoo') + .expect(200, 'saw GET /zoo', cb) }) it('should support parameterized path', function (done) { @@ -110,20 +110,20 @@ describe('Router', function () { router.all('/:thing', saw) request(server) - .get('/') - .expect(404, cb) + .get('/') + .expect(404, cb) request(server) - .get('/foo') - .expect(200, 'saw GET /foo', cb) + .get('/foo') + .expect(200, 'saw GET /foo', cb) request(server) - .get('/bar') - .expect(200, 'saw GET /bar', cb) + .get('/bar') + .expect(200, 'saw GET /bar', cb) request(server) - .get('/foo/bar') - .expect(404, cb) + .get('/foo/bar') + .expect(404, cb) }) it('should not stack overflow with many registered routes', function (done) { @@ -137,8 +137,8 @@ describe('Router', function () { router.get('/', helloWorld) request(server) - .get('/') - .expect(200, 'hello, world', done) + .get('/') + .expect(200, 'hello, world', done) }) describe('with "caseSensitive" option', function () { @@ -150,16 +150,16 @@ describe('Router', function () { router.all('/foo/bar', saw) request(server) - .get('/foo/bar') - .expect(200, 'saw GET /foo/bar', cb) + .get('/foo/bar') + .expect(200, 'saw GET /foo/bar', cb) request(server) - .get('/FOO/bar') - .expect(200, 'saw GET /FOO/bar', cb) + .get('/FOO/bar') + .expect(200, 'saw GET /FOO/bar', cb) request(server) - .get('/FOO/BAR') - .expect(200, 'saw GET /FOO/BAR', cb) + .get('/FOO/BAR') + .expect(200, 'saw GET /FOO/BAR', cb) }) it('should not match paths case-sensitively when false', function (done) { @@ -170,16 +170,16 @@ describe('Router', function () { router.all('/foo/bar', saw) request(server) - .get('/foo/bar') - .expect(200, 'saw GET /foo/bar', cb) + .get('/foo/bar') + .expect(200, 'saw GET /foo/bar', cb) request(server) - .get('/FOO/bar') - .expect(200, 'saw GET /FOO/bar', cb) + .get('/FOO/bar') + .expect(200, 'saw GET /FOO/bar', cb) request(server) - .get('/FOO/BAR') - .expect(200, 'saw GET /FOO/BAR', cb) + .get('/FOO/BAR') + .expect(200, 'saw GET /FOO/BAR', cb) }) it('should match paths case-sensitively when true', function (done) { @@ -190,16 +190,16 @@ describe('Router', function () { router.all('/foo/bar', saw) request(server) - .get('/foo/bar') - .expect(200, 'saw GET /foo/bar', cb) + .get('/foo/bar') + .expect(200, 'saw GET /foo/bar', cb) request(server) - .get('/FOO/bar') - .expect(404, cb) + .get('/FOO/bar') + .expect(404, cb) request(server) - .get('/FOO/BAR') - .expect(404, cb) + .get('/FOO/BAR') + .expect(404, cb) }) }) @@ -212,12 +212,12 @@ describe('Router', function () { router.all('/foo', saw) request(server) - .get('/foo') - .expect(200, 'saw GET /foo', cb) + .get('/foo') + .expect(200, 'saw GET /foo', cb) request(server) - .get('/foo/') - .expect(200, 'saw GET /foo/', cb) + .get('/foo/') + .expect(200, 'saw GET /foo/', cb) }) it('should accept optional trailing slashes when false', function (done) { @@ -228,12 +228,12 @@ describe('Router', function () { router.all('/foo', saw) request(server) - .get('/foo') - .expect(200, 'saw GET /foo', cb) + .get('/foo') + .expect(200, 'saw GET /foo', cb) request(server) - .get('/foo/') - .expect(200, 'saw GET /foo/', cb) + .get('/foo/') + .expect(200, 'saw GET /foo/', cb) }) it('should not accept optional trailing slashes when true', function (done) { @@ -244,12 +244,12 @@ describe('Router', function () { router.all('/foo', saw) request(server) - .get('/foo') - .expect(200, 'saw GET /foo', cb) + .get('/foo') + .expect(200, 'saw GET /foo', cb) request(server) - .get('/foo/') - .expect(404, cb) + .get('/foo/') + .expect(404, cb) }) }) }) @@ -277,8 +277,8 @@ describe('Router', function () { router[method]('/', helloWorld) request(server) - [method]('/') - .expect(200, body, done) + [method]('/') + .expect(200, body, done) }) it('should reject invalid fn', function () { @@ -294,19 +294,19 @@ describe('Router', function () { router[method](['/foo', '/bar'], createHitHandle(1), helloWorld) request(server) - [method]('/') - .expect(shouldNotHitHandle(1)) - .expect(404, cb) + [method]('/') + .expect(shouldNotHitHandle(1)) + .expect(404, cb) request(server) - [method]('/foo') - .expect(shouldHitHandle(1)) - .expect(200, body, cb) + [method]('/foo') + .expect(shouldHitHandle(1)) + .expect(200, body, cb) request(server) - [method]('/bar') - .expect(shouldHitHandle(1)) - .expect(200, body, cb) + [method]('/bar') + .expect(shouldHitHandle(1)) + .expect(200, body, cb) }) it('should support regexp path', function (done) { @@ -317,19 +317,19 @@ describe('Router', function () { router[method](/^\/[a-z]oo$/, createHitHandle(1), helloWorld) request(server) - [method]('/') - .expect(shouldNotHitHandle(1)) - .expect(404, cb) + [method]('/') + .expect(shouldNotHitHandle(1)) + .expect(404, cb) request(server) - [method]('/foo') - .expect(shouldHitHandle(1)) - .expect(200, body, cb) + [method]('/foo') + .expect(shouldHitHandle(1)) + .expect(200, body, cb) request(server) - [method]('/zoo') - .expect(shouldHitHandle(1)) - .expect(200, body, cb) + [method]('/zoo') + .expect(shouldHitHandle(1)) + .expect(200, body, cb) }) it('should support parameterized path', function (done) { @@ -340,24 +340,24 @@ describe('Router', function () { router[method]('/:thing', createHitHandle(1), helloWorld) request(server) - [method]('/') - .expect(shouldNotHitHandle(1)) - .expect(404, cb) + [method]('/') + .expect(shouldNotHitHandle(1)) + .expect(404, cb) request(server) - [method]('/foo') - .expect(shouldHitHandle(1)) - .expect(200, body, cb) + [method]('/foo') + .expect(shouldHitHandle(1)) + .expect(200, body, cb) request(server) - [method]('/bar') - .expect(shouldHitHandle(1)) - .expect(200, body, cb) + [method]('/bar') + .expect(shouldHitHandle(1)) + .expect(200, body, cb) request(server) - [method]('/foo/bar') - .expect(shouldNotHitHandle(1)) - .expect(404, cb) + [method]('/foo/bar') + .expect(shouldNotHitHandle(1)) + .expect(404, cb) }) it('should accept multiple arguments', function (done) { @@ -367,10 +367,10 @@ describe('Router', function () { router[method]('/', createHitHandle(1), createHitHandle(2), helloWorld) request(server) - [method]('/') - .expect(shouldHitHandle(1)) - .expect(shouldHitHandle(2)) - .expect(200, body, done) + [method]('/') + .expect(shouldHitHandle(1)) + .expect(shouldHitHandle(2)) + .expect(200, body, done) }) describe('req.baseUrl', function () { @@ -384,9 +384,9 @@ describe('Router', function () { }) request(server) - [method]('/foo') - .expect('x-url-base', '""') - .expect(200, done) + [method]('/foo') + .expect('x-url-base', '""') + .expect(200, done) }) }) @@ -401,9 +401,9 @@ describe('Router', function () { }) request(server) - [method]('/foo') - .expect('x-is-route', 'true') - .expect(200, done) + [method]('/foo') + .expect('x-is-route', 'true') + .expect(200, done) }) it('should be the matched route', function (done) { @@ -416,9 +416,9 @@ describe('Router', function () { }) request(server) - [method]('/foo') - .expect('x-is-route', 'true') - .expect(200, done) + [method]('/foo') + .expect('x-is-route', 'true') + .expect(200, done) }) }) }) @@ -456,20 +456,20 @@ describe('Router', function () { router.use(saw) request(server) - .get('/') - .expect(200, 'saw GET /', cb) + .get('/') + .expect(200, 'saw GET /', cb) request(server) - .put('/') - .expect(200, 'saw PUT /', cb) + .put('/') + .expect(200, 'saw PUT /', cb) request(server) - .post('/foo') - .expect(200, 'saw POST /foo', cb) + .post('/foo') + .expect(200, 'saw POST /foo', cb) rawrequest(server) - .options('*') - .expect(200, 'saw OPTIONS *', cb) + .options('*') + .expect(200, 'saw OPTIONS *', cb) }) it('should not invoke for blank URLs', function (done) { @@ -482,8 +482,8 @@ describe('Router', function () { router.use(saw) request(server) - .get('/') - .expect(404, done) + .get('/') + .expect(404, done) }) it('should support another router', function (done) { @@ -495,8 +495,8 @@ describe('Router', function () { router.use(inner) request(server) - .get('/') - .expect(200, 'saw GET /', done) + .get('/') + .expect(200, 'saw GET /', done) }) it('should accept multiple arguments', function (done) { @@ -506,10 +506,10 @@ describe('Router', function () { router.use(createHitHandle(1), createHitHandle(2), helloWorld) request(server) - .get('/') - .expect(shouldHitHandle(1)) - .expect(shouldHitHandle(2)) - .expect(200, 'hello, world', done) + .get('/') + .expect(shouldHitHandle(1)) + .expect(shouldHitHandle(2)) + .expect(200, 'hello, world', done) }) it('should accept single array of middleware', function (done) { @@ -519,10 +519,10 @@ describe('Router', function () { router.use([createHitHandle(1), createHitHandle(2), helloWorld]) request(server) - .get('/') - .expect(shouldHitHandle(1)) - .expect(shouldHitHandle(2)) - .expect(200, 'hello, world', done) + .get('/') + .expect(shouldHitHandle(1)) + .expect(shouldHitHandle(2)) + .expect(200, 'hello, world', done) }) it('should accept nested arrays of middleware', function (done) { @@ -532,11 +532,11 @@ describe('Router', function () { router.use([[createHitHandle(1), createHitHandle(2)], createHitHandle(3)], helloWorld) request(server) - .get('/') - .expect(shouldHitHandle(1)) - .expect(shouldHitHandle(2)) - .expect(shouldHitHandle(3)) - .expect(200, 'hello, world', done) + .get('/') + .expect(shouldHitHandle(1)) + .expect(shouldHitHandle(2)) + .expect(shouldHitHandle(3)) + .expect(200, 'hello, world', done) }) it('should not invoke singular error function', function (done) { @@ -548,8 +548,8 @@ describe('Router', function () { }) request(server) - .get('/') - .expect(404, done) + .get('/') + .expect(404, done) }) describe('error handling', function () { @@ -564,8 +564,8 @@ describe('Router', function () { router.use(sawError) request(server) - .get('/') - .expect(200, 'saw Error: boom!', done) + .get('/') + .expect(200, 'saw Error: boom!', done) }) it('should invoke error function after throw err', function (done) { @@ -579,8 +579,8 @@ describe('Router', function () { router.use(sawError) request(server) - .get('/') - .expect(200, 'saw Error: boom!', done) + .get('/') + .expect(200, 'saw Error: boom!', done) }) it('should not invoke error functions above function', function (done) { @@ -594,8 +594,8 @@ describe('Router', function () { }) request(server) - .get('/') - .expect(500, done) + .get('/') + .expect(500, done) }) }) @@ -612,9 +612,9 @@ describe('Router', function () { router.use(saw) request(server) - .get('/') - .expect('x-next', 'route') - .expect(200, 'saw GET /', done) + .get('/') + .expect('x-next', 'route') + .expect(200, 'saw GET /', done) }) it('should invoke next function', function (done) { @@ -629,11 +629,11 @@ describe('Router', function () { router.use(createHitHandle(1), goNext, createHitHandle(2), saw) request(server) - .get('/') - .expect(shouldHitHandle(1)) - .expect('x-next', 'route') - .expect(shouldHitHandle(2)) - .expect(200, 'saw GET /', done) + .get('/') + .expect(shouldHitHandle(1)) + .expect('x-next', 'route') + .expect(shouldHitHandle(2)) + .expect(200, 'saw GET /', done) }) it('should not invoke error handlers', function (done) { @@ -648,9 +648,9 @@ describe('Router', function () { router.use(sawError) request(server) - .get('/') - .expect('x-next', 'route') - .expect(404, done) + .get('/') + .expect('x-next', 'route') + .expect(404, done) }) }) @@ -668,10 +668,10 @@ describe('Router', function () { router.use(saw) request(server) - .get('/') - .expect('x-next', 'router') - .expect(shouldNotHitHandle(1)) - .expect(404, done) + .get('/') + .expect('x-next', 'router') + .expect(shouldNotHitHandle(1)) + .expect(404, done) }) it('should not invoke error handlers', function (done) { @@ -686,9 +686,9 @@ describe('Router', function () { router.use(sawError) request(server) - .get('/') - .expect('x-next', 'router') - .expect(404, done) + .get('/') + .expect('x-next', 'router') + .expect(404, done) }) }) @@ -704,8 +704,8 @@ describe('Router', function () { router.use(sawError) request(server) - .get('/') - .expect(200, 'saw Error: boom!', done) + .get('/') + .expect(200, 'saw Error: boom!', done) }) it('should pass rejected promise without value', function (done) { @@ -719,8 +719,8 @@ describe('Router', function () { router.use(sawError) request(server) - .get('/') - .expect(200, 'saw Error: Rejected promise', done) + .get('/') + .expect(200, 'saw Error: Rejected promise', done) }) it('should ignore resolved promise', function (done) { @@ -737,8 +737,8 @@ describe('Router', function () { }) request(server) - .get('/foo') - .expect(200, 'saw GET /foo', done) + .get('/foo') + .expect(200, 'saw GET /foo', done) }) describe('error handling', function () { @@ -757,8 +757,8 @@ describe('Router', function () { router.use(sawError) request(server) - .get('/') - .expect(200, 'saw Error: caught: boom!', done) + .get('/') + .expect(200, 'saw Error: caught: boom!', done) }) it('should pass rejected promise without value', function (done) { @@ -776,8 +776,8 @@ describe('Router', function () { router.use(sawError) request(server) - .get('/') - .expect(200, 'saw Error: caught: Rejected promise', done) + .get('/') + .expect(200, 'saw Error: caught: Rejected promise', done) }) it('should ignore resolved promise', function (done) { @@ -798,8 +798,8 @@ describe('Router', function () { }) request(server) - .get('/foo') - .expect(200, 'saw Error: boom!', done) + .get('/foo') + .expect(200, 'saw Error: boom!', done) }) }) }) @@ -812,8 +812,8 @@ describe('Router', function () { router.use(sawBase) request(server) - .get('/foo/bar') - .expect(200, 'saw ', done) + .get('/foo/bar') + .expect(200, 'saw ', done) }) }) }) @@ -832,16 +832,16 @@ describe('Router', function () { router.use('/foo', saw) request(server) - .get('/') - .expect(404, cb) + .get('/') + .expect(404, cb) request(server) - .post('/foo') - .expect(200, 'saw POST /', cb) + .post('/foo') + .expect(200, 'saw POST /', cb) request(server) - .post('/foo/bar') - .expect(200, 'saw POST /bar', cb) + .post('/foo/bar') + .expect(200, 'saw POST /bar', cb) }) it('should match if path has trailing slash', function (done) { @@ -852,16 +852,16 @@ describe('Router', function () { router.use('/foo/', saw) request(server) - .get('/') - .expect(404, cb) + .get('/') + .expect(404, cb) request(server) - .post('/foo') - .expect(200, 'saw POST /', cb) + .post('/foo') + .expect(200, 'saw POST /', cb) request(server) - .post('/foo/bar') - .expect(200, 'saw POST /bar', cb) + .post('/foo/bar') + .expect(200, 'saw POST /bar', cb) }) it('should support array of paths', function (done) { @@ -872,16 +872,16 @@ describe('Router', function () { router.use(['/foo/', '/bar'], saw) request(server) - .get('/') - .expect(404, cb) + .get('/') + .expect(404, cb) request(server) - .get('/foo') - .expect(200, 'saw GET /', cb) + .get('/foo') + .expect(200, 'saw GET /', cb) request(server) - .get('/bar') - .expect(200, 'saw GET /', cb) + .get('/bar') + .expect(200, 'saw GET /', cb) }) it('should support regexp path', function (done) { @@ -892,24 +892,24 @@ describe('Router', function () { router.use(/^\/[a-z]oo/, saw) request(server) - .get('/') - .expect(404, cb) + .get('/') + .expect(404, cb) request(server) - .get('/foo') - .expect(200, 'saw GET /', cb) + .get('/foo') + .expect(200, 'saw GET /', cb) request(server) - .get('/fooo') - .expect(404, cb) + .get('/fooo') + .expect(404, cb) request(server) - .get('/zoo/bear') - .expect(200, 'saw GET /bear', cb) + .get('/zoo/bear') + .expect(200, 'saw GET /bear', cb) request(server) - .get('/get/zoo') - .expect(404, cb) + .get('/get/zoo') + .expect(404, cb) }) it('should support parameterized path', function (done) { @@ -920,20 +920,20 @@ describe('Router', function () { router.use('/:thing', saw) request(server) - .get('/') - .expect(404, cb) + .get('/') + .expect(404, cb) request(server) - .get('/foo') - .expect(200, 'saw GET /', cb) + .get('/foo') + .expect(200, 'saw GET /', cb) request(server) - .get('/bar') - .expect(200, 'saw GET /', cb) + .get('/bar') + .expect(200, 'saw GET /', cb) request(server) - .get('/foo/bar') - .expect(200, 'saw GET /bar', cb) + .get('/foo/bar') + .expect(200, 'saw GET /bar', cb) }) it('should accept multiple arguments', function (done) { @@ -943,10 +943,10 @@ describe('Router', function () { router.use('/foo', createHitHandle(1), createHitHandle(2), helloWorld) request(server) - .get('/foo') - .expect(shouldHitHandle(1)) - .expect(shouldHitHandle(2)) - .expect(200, 'hello, world', done) + .get('/foo') + .expect(shouldHitHandle(1)) + .expect(shouldHitHandle(2)) + .expect(200, 'hello, world', done) }) describe('with "caseSensitive" option', function () { @@ -958,16 +958,16 @@ describe('Router', function () { router.use('/foo', saw) request(server) - .get('/foo/bar') - .expect(200, 'saw GET /bar', cb) + .get('/foo/bar') + .expect(200, 'saw GET /bar', cb) request(server) - .get('/FOO/bar') - .expect(200, 'saw GET /bar', cb) + .get('/FOO/bar') + .expect(200, 'saw GET /bar', cb) request(server) - .get('/FOO/BAR') - .expect(200, 'saw GET /BAR', cb) + .get('/FOO/BAR') + .expect(200, 'saw GET /BAR', cb) }) it('should not match paths case-sensitively when false', function (done) { @@ -978,16 +978,16 @@ describe('Router', function () { router.use('/foo', saw) request(server) - .get('/foo/bar') - .expect(200, 'saw GET /bar', cb) + .get('/foo/bar') + .expect(200, 'saw GET /bar', cb) request(server) - .get('/FOO/bar') - .expect(200, 'saw GET /bar', cb) + .get('/FOO/bar') + .expect(200, 'saw GET /bar', cb) request(server) - .get('/FOO/BAR') - .expect(200, 'saw GET /BAR', cb) + .get('/FOO/BAR') + .expect(200, 'saw GET /BAR', cb) }) it('should match paths case-sensitively when true', function (done) { @@ -998,16 +998,16 @@ describe('Router', function () { router.use('/foo', saw) request(server) - .get('/foo/bar') - .expect(200, 'saw GET /bar', cb) + .get('/foo/bar') + .expect(200, 'saw GET /bar', cb) request(server) - .get('/FOO/bar') - .expect(404, cb) + .get('/FOO/bar') + .expect(404, cb) request(server) - .get('/FOO/BAR') - .expect(404, cb) + .get('/FOO/BAR') + .expect(404, cb) }) }) @@ -1020,12 +1020,12 @@ describe('Router', function () { router.use('/foo', saw) request(server) - .get('/foo') - .expect(200, 'saw GET /', cb) + .get('/foo') + .expect(200, 'saw GET /', cb) request(server) - .get('/foo/') - .expect(200, 'saw GET /', cb) + .get('/foo/') + .expect(200, 'saw GET /', cb) }) it('should accept optional trailing slashes when false', function (done) { @@ -1036,12 +1036,12 @@ describe('Router', function () { router.use('/foo', saw) request(server) - .get('/foo') - .expect(200, 'saw GET /', cb) + .get('/foo') + .expect(200, 'saw GET /', cb) request(server) - .get('/foo/') - .expect(200, 'saw GET /', cb) + .get('/foo/') + .expect(200, 'saw GET /', cb) }) it('should accept optional trailing slashes when true', function (done) { @@ -1052,12 +1052,12 @@ describe('Router', function () { router.use('/foo', saw) request(server) - .get('/foo') - .expect(200, 'saw GET /', cb) + .get('/foo') + .expect(200, 'saw GET /', cb) request(server) - .get('/foo/') - .expect(200, 'saw GET /', cb) + .get('/foo/') + .expect(200, 'saw GET /', cb) }) }) @@ -1074,9 +1074,9 @@ describe('Router', function () { router.use('/foo', saw) request(server) - .get('/foo') - .expect('x-next', 'route') - .expect(200, 'saw GET /', done) + .get('/foo') + .expect('x-next', 'route') + .expect(200, 'saw GET /', done) }) it('should invoke next function', function (done) { @@ -1091,11 +1091,11 @@ describe('Router', function () { router.use('/foo', createHitHandle(1), goNext, createHitHandle(2), saw) request(server) - .get('/foo') - .expect(shouldHitHandle(1)) - .expect('x-next', 'route') - .expect(shouldHitHandle(2)) - .expect(200, 'saw GET /', done) + .get('/foo') + .expect(shouldHitHandle(1)) + .expect('x-next', 'route') + .expect(shouldHitHandle(2)) + .expect(200, 'saw GET /', done) }) }) @@ -1107,8 +1107,8 @@ describe('Router', function () { router.use('/foo', sawBase) request(server) - .get('/foo/bar') - .expect(200, 'saw /foo', done) + .get('/foo/bar') + .expect(200, 'saw /foo', done) }) it('should contain the stripped path for multiple levels', function (done) { @@ -1120,8 +1120,8 @@ describe('Router', function () { router2.use('/bar', sawBase) request(server) - .get('/foo/bar/baz') - .expect(200, 'saw /foo/bar', done) + .get('/foo/bar/baz') + .expect(200, 'saw /foo/bar', done) }) it('should be altered correctly', function(done){ @@ -1146,13 +1146,13 @@ describe('Router', function () { router.use(helloWorld) request(server) - .get('/foo/bar/baz/zed') - .expect('x-saw-base-1', '/foo/bar/baz') - .expect('x-saw-base-2', '/foo') - .expect('x-saw-base-3', '/foo/bar') - .expect('x-saw-base-4', '') - .expect('x-saw-base-5', '') - .expect(200, done) + .get('/foo/bar/baz/zed') + .expect('x-saw-base-1', '/foo/bar/baz') + .expect('x-saw-base-2', '/foo') + .expect('x-saw-base-3', '/foo/bar') + .expect('x-saw-base-4', '') + .expect('x-saw-base-5', '') + .expect(200, done) }) }) @@ -1164,8 +1164,8 @@ describe('Router', function () { router.use('/foo', saw) request(server) - .get('/foo/bar') - .expect(200, 'saw GET /bar', done) + .get('/foo/bar') + .expect(200, 'saw GET /bar', done) }) it('should restore req.url after stripping', function (done) { @@ -1176,9 +1176,9 @@ describe('Router', function () { router.use(saw) request(server) - .get('/foo/bar') - .expect('x-saw-1', 'GET /bar') - .expect(200, 'saw GET /foo/bar', done) + .get('/foo/bar') + .expect('x-saw-1', 'GET /bar') + .expect(200, 'saw GET /foo/bar', done) }) it('should strip/restore with trailing stash', function (done) { @@ -1189,9 +1189,9 @@ describe('Router', function () { router.use(saw) request(server) - .get('/foo/') - .expect('x-saw-1', 'GET /') - .expect(200, 'saw GET /foo/', done) + .get('/foo/') + .expect('x-saw-1', 'GET /') + .expect(200, 'saw GET /foo/', done) }) }) }) @@ -1212,12 +1212,12 @@ describe('Router', function () { router.use(saw) request(server) - .post('/foo') - .expect(shouldNotHitHandle(1)) - .expect(shouldHitHandle(2)) - .expect(shouldNotHitHandle(3)) - .expect(shouldHitHandle(4)) - .expect(200, 'saw PUT /foo', done) + .post('/foo') + .expect(shouldNotHitHandle(1)) + .expect(shouldHitHandle(2)) + .expect(shouldNotHitHandle(3)) + .expect(shouldHitHandle(4)) + .expect(200, 'saw PUT /foo', done) }) it('should support altering req.url', function (done) { @@ -1235,12 +1235,12 @@ describe('Router', function () { router.use(saw) request(server) - .get('/foo') - .expect(shouldNotHitHandle(1)) - .expect(shouldHitHandle(2)) - .expect(shouldNotHitHandle(3)) - .expect(shouldHitHandle(4)) - .expect(200, 'saw GET /bar', done) + .get('/foo') + .expect(shouldNotHitHandle(1)) + .expect(shouldHitHandle(2)) + .expect(shouldNotHitHandle(3)) + .expect(shouldHitHandle(4)) + .expect(200, 'saw GET /bar', done) }) }) }) From 49a8be108677ab7e6966c43f2bd070a1cfb029cb Mon Sep 17 00:00:00 2001 From: Wes Todd Date: Sun, 5 Jan 2020 10:02:27 -0800 Subject: [PATCH 09/11] deps: eslint-plugin-markdown@1.0.1 --- HISTORY.md | 1 + README.md | 16 ++++++++-------- package.json | 3 ++- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 85c1ed0..cdf557a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -7,6 +7,7 @@ unreleased * deps: setprototypeof@1.2.0 * deps: path-to-regexp@3.0.0 * deps: eslint@6.8.0 + * deps: eslint-plugin-markdown@1.0.1 2.0.0-alpha.1 / 2018-07-27 ========================== diff --git a/README.md b/README.md index 90f662a..7a13cb5 100644 --- a/README.md +++ b/README.md @@ -175,14 +175,14 @@ rather than responding. ```js router.route('/') -.all(function (req, res, next) { - next() -}) -.all(check_something) -.get(function (req, res) { - res.setHeader('Content-Type', 'text/plain; charset=utf-8') - res.end('Hello World!') -}) + .all(function (req, res, next) { + next() + }) + .all(check_something) + .get(function (req, res) { + res.setHeader('Content-Type', 'text/plain; charset=utf-8') + res.end('Hello World!') + }) ``` ## Middleware diff --git a/package.json b/package.json index 68fb956..40eee8e 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "devDependencies": { "after": "0.8.2", "eslint": "6.8.0", - "eslint-plugin-markdown": "1.0.0-beta.6", + "eslint-plugin-markdown": "1.0.1", "finalhandler": "1.1.1", "mocha": "7.0.0", "nyc": "15.0.0", @@ -38,6 +38,7 @@ }, "scripts": { "lint": "eslint --plugin markdown --ext js,md .", + "lint-fix": "eslint --plugin markdown --ext js,md . --fix", "test": "mocha --reporter spec --bail --check-leaks test/", "test-cov": "nyc --reporter=text npm test", "test-travis": "nyc --reporter=html --reporter=text npm test" From c512eecadef93a6c3b3f6b5c2672b4f610f75a01 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 5 Mar 2019 22:17:16 +0100 Subject: [PATCH 10/11] test: fix potential timeout closes #79 The default timeout for one test was too low in case the test was run from a slow machine. This increases the timeout to 4 seconds. --- test/router.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/router.js b/test/router.js index 691f65b..7c8f74d 100644 --- a/test/router.js +++ b/test/router.js @@ -127,6 +127,9 @@ describe('Router', function () { }) it('should not stack overflow with many registered routes', function (done) { + // This test might take longer on slow machines. + this.timeout(4000) + var router = new Router() var server = createServer(router) From 05babdfff4847b74233b51a1b6632ad9cc9b1f41 Mon Sep 17 00:00:00 2001 From: Augusto Franzoia Date: Sat, 13 Feb 2016 17:10:07 -0300 Subject: [PATCH 11/11] feat: store matched routes in request closes #34 closes #85 --- HISTORY.md | 1 + index.js | 11 ++- lib/layer.js | 50 +++++++++---- test/router.js | 197 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 243 insertions(+), 16 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index cdf557a..973d02a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -8,6 +8,7 @@ unreleased * deps: path-to-regexp@3.0.0 * deps: eslint@6.8.0 * deps: eslint-plugin-markdown@1.0.1 + * Store matched routes in request 2.0.0-alpha.1 / 2018-07-27 ========================== diff --git a/index.js b/index.js index 7c68516..b8f43d7 100644 --- a/index.js +++ b/index.js @@ -166,7 +166,8 @@ Router.prototype.handle = function handle(req, res, callback) { // manage inter-router variables var parentParams = req.params var parentUrl = req.baseUrl || '' - var done = restore(callback, req, 'baseUrl', 'next', 'params') + var parentMatchedRoutes = req.matchedRoutes + var done = restore(callback, req, 'baseUrl', 'next', 'params', 'matchedRoutes') // setup next layer req.next = next @@ -199,6 +200,7 @@ Router.prototype.handle = function handle(req, res, callback) { req.baseUrl = parentUrl req.url = protohost + removed + req.url.substr(protohost.length) removed = '' + req.matchedRoutes = parentMatchedRoutes } // signal to exit router @@ -287,6 +289,11 @@ Router.prototype.handle = function handle(req, res, callback) { return next(layerError || err) } + if (layer.path) { + req.matchedRoutes = req.matchedRoutes || [] + req.matchedRoutes.push(layer.matchedPath.path) + } + if (route) { return layer.handle_request(req, res, next) } @@ -342,7 +349,7 @@ Router.prototype.process_params = function process_params(layer, called, req, re var params = this.params // captured parameters from the layer, keys and values - var keys = layer.keys + var keys = layer.matchedPath && layer.matchedPath.keys // fast track if (!keys || keys.length === 0) { diff --git a/lib/layer.js b/lib/layer.js index e6d8576..e9e64b2 100644 --- a/lib/layer.js +++ b/lib/layer.js @@ -14,6 +14,7 @@ var pathRegexp = require('path-to-regexp') var debug = require('debug')('router:layer') +var flatten = require('array-flatten') /** * Module variables. @@ -33,18 +34,18 @@ function Layer(p, options, fn) { return new Layer(p, options, fn) } - debug('new %o', path) + debug('new %o', paths) var opts = options || {} // If not in strict allow both with or without trailing slash - var path = p + var paths = p if (!opts.strict) { - if (!Array.isArray(path) && path !== '/' && path[path.length - 1] === '/') { - path = path.substr(0, path.length - 1) + if (!Array.isArray(paths) && paths !== '/' && paths[paths.length - 1] === '/') { + paths = paths.substr(0, paths.length - 1) } else { - for (var i = 0; i < path.length; i++) { - if (path[i] !== '/' && path[i][path[i].length - 1] === '/') { - path[i] = path[i].substr(0, path[i].length - 1) + for (var i = 0; i < paths.length; i++) { + if (paths[i] !== '/' && paths[i][paths[i].length - 1] === '/') { + paths[i] = paths[i].substr(0, paths[i].length - 1) } } } @@ -54,11 +55,22 @@ function Layer(p, options, fn) { this.name = fn.name || '' this.params = undefined this.path = undefined - this.regexp = pathRegexp(path, this.keys = [], opts) + this.matchedPath = undefined // set fast path flags - this.regexp.fast_star = path === '*' - this.regexp.fast_slash = path === '/' && opts.end === false + this.fastStar = paths === '*' + this.fastSlash = paths === '/' && opts.end === false + + this.paths = (!Array.isArray(paths) ? [paths] : flatten(paths)).map(function (path) { + var keys = [] + var pathObj = { + path: path, + keys: keys, + regexp: pathRegexp(path, keys, opts) + } + + return pathObj + }) } /** @@ -137,29 +149,39 @@ Layer.prototype.handle_request = function handle(req, res, next) { Layer.prototype.match = function match(path) { var match + var checkPath if (path != null) { // fast path non-ending match for / (any path matches) - if (this.regexp.fast_slash) { + if (this.fastSlash) { this.params = {} this.path = '' + this.matchedPath = this.paths[0] return true } // fast path for * (everything matched in a param) - if (this.regexp.fast_star) { + if (this.fastStar) { this.params = {'0': decode_param(path)} this.path = path + this.matchedPath = this.paths[0] return true } // match the path - match = this.regexp.exec(path) + for (var i = 0; i < this.paths.length; i++) { + checkPath = this.paths[i] + if (match = checkPath.regexp.exec(path)) { + this.matchedPath = checkPath + break + } + } } if (!match) { this.params = undefined this.path = undefined + this.matchedPath = undefined return false } @@ -172,7 +194,7 @@ Layer.prototype.match = function match(path) { var params = this.params for (var i = 1; i < match.length; i++) { - var key = keys[i - 1] + var key = this.matchedPath.keys[i - 1] var prop = key.name var val = decode_param(match[i]) diff --git a/test/router.js b/test/router.js index 7c8f74d..02e56c0 100644 --- a/test/router.js +++ b/test/router.js @@ -1246,6 +1246,203 @@ describe('Router', function () { .expect(200, 'saw GET /bar', done) }) }) + + describe('req.matchedRoutes', function () { + it('should be set if there is a match', function (done) { + var router = new Router() + var server = createServer(router) + var matchedRoutes + + router.get('/foo', function (req, res, next) { + matchedRoutes = req.matchedRoutes + next() + }) + router.use(saw) + + request(server) + .get('/foo') + .expect(200, 'saw GET /foo', function (err, res) { + assert.deepEqual(matchedRoutes, ['/foo']) + done(err) + }) + }) + + it('should be undefined if there is not a match', function (done) { + var router = new Router() + var server = createServer(router) + var matchedRoutes + + router.use(function (req, res, next) { + matchedRoutes = req.matchedRoutes + next() + }) + + request(server) + .get('/foo') + .expect(404, function (err, res) { + assert.strictEqual(matchedRoutes, undefined) + done(err) + }) + }) + + it('should work with sub-routers', function (done) { + var router = new Router() + var fooRouter = new Router() + var server = createServer(router) + var matchedFooRoutes + var matchedBarRoutes + + router.use('/foo', function (req, res, next) { + matchedFooRoutes = req.matchedRoutes + next() + }, fooRouter) + fooRouter.get('/bar', function (req, res, next) { + matchedBarRoutes = req.matchedRoutes + next() + }) + router.use(saw) + + request(server) + .get('/foo/bar') + .expect(200, 'saw GET /foo/bar', function (err, res) { + assert.deepEqual(matchedFooRoutes, ['/foo']) + assert.deepEqual(matchedBarRoutes, ['/foo', '/bar']) + done(err) + }) + }) + + it('should be undefined if sub-router did not match', function (done) { + var router = new Router() + var fooRouter = new Router() + var server = createServer(router) + var matchedRoutes + + router.use('/foo', fooRouter) + fooRouter.get('/bar', function (req, res, next) { + matchedRoutes = req.matchedRoutes + next() + }) + router.use(saw) + + request(server) + .get('/foo/baz') + .expect(200, 'saw GET /foo/baz', function (err, res) { + assert.strictEqual(matchedRoutes, undefined) + done(err) + }) + }) + + it('should work with regexp-defined routes', function (done) { + var router = new Router() + var server = createServer(router) + var matchedRoutes + var regexp = /fo+/ + + router.get(regexp, function (req, res, next) { + matchedRoutes = req.matchedRoutes + next() + }) + router.use(saw) + + request(server) + .get('/foo') + .expect(200, 'saw GET /foo', function (err, res) { + assert.deepEqual(matchedRoutes, [regexp]) + done(err) + }) + }) + + it('should support routes defined with arrays of paths', function (done) { + var router = new Router() + var server = createServer(router) + var matchedRoutes + + router.get(['/foo', '/bar/:id'], function (req, res, next) { + matchedRoutes = req.matchedRoutes + next() + }) + router.use(saw) + + request(server) + .get('/foo') + .expect(200, 'saw GET /foo', function (err, res) { + if (err) { + return done(err) + } + assert.deepEqual(matchedRoutes, ['/foo']) + + request(server) + .get('/bar/1') + .expect(200, 'saw GET /bar/1', function (err, res) { + if (err) { + return done(err) + } + assert.deepEqual(matchedRoutes, ['/bar/:id']) + done() + }) + }) + }) + + it('should support routes defined with nested arrays of paths', function (done) { + var router = new Router() + var server = createServer(router) + var matchedRoutes + + router.get([['/foo', ['/bar/:id']]], function (req, res, next) { + matchedRoutes = req.matchedRoutes + next() + }) + router.use(saw) + + request(server) + .get('/foo') + .expect(200, 'saw GET /foo', function (err, res) { + if (err) { + return done(err) + } + assert.deepEqual(matchedRoutes, ['/foo']) + + request(server) + .get('/bar/1') + .expect(200, 'saw GET /bar/1', function (err, res) { + if (err) { + return done(err) + } + assert.deepEqual(matchedRoutes, ['/bar/:id']) + done() + }) + }) + }) + + it('should support sibling routes at same path', function (done) { + var router = new Router() + var server = createServer(router) + var matchedRoutes1 + var matchedRoutes2 + + router.get('/foo', function (req, res, next) { + matchedRoutes1 = req.matchedRoutes + next() + }) + router.get('/foo', function (req, res, next) { + matchedRoutes2 = req.matchedRoutes + next() + }) + router.use(saw) + + request(server) + .get('/foo') + .expect(200, 'saw GET /foo', function (err, res) { + if (err) { + return done(err) + } + assert.deepEqual(matchedRoutes1, ['/foo']) + assert.deepEqual(matchedRoutes2, ['/foo']) + + done() + }) + }) + }) }) function helloWorld(req, res) {