Repository navigation
feat: add app.listRoutes with sub-application support #7531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bjohansebas
wants to merge
1
commit into
master
Choose a base branch
from
feat/list-routes-subapps
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+242
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package-lock=false | ||
| min-release-age=7 | ||
| ignore-scripts=true | ||
| allow-git=none | ||
| allow-git=root |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| 'use strict'; | ||
|
|
||
| var assert = require('node:assert'); | ||
| var methods = require('node:http').METHODS; | ||
| var express = require('../'); | ||
| var request = require('supertest'); | ||
|
|
||
| describe('app.listRoutes()', function () { | ||
| it('should return an empty array for a new application', function () { | ||
| var app = express(); | ||
|
|
||
| assert.deepStrictEqual(app.listRoutes(), []); | ||
| }); | ||
|
|
||
| it('should list routes registered through the application APIs', function () { | ||
| var app = express(); | ||
|
|
||
| app.get('/users', noop); | ||
| app.route('/posts').get(noop).all(noop).post(noop); | ||
| app.route('/draft'); | ||
|
|
||
| assert.deepStrictEqual(app.listRoutes(), [ | ||
| { path: '/users', methods: ['GET', 'HEAD'], router: undefined }, | ||
| { path: '/posts', methods: ['GET', '*', 'POST', 'HEAD'], router: undefined }, | ||
| { path: '/draft', methods: [], router: undefined } | ||
| ]); | ||
| }); | ||
|
|
||
| it('should list the individual HTTP methods registered by app.all()', function () { | ||
| var app = express(); | ||
|
|
||
| app.all('/all', noop); | ||
| app.route('/wildcard').all(noop); | ||
|
|
||
| assert.deepStrictEqual(app.listRoutes(), [ | ||
| { path: '/all', methods: methods, router: undefined }, | ||
| { path: '/wildcard', methods: ['*'], router: undefined } | ||
| ]); | ||
| }); | ||
|
|
||
| it('should expose mounted routers for separate inspection', function () { | ||
| var app = express(); | ||
| var router = express.Router(); | ||
|
|
||
| router.all('/users', noop); | ||
| app.use('/api', router); | ||
|
|
||
| var routes = app.listRoutes(); | ||
|
|
||
| assert.deepStrictEqual(routes, [ | ||
| { path: '/api', methods: undefined, router: router } | ||
| ]); | ||
| assert.deepStrictEqual(routes[0].router.listRoutes(), [ | ||
| { path: '/users', methods: ['*'], router: undefined } | ||
| ]); | ||
| }); | ||
|
|
||
| it('should omit plain middleware', function () { | ||
| var app = express(); | ||
|
|
||
| app.use(noop); | ||
| app.use('/admin', noop); | ||
| app.get('/users', noop); | ||
|
|
||
| assert.deepStrictEqual(app.listRoutes(), [ | ||
| { path: '/users', methods: ['GET', 'HEAD'], router: undefined } | ||
| ]); | ||
| }); | ||
|
|
||
| it('should expose the router of a mounted sub-application', function () { | ||
| var app = express(); | ||
| var admin = express(); | ||
|
|
||
| admin.get('/users', noop); | ||
| app.use('/admin', admin); | ||
|
|
||
| var routes = app.listRoutes(); | ||
|
|
||
| assert.deepStrictEqual(routes, [ | ||
| { path: '/admin', methods: undefined, router: admin.router } | ||
| ]); | ||
| assert.deepStrictEqual(app.router.listRoutes(), routes); | ||
| assert.deepStrictEqual(routes[0].router.listRoutes(), [ | ||
| { path: '/users', methods: ['GET', 'HEAD'], router: undefined } | ||
| ]); | ||
| }); | ||
|
|
||
| it('should preserve registration order and the paths of sub-application mounts', function () { | ||
| var app = express(); | ||
| var admin = express(); | ||
| var path = /^\/admin/; | ||
|
|
||
| app.get('/before', noop); | ||
| app.use(admin); | ||
| app.use(['/one', ['/two']], admin); | ||
| app.use(path, admin); | ||
| app.get('/after', noop); | ||
|
|
||
| assert.deepStrictEqual(app.listRoutes(), [ | ||
| { path: '/before', methods: ['GET', 'HEAD'], router: undefined }, | ||
| { path: '/', methods: undefined, router: admin.router }, | ||
| { path: '/one', methods: undefined, router: admin.router }, | ||
| { path: '/two', methods: undefined, router: admin.router }, | ||
| { path: path, methods: undefined, router: admin.router }, | ||
| { path: '/after', methods: ['GET', 'HEAD'], router: undefined } | ||
| ]); | ||
| }); | ||
|
|
||
| it('should allow inspection of nested sub-applications through their routers', function () { | ||
| var app = express(); | ||
| var api = express(); | ||
| var admin = express(); | ||
|
|
||
| app.use('/api', api); | ||
| api.use('/admin', admin); | ||
| admin.get('/users', noop); | ||
|
|
||
| var apiRouter = app.listRoutes()[0].router; | ||
| assert.strictEqual(apiRouter, api.router); | ||
| assert.deepStrictEqual(apiRouter.listRoutes(), [ | ||
| { path: '/admin', methods: undefined, router: admin.router } | ||
| ]); | ||
| assert.deepStrictEqual(apiRouter.listRoutes()[0].router.listRoutes(), [ | ||
| { path: '/users', methods: ['GET', 'HEAD'], router: undefined } | ||
| ]); | ||
| }); | ||
|
|
||
| it('should expose sub-applications mounted directly on an express.Router()', function () { | ||
| var app = express(); | ||
| var router = express.Router(); | ||
| var admin = express(); | ||
|
|
||
| app.use('/api', router); | ||
| router.use('/admin', admin); | ||
| admin.get('/users', noop); | ||
|
|
||
| assert.deepStrictEqual(app.listRoutes()[0].router.listRoutes(), [ | ||
| { path: '/admin', methods: undefined, router: admin.router } | ||
| ]); | ||
| }); | ||
|
|
||
| it('should reflect routes added after inspecting an empty mounted sub-application', function () { | ||
| var app = express(); | ||
| var admin = express(); | ||
|
|
||
| app.use('/admin', admin); | ||
|
|
||
| var mountedRouter = app.listRoutes()[0].router; | ||
| assert.strictEqual(mountedRouter, admin.router); | ||
| assert.deepStrictEqual(mountedRouter.listRoutes(), []); | ||
|
|
||
| admin.get('/users', noop); | ||
|
|
||
| assert.deepStrictEqual(mountedRouter.listRoutes(), [ | ||
| { path: '/users', methods: ['GET', 'HEAD'], router: undefined } | ||
| ]); | ||
| }); | ||
|
|
||
| it('should defer router creation until after mounting and configuration', function (done) { | ||
| var app = express(); | ||
| var admin = express(); | ||
|
|
||
| admin.on('mount', function () { | ||
| this.enable('case sensitive routing'); | ||
| }); | ||
| app.use('/admin', admin); | ||
| admin.enable('strict routing'); | ||
| admin.get('/Users', function (req, res) { | ||
| res.send('users'); | ||
| }); | ||
|
|
||
| var mountedRouter = app.listRoutes()[0].router; | ||
| assert.strictEqual(mountedRouter, admin.router); | ||
| assert.strictEqual(mountedRouter.caseSensitive, true); | ||
| assert.strictEqual(mountedRouter.strict, true); | ||
|
|
||
| request(app).get('/admin/users').expect(404, done); | ||
| }); | ||
|
|
||
| it('should preserve sub-application dispatch and restore the parent application', function (done) { | ||
| var app = express(); | ||
| var admin = express(); | ||
|
|
||
| app.request.scope = function () { return 'parent'; }; | ||
| admin.request.scope = function () { return 'child'; }; | ||
| app.response.scope = function () { return 'parent'; }; | ||
| admin.response.scope = function () { return 'child'; }; | ||
|
|
||
| admin.get('/users', function (req, res, next) { | ||
| assert.strictEqual(req.app, admin); | ||
| assert.strictEqual(res.app, admin); | ||
| assert.strictEqual(req.scope(), 'child'); | ||
| assert.strictEqual(res.scope(), 'child'); | ||
| res.setHeader('x-subapp', 'hit'); | ||
| next(); | ||
| }); | ||
| app.use('/admin', admin); | ||
| app.use(function (req, res) { | ||
| assert.strictEqual(req.app, app); | ||
| assert.strictEqual(res.app, app); | ||
| assert.strictEqual(req.scope(), 'parent'); | ||
| assert.strictEqual(res.scope(), 'parent'); | ||
| res.send('parent'); | ||
| }); | ||
|
|
||
| assert.strictEqual(app.listRoutes()[0].router, admin.router); | ||
|
|
||
| request(app).get('/admin/users') | ||
| .expect('x-subapp', 'hit') | ||
| .expect(200, 'parent', done); | ||
| }); | ||
| }); | ||
|
|
||
| function noop() {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: This commit isn't in your PR yet, I assume it will?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was, but I rebased, so the commit hashes changed. To test it locally, you’ll need to use the latest commit hash from that PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the wrapper change in
listRoutes, there's nohandle.routercheck. I think it had this?Can you double check if it is there?