2022-02-09 01:07:08 -05:00
|
|
|
'use strict'
|
2014-01-25 17:57:25 -05:00
|
|
|
|
2014-07-23 17:28:14 -04:00
|
|
|
var after = require('after');
|
2024-12-21 22:58:33 +01:00
|
|
|
var assert = require('node:assert')
|
2014-01-25 17:57:25 -05:00
|
|
|
var express = require('../')
|
|
|
|
|
, Route = express.Route
|
2025-01-02 08:00:30 +01:00
|
|
|
, methods = require('../lib/utils').methods
|
2014-01-25 17:57:25 -05:00
|
|
|
|
|
|
|
|
describe('Route', function(){
|
2016-05-23 23:03:13 -04:00
|
|
|
it('should work without handlers', function(done) {
|
|
|
|
|
var req = { method: 'GET', url: '/' }
|
|
|
|
|
var route = new Route('/foo')
|
|
|
|
|
route.dispatch(req, {}, done)
|
|
|
|
|
})
|
2014-01-25 17:57:25 -05:00
|
|
|
|
2022-04-13 23:29:25 -04:00
|
|
|
it('should not stack overflow with a large sync stack', function (done) {
|
|
|
|
|
this.timeout(5000) // long-running test
|
|
|
|
|
|
|
|
|
|
var req = { method: 'GET', url: '/' }
|
|
|
|
|
var route = new Route('/foo')
|
|
|
|
|
|
2022-05-20 09:37:20 -04:00
|
|
|
route.get(function (req, res, next) {
|
|
|
|
|
req.counter = 0
|
|
|
|
|
next()
|
|
|
|
|
})
|
|
|
|
|
|
2022-04-13 23:29:25 -04:00
|
|
|
for (var i = 0; i < 6000; i++) {
|
2022-05-20 09:37:20 -04:00
|
|
|
route.all(function (req, res, next) {
|
|
|
|
|
req.counter++
|
|
|
|
|
next()
|
|
|
|
|
})
|
2022-04-13 23:29:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
route.get(function (req, res, next) {
|
|
|
|
|
req.called = true
|
|
|
|
|
next()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
route.dispatch(req, {}, function (err) {
|
|
|
|
|
if (err) return done(err)
|
|
|
|
|
assert.ok(req.called)
|
2022-05-20 09:37:20 -04:00
|
|
|
assert.strictEqual(req.counter, 6000)
|
2022-04-13 23:29:25 -04:00
|
|
|
done()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2014-01-25 17:57:25 -05:00
|
|
|
describe('.all', function(){
|
|
|
|
|
it('should add handler', function(done){
|
2014-07-23 17:28:14 -04:00
|
|
|
var req = { method: 'GET', url: '/' };
|
2014-01-25 17:57:25 -05:00
|
|
|
var route = new Route('/foo');
|
|
|
|
|
|
|
|
|
|
route.all(function(req, res, next) {
|
2014-07-23 17:28:14 -04:00
|
|
|
req.called = true;
|
2014-01-25 17:57:25 -05:00
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2014-07-23 17:28:14 -04:00
|
|
|
route.dispatch(req, {}, function (err) {
|
|
|
|
|
if (err) return done(err);
|
2022-02-21 19:54:52 -05:00
|
|
|
assert.ok(req.called)
|
2014-07-23 17:28:14 -04:00
|
|
|
done();
|
|
|
|
|
});
|
2014-01-25 17:57:25 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should handle VERBS', function(done) {
|
2014-07-23 17:28:14 -04:00
|
|
|
var count = 0;
|
2014-01-25 17:57:25 -05:00
|
|
|
var route = new Route('/foo');
|
2014-07-23 17:28:14 -04:00
|
|
|
var cb = after(methods.length, function (err) {
|
|
|
|
|
if (err) return done(err);
|
2022-02-21 19:54:52 -05:00
|
|
|
assert.strictEqual(count, methods.length)
|
2014-07-23 17:28:14 -04:00
|
|
|
done();
|
|
|
|
|
});
|
2014-01-25 17:57:25 -05:00
|
|
|
|
|
|
|
|
route.all(function(req, res, next) {
|
|
|
|
|
count++;
|
2014-07-23 17:28:14 -04:00
|
|
|
next();
|
2014-01-25 17:57:25 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
methods.forEach(function testMethod(method) {
|
2014-07-23 17:28:14 -04:00
|
|
|
var req = { method: method, url: '/' };
|
|
|
|
|
route.dispatch(req, {}, cb);
|
2014-01-25 17:57:25 -05:00
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should stack', function(done) {
|
2014-07-23 17:28:14 -04:00
|
|
|
var req = { count: 0, method: 'GET', url: '/' };
|
2014-01-25 17:57:25 -05:00
|
|
|
var route = new Route('/foo');
|
|
|
|
|
|
|
|
|
|
route.all(function(req, res, next) {
|
2014-07-23 17:28:14 -04:00
|
|
|
req.count++;
|
2014-01-25 17:57:25 -05:00
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
route.all(function(req, res, next) {
|
2014-07-23 17:28:14 -04:00
|
|
|
req.count++;
|
2014-01-25 17:57:25 -05:00
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2014-07-23 17:28:14 -04:00
|
|
|
route.dispatch(req, {}, function (err) {
|
|
|
|
|
if (err) return done(err);
|
2022-02-21 19:54:52 -05:00
|
|
|
assert.strictEqual(req.count, 2)
|
2014-07-23 17:28:14 -04:00
|
|
|
done();
|
2014-01-25 17:57:25 -05:00
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('.VERB', function(){
|
|
|
|
|
it('should support .get', function(done){
|
2014-07-23 17:28:14 -04:00
|
|
|
var req = { method: 'GET', url: '/' };
|
2014-01-25 17:57:25 -05:00
|
|
|
var route = new Route('');
|
|
|
|
|
|
|
|
|
|
route.get(function(req, res, next) {
|
2014-07-23 17:28:14 -04:00
|
|
|
req.called = true;
|
|
|
|
|
next();
|
2014-01-25 17:57:25 -05:00
|
|
|
})
|
|
|
|
|
|
2014-07-23 17:28:14 -04:00
|
|
|
route.dispatch(req, {}, function (err) {
|
|
|
|
|
if (err) return done(err);
|
2022-02-21 19:54:52 -05:00
|
|
|
assert.ok(req.called)
|
2014-07-23 17:28:14 -04:00
|
|
|
done();
|
|
|
|
|
});
|
2014-01-25 17:57:25 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should limit to just .VERB', function(done){
|
2014-07-23 17:28:14 -04:00
|
|
|
var req = { method: 'POST', url: '/' };
|
2014-01-25 17:57:25 -05:00
|
|
|
var route = new Route('');
|
|
|
|
|
|
2023-03-04 13:48:00 +05:30
|
|
|
route.get(function () {
|
2014-07-23 17:28:14 -04:00
|
|
|
throw new Error('not me!');
|
2014-01-25 17:57:25 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
route.post(function(req, res, next) {
|
2014-07-23 17:28:14 -04:00
|
|
|
req.called = true;
|
|
|
|
|
next();
|
2014-01-25 17:57:25 -05:00
|
|
|
})
|
|
|
|
|
|
2014-07-23 17:28:14 -04:00
|
|
|
route.dispatch(req, {}, function (err) {
|
|
|
|
|
if (err) return done(err);
|
2022-02-21 19:54:52 -05:00
|
|
|
assert.ok(req.called)
|
2014-07-23 17:28:14 -04:00
|
|
|
done();
|
|
|
|
|
});
|
2014-01-25 17:57:25 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should allow fallthrough', function(done){
|
2014-07-23 17:28:14 -04:00
|
|
|
var req = { order: '', method: 'GET', url: '/' };
|
2014-01-25 17:57:25 -05:00
|
|
|
var route = new Route('');
|
|
|
|
|
|
|
|
|
|
route.get(function(req, res, next) {
|
2014-07-23 17:28:14 -04:00
|
|
|
req.order += 'a';
|
2014-01-25 17:57:25 -05:00
|
|
|
next();
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
route.all(function(req, res, next) {
|
2014-07-23 17:28:14 -04:00
|
|
|
req.order += 'b';
|
2014-01-25 17:57:25 -05:00
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
route.get(function(req, res, next) {
|
2014-07-23 17:28:14 -04:00
|
|
|
req.order += 'c';
|
|
|
|
|
next();
|
2014-01-25 17:57:25 -05:00
|
|
|
})
|
|
|
|
|
|
2014-07-23 17:28:14 -04:00
|
|
|
route.dispatch(req, {}, function (err) {
|
|
|
|
|
if (err) return done(err);
|
2022-02-21 19:54:52 -05:00
|
|
|
assert.strictEqual(req.order, 'abc')
|
2014-07-23 17:28:14 -04:00
|
|
|
done();
|
|
|
|
|
});
|
2014-01-25 17:57:25 -05:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('errors', function(){
|
|
|
|
|
it('should handle errors via arity 4 functions', function(done){
|
2014-07-23 17:28:14 -04:00
|
|
|
var req = { order: '', method: 'GET', url: '/' };
|
2014-01-25 17:57:25 -05:00
|
|
|
var route = new Route('');
|
|
|
|
|
|
|
|
|
|
route.all(function(req, res, next){
|
|
|
|
|
next(new Error('foobar'));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
route.all(function(req, res, next){
|
2014-07-23 17:28:14 -04:00
|
|
|
req.order += '0';
|
2014-01-25 17:57:25 -05:00
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
route.all(function(err, req, res, next){
|
2014-07-23 17:28:14 -04:00
|
|
|
req.order += 'a';
|
2014-01-25 17:57:25 -05:00
|
|
|
next(err);
|
|
|
|
|
});
|
|
|
|
|
|
2014-07-23 17:28:14 -04:00
|
|
|
route.dispatch(req, {}, function (err) {
|
2022-02-21 19:54:52 -05:00
|
|
|
assert.ok(err)
|
|
|
|
|
assert.strictEqual(err.message, 'foobar')
|
|
|
|
|
assert.strictEqual(req.order, 'a')
|
2014-01-25 17:57:25 -05:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
})
|
2014-04-08 14:50:26 -04:00
|
|
|
|
|
|
|
|
it('should handle throw', function(done) {
|
2014-07-23 17:28:14 -04:00
|
|
|
var req = { order: '', method: 'GET', url: '/' };
|
2014-04-08 14:50:26 -04:00
|
|
|
var route = new Route('');
|
|
|
|
|
|
2023-03-04 13:48:00 +05:30
|
|
|
route.all(function () {
|
2014-04-08 14:50:26 -04:00
|
|
|
throw new Error('foobar');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
route.all(function(req, res, next){
|
2014-07-23 17:28:14 -04:00
|
|
|
req.order += '0';
|
2014-04-08 14:50:26 -04:00
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
route.all(function(err, req, res, next){
|
2014-07-23 17:28:14 -04:00
|
|
|
req.order += 'a';
|
2014-04-08 14:50:26 -04:00
|
|
|
next(err);
|
|
|
|
|
});
|
|
|
|
|
|
2014-07-23 17:28:14 -04:00
|
|
|
route.dispatch(req, {}, function (err) {
|
2022-02-21 19:54:52 -05:00
|
|
|
assert.ok(err)
|
|
|
|
|
assert.strictEqual(err.message, 'foobar')
|
|
|
|
|
assert.strictEqual(req.order, 'a')
|
2014-04-08 14:50:26 -04:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
2014-06-06 11:12:52 -04:00
|
|
|
|
|
|
|
|
it('should handle throwing inside error handlers', function(done) {
|
2014-07-23 17:28:14 -04:00
|
|
|
var req = { method: 'GET', url: '/' };
|
2014-06-06 11:12:52 -04:00
|
|
|
var route = new Route('');
|
|
|
|
|
|
2023-03-04 13:48:00 +05:30
|
|
|
route.get(function () {
|
2014-06-06 11:12:52 -04:00
|
|
|
throw new Error('boom!');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
route.get(function(err, req, res, next){
|
|
|
|
|
throw new Error('oops');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
route.get(function(err, req, res, next){
|
2014-07-23 17:28:14 -04:00
|
|
|
req.message = err.message;
|
|
|
|
|
next();
|
2014-06-06 11:12:52 -04:00
|
|
|
});
|
|
|
|
|
|
2014-07-23 17:28:14 -04:00
|
|
|
route.dispatch(req, {}, function (err) {
|
|
|
|
|
if (err) return done(err);
|
2022-02-21 19:54:52 -05:00
|
|
|
assert.strictEqual(req.message, 'oops')
|
2014-07-23 17:28:14 -04:00
|
|
|
done();
|
|
|
|
|
});
|
2014-06-06 22:16:39 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should handle throw in .all', function(done) {
|
2014-07-23 17:28:14 -04:00
|
|
|
var req = { method: 'GET', url: '/' };
|
2014-06-06 22:16:39 -04:00
|
|
|
var route = new Route('');
|
|
|
|
|
|
|
|
|
|
route.all(function(req, res, next){
|
|
|
|
|
throw new Error('boom!');
|
|
|
|
|
});
|
|
|
|
|
|
2014-07-23 17:28:14 -04:00
|
|
|
route.dispatch(req, {}, function(err){
|
2022-02-21 19:54:52 -05:00
|
|
|
assert.ok(err)
|
|
|
|
|
assert.strictEqual(err.message, 'boom!')
|
2014-06-06 22:16:39 -04:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should handle single error handler', function(done) {
|
2014-07-23 17:28:14 -04:00
|
|
|
var req = { method: 'GET', url: '/' };
|
2014-06-06 22:16:39 -04:00
|
|
|
var route = new Route('');
|
|
|
|
|
|
|
|
|
|
route.all(function(err, req, res, next){
|
|
|
|
|
// this should not execute
|
2022-02-21 19:54:52 -05:00
|
|
|
throw new Error('should not be called')
|
2014-06-06 22:16:39 -04:00
|
|
|
});
|
|
|
|
|
|
2014-07-23 17:28:14 -04:00
|
|
|
route.dispatch(req, {}, done);
|
2014-06-06 11:12:52 -04:00
|
|
|
});
|
2014-01-25 17:57:25 -05:00
|
|
|
})
|
|
|
|
|
})
|