Skip to content
Myr

Route.js

This file

Path

test/Route.js

144 lines · 2.9 KiB · JavaScript

Last changed by

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

History of this file

Take it

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 1–144why, one step back
1
2var express = require('../')3 , Route = express.Route4 , methods = require('methods')5 , assert = require('assert');67describe('Route', function(){89 describe('.all', function(){10 it('should add handler', function(done){11 var route = new Route('/foo');1213 route.all(function(req, res, next) {14 assert.equal(req.a, 1);15 assert.equal(res.b, 2);16 next();17 });1819 route.dispatch({ a:1, method: 'GET' }, { b:2 }, done);20 })2122 it('should handle VERBS', function(done) {23 var route = new Route('/foo');2425 var count = 0;26 route.all(function(req, res, next) {27 count++;28 });2930 methods.forEach(function testMethod(method) {31 route.dispatch({ method: method }, {});32 });3334 assert.equal(count, methods.length);35 done();36 })3738 it('should stack', function(done) {39 var route = new Route('/foo');4041 var count = 0;42 route.all(function(req, res, next) {43 count++;44 next();45 });4647 route.all(function(req, res, next) {48 count++;49 next();50 });5152 route.dispatch({ method: 'GET' }, {}, function(err) {53 assert.ifError(err);54 count++;55 });5657 assert.equal(count, 3);58 done();59 })60 })6162 describe('.VERB', function(){63 it('should support .get', function(done){64 var route = new Route('');6566 var count = 0;67 route.get(function(req, res, next) {68 count++;69 })7071 route.dispatch({ method: 'GET' }, {});72 assert(count);73 done();74 })7576 it('should limit to just .VERB', function(done){77 var route = new Route('');7879 route.get(function(req, res, next) {80 assert(false);81 done();82 })8384 route.post(function(req, res, next) {85 assert(true);86 })8788 route.dispatch({ method: 'post' }, {});89 done();90 })9192 it('should allow fallthrough', function(done){93 var route = new Route('');9495 var order = '';96 route.get(function(req, res, next) {97 order += 'a';98 next();99 })100101 route.all(function(req, res, next) {102 order += 'b';103 next();104 });105106 route.get(function(req, res, next) {107 order += 'c';108 })109110 route.dispatch({ method: 'get' }, {});111 assert.equal(order, 'abc');112 done();113 })114 })115116 describe('errors', function(){117 it('should handle errors via arity 4 functions', function(done){118 var route = new Route('');119120 var order = '';121 route.all(function(req, res, next){122 next(new Error('foobar'));123 });124125 route.all(function(req, res, next){126 order += '0';127 next();128 });129130 route.all(function(err, req, res, next){131 order += 'a';132 next(err);133 });134135 route.all(function(err, req, res, next){136 assert.equal(err.message, 'foobar');137 assert.equal(order, 'a');138 done();139 });140141 route.dispatch({ method: 'get' }, {});142 })143 })144})