Skip to content
Myr

Router.js

This file

Path

test/Router.js

156 lines · 3.9 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

refactorTJ Holowaychukbefore adoptiona566624

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')

refactorTJ Holowaychukbefore adoptiona566624

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

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

lines 9–33why, one step back
9 it('should return a function with router methods', function() {
10 var router = Router();11 assert(typeof router == 'function');1213 var router = new Router();14 assert(typeof router == 'function');1516 assert(typeof router.get == 'function');17 assert(typeof router.handle == 'function');18 assert(typeof router.use == 'function');19 });2021 it('should support .use of other routers', function(done) {22 var router = Router();23 var another = Router();2425 another.get('/bar', function(req, res) {26 res.done();27 });28 router.use('/foo', another);2930 router.handle({ url: '/foo/bar', method: 'GET' }, { done: done });31 });3233 describe('.handle', function(){

refactorTJ Holowaychukbefore adoptiona566624

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

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

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

refactorTJ Holowaychukbefore adoptiona566624

lines 38–40why, one step back
38 res.send('foo');
39 });40

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 41–47why, one step back
41 var res = {
42 send: function(val) {43 val.should.equal('foo');44 done();45 }46 }47 router.handle({ url: '/foo', method: 'GET' }, res);

refactorTJ Holowaychukbefore adoptiona566624

lines 48–53why, one step back
48 })
49 })5051 describe('.multiple callbacks', function(){52 it('should throw if a callback is null', function(){53 assert.throws(function () {

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

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

refactorTJ Holowaychukbefore adoptiona566624

lines 56–60why, one step back
56 })
57 })5859 it('should throw if a callback is undefined', function(){60 assert.throws(function () {

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

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

refactorTJ Holowaychukbefore adoptiona566624

lines 63–67why, one step back
63 })
64 })6566 it('should throw if a callback is not a function', function(){67 assert.throws(function () {

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

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

refactorTJ Holowaychukbefore adoptiona566624

lines 70–73why, one step back
70 })
71 })7273 it('should not throw if all callbacks are functions', function(){

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

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

add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9

lines 76–78why, one step back
76 })
77 })78

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 79–103why, one step back
79 describe('error', function(){
80 it('should skip non error middleware', function(done){81 var router = new Router();8283 router.get('/foo', function(req, res, next){84 next(new Error('foo'));85 });8687 router.get('/bar', function(req, res, next){88 next(new Error('bar'));89 });9091 router.use(function(req, res, next){92 assert(false);93 });9495 router.use(function(err, req, res, next){96 assert.equal(err.message, 'foo');97 done();98 });99100 router.handle({ url: '/foo', method: 'GET' }, {}, done);101 });102 })103

add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9

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

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

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

add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9

lines 106–107why, one step back
106 var router = new Router();
107

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

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

add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9

lines 110–113why, one step back
110
111 var url = '/foo?bar=baz';112113 methods.forEach(function testMethod(method) {

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

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

add Router.all() methodRoman Shtylmanbefore adoptionf47c0d9

line 115why, one step back
115 });

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 116–118why, one step back
116
117 assert.equal(count, methods.length);118 done();

refactorTJ Holowaychukbefore adoptiona566624

lines 119–120why, one step back
119 })
120 })

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

lines 121–155why, one step back
121
122 describe('.param', function() {123 it('should call param function when routing VERBS', function(done) {124 var router = new Router();125126 router.param('id', function(req, res, next, id) {127 assert.equal(id, '123');128 next();129 });130131 router.get('/foo/:id/bar', function(req, res, next) {132 assert.equal(req.params.id, '123');133 next();134 });135136 router.handle({ url: '/foo/123/bar', method: 'get' }, {}, done);137 });138139 it('should call param function when routing middleware', function(done) {140 var router = new Router();141142 router.param('id', function(req, res, next, id) {143 assert.equal(id, '123');144 next();145 });146147 router.use('/foo/:id/bar', function(req, res, next) {148 assert.equal(req.params.id, '123');149 assert.equal(req.url, '/baz');150 next();151 });152153 router.handle({ url: '/foo/123/bar/baz', method: 'get' }, {}, done);154 });155 });

refactorTJ Holowaychukbefore adoptiona566624

line 156why, one step back
156})