req.baseUrl.js
This file
Path
test/req.baseUrl.js
87 lines · 2.1 KiB · JavaScript
Last changed by
add req.baseUrl to access stripped path in routesDouglas Christopher Wilsonbefore adoption739586f
History of this fileTake it
add req.baseUrl to access stripped path in routesDouglas Christopher Wilsonbefore adoption739586f
lines 1–87why, one step back12var express = require('..')3var request = require('supertest')45describe('req', function(){6 describe('.baseUrl', function(){7 it('should be empty for top-level route', function(done){8 var app = express()910 app.get('/:a', function(req, res){11 res.end(req.baseUrl)12 })1314 request(app)15 .get('/foo')16 .expect(200, '', done)17 })1819 it('should contain lower path', function(done){20 var app = express()21 var sub = express.Router()2223 sub.get('/:b', function(req, res){24 res.end(req.baseUrl)25 })26 app.use('/:a', sub)2728 request(app)29 .get('/foo/bar')30 .expect(200, '/foo', done);31 })3233 it('should contain full lower path', function(done){34 var app = express()35 var sub1 = express.Router()36 var sub2 = express.Router()37 var sub3 = express.Router()3839 sub3.get('/:d', function(req, res){40 res.end(req.baseUrl)41 })42 sub2.use('/:c', sub3)43 sub1.use('/:b', sub2)44 app.use('/:a', sub1)4546 request(app)47 .get('/foo/bar/baz/zed')48 .expect(200, '/foo/bar/baz', done);49 })5051 it('should travel through routers correctly', function(done){52 var urls = []53 var app = express()54 var sub1 = express.Router()55 var sub2 = express.Router()56 var sub3 = express.Router()5758 sub3.get('/:d', function(req, res, next){59 urls.push('0@' + req.baseUrl)60 next()61 })62 sub2.use('/:c', sub3)63 sub1.use('/', function(req, res, next){64 urls.push('1@' + req.baseUrl)65 next()66 })67 sub1.use('/bar', sub2)68 sub1.use('/bar', function(req, res, next){69 urls.push('2@' + req.baseUrl)70 next()71 })72 app.use(function(req, res, next){73 urls.push('3@' + req.baseUrl)74 next()75 })76 app.use('/:a', sub1)77 app.use(function(req, res, next){78 urls.push('4@' + req.baseUrl)79 res.end(urls.join(','))80 })8182 request(app)83 .get('/foo/bar/baz/zed')84 .expect(200, '3@,1@/foo,0@/foo/bar/baz,2@/foo/bar,4@', done);85 })86 })87})