Skip to content
Myr

app.param.js

This file

Path

test/app.param.js

323 lines · 7.4 KiB · JavaScript

Last changed by

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

History of this file

Take it

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

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

line 1why, one step back
1'use strict'

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 2–19why, one step back
2
3var express = require('../')4 , request = require('supertest');56describe('app', function(){7 describe('.param(names, fn)', function(){8 it('should map the array', function(done){9 var app = express();1011 app.param(['id', 'uid'], function(req, res, next, id){12 id = Number(id);13 if (isNaN(id)) return next('route');14 req.params.id = id;15 next();16 });1718 app.get('/post/:id', function(req, res){19 var id = req.params.id;

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

line 20why, one step back
20 res.send((typeof id) + ':' + id)

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 21–24why, one step back
21 });
2223 app.get('/user/:uid', function(req, res){24 var id = req.params.id;

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

line 25why, one step back
25 res.send((typeof id) + ':' + id)

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 26–28why, one step back
26 });
2728 request(app)

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

lines 29–35why, one step back
29 .get('/user/123')
30 .expect(200, 'number:123', function (err) {31 if (err) return done(err)32 request(app)33 .get('/post/123')34 .expect('number:123', done)35 })

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 36–51why, one step back
36 })
37 })3839 describe('.param(name, fn)', function(){40 it('should map logic for a single param', function(done){41 var app = express();4243 app.param('id', function(req, res, next, id){44 id = Number(id);45 if (isNaN(id)) return next('route');46 req.params.id = id;47 next();48 });4950 app.get('/user/:id', function(req, res){51 var id = req.params.id;

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

line 52why, one step back
52 res.send((typeof id) + ':' + id)

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 53–55why, one step back
53 });
5455 request(app)

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

lines 56–57why, one step back
56 .get('/user/123')
57 .expect(200, 'number:123', done)

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 58–126why, one step back
58 })
5960 it('should only call once per request', function(done) {61 var app = express();62 var called = 0;63 var count = 0;6465 app.param('user', function(req, res, next, user) {66 called++;67 req.user = user;68 next();69 });7071 app.get('/foo/:user', function(req, res, next) {72 count++;73 next();74 });75 app.get('/foo/:user', function(req, res, next) {76 count++;77 next();78 });79 app.use(function(req, res) {80 res.end([count, called, req.user].join(' '));81 });8283 request(app)84 .get('/foo/bob')85 .expect('2 1 bob', done);86 })8788 it('should call when values differ', function(done) {89 var app = express();90 var called = 0;91 var count = 0;9293 app.param('user', function(req, res, next, user) {94 called++;95 req.users = (req.users || []).concat(user);96 next();97 });9899 app.get('/:user/bob', function(req, res, next) {100 count++;101 next();102 });103 app.get('/foo/:user', function(req, res, next) {104 count++;105 next();106 });107 app.use(function(req, res) {108 res.end([count, called, req.users.join(',')].join(' '));109 });110111 request(app)112 .get('/foo/bob')113 .expect('2 2 foo,bob', done);114 })115116 it('should support altering req.params across routes', function(done) {117 var app = express();118119 app.param('user', function(req, res, next, user) {120 req.params.user = 'loki';121 next();122 });123124 app.get('/:user', function(req, res, next) {125 next('route');126 });

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 127why, one step back
127 app.get('/:user', function (req, res) {

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 128–144why, one step back
128 res.send(req.params.user);
129 });130131 request(app)132 .get('/bob')133 .expect('loki', done);134 })135136 it('should not invoke without route handler', function(done) {137 var app = express();138139 app.param('thing', function(req, res, next, thing) {140 req.thing = thing;141 next();142 });143144 app.param('user', function(req, res, next, user) {

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

line 145why, one step back
145 next(new Error('invalid invocation'))

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 146–147why, one step back
146 });
147

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 148why, one step back
148 app.post('/:user', function (req, res) {

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 149–151why, one step back
149 res.send(req.params.user);
150 });151

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 152why, one step back
152 app.get('/:thing', function (req, res) {

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 153–261why, one step back
153 res.send(req.thing);
154 });155156 request(app)157 .get('/bob')158 .expect(200, 'bob', done);159 })160161 it('should work with encoded values', function(done){162 var app = express();163164 app.param('name', function(req, res, next, name){165 req.params.name = name;166 next();167 });168169 app.get('/user/:name', function(req, res){170 var name = req.params.name;171 res.send('' + name);172 });173174 request(app)175 .get('/user/foo%25bar')176 .expect('foo%bar', done);177 })178179 it('should catch thrown error', function(done){180 var app = express();181182 app.param('id', function(req, res, next, id){183 throw new Error('err!');184 });185186 app.get('/user/:id', function(req, res){187 var id = req.params.id;188 res.send('' + id);189 });190191 request(app)192 .get('/user/123')193 .expect(500, done);194 })195196 it('should catch thrown secondary error', function(done){197 var app = express();198199 app.param('id', function(req, res, next, val){200 process.nextTick(next);201 });202203 app.param('id', function(req, res, next, id){204 throw new Error('err!');205 });206207 app.get('/user/:id', function(req, res){208 var id = req.params.id;209 res.send('' + id);210 });211212 request(app)213 .get('/user/123')214 .expect(500, done);215 })216217 it('should defer to next route', function(done){218 var app = express();219220 app.param('id', function(req, res, next, id){221 next('route');222 });223224 app.get('/user/:id', function(req, res){225 var id = req.params.id;226 res.send('' + id);227 });228229 app.get('/:name/123', function(req, res){230 res.send('name');231 });232233 request(app)234 .get('/user/123')235 .expect('name', done);236 })237238 it('should defer all the param routes', function(done){239 var app = express();240241 app.param('id', function(req, res, next, val){242 if (val === 'new') return next('route');243 return next();244 });245246 app.all('/user/:id', function(req, res){247 res.send('all.id');248 });249250 app.get('/user/:id', function(req, res){251 res.send('get.id');252 });253254 app.get('/user/new', function(req, res){255 res.send('get.new');256 });257258 request(app)259 .get('/user/new')260 .expect('get.new', done);261 })

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 262–321why, one step back
262
263 it('should not call when values differ on error', function(done) {264 var app = express();265 var called = 0;266 var count = 0;267268 app.param('user', function(req, res, next, user) {269 called++;270 if (user === 'foo') throw new Error('err!');271 req.user = user;272 next();273 });274275 app.get('/:user/bob', function(req, res, next) {276 count++;277 next();278 });279 app.get('/foo/:user', function(req, res, next) {280 count++;281 next();282 });283284 app.use(function(err, req, res, next) {285 res.status(500);286 res.send([count, called, err.message].join(' '));287 });288289 request(app)290 .get('/foo/bob')291 .expect(500, '0 1 err!', done)292 });293294 it('should call when values differ when using "next"', function(done) {295 var app = express();296 var called = 0;297 var count = 0;298299 app.param('user', function(req, res, next, user) {300 called++;301 if (user === 'foo') return next('route');302 req.user = user;303 next();304 });305306 app.get('/:user/bob', function(req, res, next) {307 count++;308 next();309 });310 app.get('/foo/:user', function(req, res, next) {311 count++;312 next();313 });314 app.use(function(req, res) {315 res.end([count, called, req.user].join(' '));316 });317318 request(app)319 .get('/foo/bob')320 .expect('1 2 bob', done);321 })

deps: serve-static@~1.4.2Douglas Christopher Wilsonbefore adoption0ee4dd8

lines 322–323why, one step back
322 })
323})