Skip to content
Myr

Router.js

This file

Path

test/Router.js

168 lines · 4.2 KiB · JavaScript

Last changed by

Merge pull request #1984 from blakeembrey/params-updateRoman Shtylmanbefore adoptioncc38ccc

History of this file

Take it

The walk stopped after 200 commits: lines older than f862ad2 point at it.

Release 3.2.2TJ Holowaychukbefore adoptionf862ad2

lines 1–3why, one step back
1
2var express = require('../')3 , Router = express.Router

add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9

line 4why, one step back
4 , methods = require('methods')

Release 3.2.2TJ Holowaychukbefore adoptionf862ad2

lines 5–7why, one step back
5 , assert = require('assert');
67describe('Router', function(){

make express.Router() return a Router function instanceRoman Shtylmanbefore adoptionf8b954b

lines 8–19why, one step back
8 it('should return a function with router methods', function() {
9 var router = Router();10 assert(typeof router == 'function');1112 var router = new Router();13 assert(typeof router == 'function');1415 assert(typeof router.get == 'function');16 assert(typeof router.handle == 'function');17 assert(typeof router.use == 'function');18 });19

Merge pull request #1964 from blakeembrey/modular-routingRoman Shtylmanbefore adoption4867cf1

lines 20–22why, one step back
20 it('should support .use of other routers', function(done){
21 var router = new Router();22 var another = new Router();

make express.Router() return a Router function instanceRoman Shtylmanbefore adoptionf8b954b

line 23why, one step back
23

Merge pull request #1964 from blakeembrey/modular-routingRoman Shtylmanbefore adoption4867cf1

lines 24–25why, one step back
24 another.get('/bar', function(req, res){
25 res.end();

make express.Router() return a Router function instanceRoman Shtylmanbefore adoptionf8b954b

lines 26–28why, one step back
26 });
27 router.use('/foo', another);28

Merge pull request #1964 from blakeembrey/modular-routingRoman Shtylmanbefore adoption4867cf1

lines 29–42why, one step back
29 router.handle({ url: '/foo/bar', method: 'GET' }, { end: done });
30 });3132 it('should support dynamic routes', function(done){33 var router = new Router();34 var another = new Router();3536 another.get('/:bar', function(req, res){37 req.params.bar.should.equal('route');38 res.end();39 });40 router.use('/:foo', another);4142 router.handle({ url: '/test/route', method: 'GET' }, { end: done });

make express.Router() return a Router function instanceRoman Shtylmanbefore adoptionf8b954b

lines 43–45why, one step back
43 });
4445 describe('.handle', function(){

Release 3.2.2TJ Holowaychukbefore adoptionf862ad2

line 46why, one step back
46 it('should dispatch', function(done){

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 47–49why, one step back
47 var router = new Router();
4849 router.route('/foo').get(function(req, res){

Release 3.2.2TJ Holowaychukbefore adoptionf862ad2

lines 50–52why, one step back
50 res.send('foo');
51 });52

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 53–59why, one step back
53 var res = {
54 send: function(val) {55 val.should.equal('foo');56 done();57 }58 }59 router.handle({ url: '/foo', method: 'GET' }, res);

Release 3.2.2TJ Holowaychukbefore adoptionf862ad2

lines 60–65why, one step back
60 })
61 })6263 describe('.multiple callbacks', function(){64 it('should throw if a callback is null', function(){65 assert.throws(function () {

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 66–67why, one step back
66 var router = new Router();
67 router.route('/foo').all(null);

Release 3.2.2TJ Holowaychukbefore adoptionf862ad2

lines 68–72why, one step back
68 })
69 })7071 it('should throw if a callback is undefined', function(){72 assert.throws(function () {

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 73–74why, one step back
73 var router = new Router();
74 router.route('/foo').all(undefined);

Release 3.2.2TJ Holowaychukbefore adoptionf862ad2

lines 75–79why, one step back
75 })
76 })7778 it('should throw if a callback is not a function', function(){79 assert.throws(function () {

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 80–81why, one step back
80 var router = new Router();
81 router.route('/foo').all('not a function');

Release 3.2.2TJ Holowaychukbefore adoptionf862ad2

lines 82–85why, one step back
82 })
83 })8485 it('should not throw if all callbacks are functions', function(){

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 86–87why, one step back
86 var router = new Router();
87 router.route('/foo').all(function(){}).all(function(){});

add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9

lines 88–90why, one step back
88 })
89 })90

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 91–115why, one step back
91 describe('error', function(){
92 it('should skip non error middleware', function(done){93 var router = new Router();9495 router.get('/foo', function(req, res, next){96 next(new Error('foo'));97 });9899 router.get('/bar', function(req, res, next){100 next(new Error('bar'));101 });102103 router.use(function(req, res, next){104 assert(false);105 });106107 router.use(function(err, req, res, next){108 assert.equal(err.message, 'foo');109 done();110 });111112 router.handle({ url: '/foo', method: 'GET' }, {}, done);113 });114 })115

add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9

line 116why, one step back
116 describe('.all', function() {

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

line 117why, one step back
117 it('should support using .all to capture all http verbs', function(done){

add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9

lines 118–119why, one step back
118 var router = new Router();
119

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 120–121why, one step back
120 var count = 0;
121 router.all('/foo', function(){ count++; });

add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9

lines 122–125why, one step back
122
123 var url = '/foo?bar=baz';124125 methods.forEach(function testMethod(method) {

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

line 126why, one step back
126 router.handle({ url: url, method: method }, {}, function() {});

add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9

line 127why, one step back
127 });

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 128–130why, one step back
128
129 assert.equal(count, methods.length);130 done();

Release 3.2.2TJ Holowaychukbefore adoptionf862ad2

lines 131–132why, one step back
131 })
132 })

Merge pull request #1935 from visionmedia/router-params-middlewareRoman Shtylmanbefore adoptioncaa25b5

lines 133–167why, one step back
133
134 describe('.param', function() {135 it('should call param function when routing VERBS', function(done) {136 var router = new Router();137138 router.param('id', function(req, res, next, id) {139 assert.equal(id, '123');140 next();141 });142143 router.get('/foo/:id/bar', function(req, res, next) {144 assert.equal(req.params.id, '123');145 next();146 });147148 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);149 });150151 it('should call param function when routing middleware', function(done) {152 var router = new Router();153154 router.param('id', function(req, res, next, id) {155 assert.equal(id, '123');156 next();157 });158159 router.use('/foo/:id/bar', function(req, res, next) {160 assert.equal(req.params.id, '123');161 assert.equal(req.url, '/baz');162 next();163 });164165 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);166 });167 });

Release 3.2.2TJ Holowaychukbefore adoptionf862ad2

line 168why, one step back
168})