Skip to content
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

Added ignoreTrailingSlash router option #1632

Merged
merged 2 commits into from Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/router.js
Expand Up @@ -33,12 +33,18 @@ var ResourceNotFoundError = errors.ResourceNotFoundError;
* @param {Boolean} [options.strictNext=false] - Throws error when next() is
* called more than once, enabled onceNext option
* @param {Object} [options.registry] - route registry
* @param {Object} [options.ignoreTrailingSlash] - ignore trailing slash on
* paths
*/
function Router(options) {
assert.object(options, 'options');
assert.object(options.log, 'options.log');
assert.optionalBool(options.onceNext, 'options.onceNext');
assert.optionalBool(options.strictNext, 'options.strictNext');
assert.optionalBool(
options.ignoreTrailingSlash,
'options.ignoreTrailingSlash'
);

EventEmitter.call(this);

Expand Down
7 changes: 5 additions & 2 deletions lib/routerRegistryRadix.js
Expand Up @@ -9,9 +9,12 @@ var Chain = require('./chain');
*
* @class RouterRegistryRadix
* @public
* @param {Object} options - an options object
* @param {Object} [options.ignoreTrailingSlash] - ignore trailing slash on
* paths
*/
function RouterRegistryRadix() {
this._findMyWay = new FindMyWay();
function RouterRegistryRadix(options) {
this._findMyWay = new FindMyWay(options);
this._routes = {};
}

Expand Down
25 changes: 25 additions & 0 deletions test/router.test.js
Expand Up @@ -346,3 +346,28 @@ test('toString()', function(t) {
);
t.end();
});

test('toString() with ignoreTrailingSlash', function(t) {
function handler(req, res, next) {
res.send('Hello world');
}

var router = new Router({
log: {},
ignoreTrailingSlash: true
});
router.mount({ method: 'GET', path: '/' }, [handler]);
router.mount({ method: 'GET', path: '/a' }, [handler]);
router.mount({ method: 'GET', path: '/a/b' }, [handler]);
router.mount({ method: 'POST', path: '/' }, [handler]);

t.deepEqual(
router.toString(),
'└── / (GET|POST)\n' +
' └── a (GET)\n' +
' └── / (GET)\n' +
' └── b (GET)\n' +
' └── / (GET)\n'
);
t.end();
});
18 changes: 18 additions & 0 deletions test/routerRegistryRadix.test.js
Expand Up @@ -99,3 +99,21 @@ test('toString()', function(t) {
);
t.end();
});

test('toString() with ignoreTrailingSlash', function(t) {
var registry = new RouterRegistryRadix({ ignoreTrailingSlash: true });
registry.add(getTestRoute({ method: 'GET', path: '/' }));
registry.add(getTestRoute({ method: 'GET', path: '/a' }));
registry.add(getTestRoute({ method: 'GET', path: '/a/b' }));
registry.add(getTestRoute({ method: 'POST', path: '/' }));

t.deepEqual(
registry.toString(),
'└── / (GET|POST)\n' +
' └── a (GET)\n' +
' └── / (GET)\n' +
' └── b (GET)\n' +
' └── / (GET)\n'
);
t.end();
});
82 changes: 81 additions & 1 deletion test/server.test.js
Expand Up @@ -46,7 +46,8 @@ before(function(cb) {
dtrace: helper.dtrace,
handleUncaughtExceptions: true,
log: helper.getLog('server'),
version: ['2.0.0', '0.5.4', '1.4.3']
version: ['2.0.0', '0.5.4', '1.4.3'],
ignoreTrailingSlash: true
});
SERVER.listen(PORT, '127.0.0.1', function() {
PORT = SERVER.address().port;
Expand Down Expand Up @@ -159,6 +160,85 @@ test('get (path only)', function(t) {
});
});

test('get (path only - with trailing slash)', function(t) {
SERVER.get('/foo/', function echoId(req, res, next) {
res.send();
next();
});

var count = 0;

CLIENT.get('/foo/', function(err, _, res) {
t.ifError(err);
t.equal(res.statusCode, 200);

if (++count === 2) {
t.end();
}
});

CLIENT.get('/foo', function(err, _, res) {
t.ifError(err);
t.equal(res.statusCode, 200);

if (++count === 2) {
t.end();
}
});
});

test('get (path only - with trailing slash and nested route)', function(t) {
SERVER.get('/foo/', function echoId(req, res, next) {
res.statusCode = 200;
res.send();
next();
});

SERVER.get('/foo/bar', function echoId(req, res, next) {
res.statusCode = 201;
res.send();
next();
});

var count = 0;

CLIENT.get('/foo/', function(err, _, res) {
t.ifError(err);
t.equal(res.statusCode, 200);

if (++count === 4) {
t.end();
}
});

CLIENT.get('/foo', function(err, _, res) {
t.ifError(err);
t.equal(res.statusCode, 200);

if (++count === 4) {
t.end();
}
});

CLIENT.get('/foo/bar/', function(err, _, res) {
t.ifError(err);
t.equal(res.statusCode, 201);

if (++count === 4) {
t.end();
}
});

CLIENT.get('/foo/bar', function(err, _, res) {
t.ifError(err);
t.equal(res.statusCode, 201);

if (++count === 4) {
t.end();
}
});
});

test('use + get (path only)', function(t) {
SERVER.use(function(req, res, next) {
next();
Expand Down