Skip to content
Myr

Merge pull request #1516 from PatternConsulting/master

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix Dotted Relative Redirects in Applications Mounted on Nested Paths

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parents b7afa4f, 97edb23 · Browse files · 3 files+107 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+107 added−7 removed

  • tests:+102 added−4 removed
  • other:+5 added−3 removed

3 files+107 added−7 removed

modified:lib/response.js+5 added-3 removed
632 unmodified lines
633 url = map[url] || url;634635 // relative636 if (!~url.indexOf('://') && 0 != url.indexOf('//')) {637 var path = app.path();637 var path638639 // relative to path640 if ('.' == url[0]) {641 path = req.originalUrl.split('?')[0]641 url = req.path + '/' + url;642 // relative to mount-point642 url = path + ('/' == path[path.length - 1] ? '' : '/') + url;643 // relative to mount-point644 } else if ('/' != url[0]) {645 path = app.path();646 url = path + '/' + url;647 }648 }649
107 unmodified lines
modified:test/req.signedCookies.js+10 added-2 removed
modified:test/res.redirect.js+92 added-2 removed
72 unmodified lines
73 done();74 })75 })76 })77 7778 describe('when accepting html', function(){79 it('should respond with html', function(done){80 var app = express();81
27 unmodified lines
109 done();110 })111 })112 })113 113114 describe('when accepting text', function(){115 it('should respond with text', function(done){116 var app = express();117
50 unmodified lines
168 done();169 })170 })171 })172173 describe('responses redirected to dotted relative paths', function () {174175 /* The requisite test fixtures are repetitive. */176 var makeApp = function (depth, parent) {177 var app = express();178179 if (parent) {180 parent.use('/depth' + depth, app);181 }182183 app.get('/', function (req, res) {184 res.redirect('./index');185 });186187 app.get('/index', function (req, res) {188 res.json({depth : depth, content : 'index'});189 });190191 return app;192 }193194 var root = makeApp(0);195 var depth1 = makeApp(1, root);196 var depth2 = makeApp(2, depth1);197 var depth3 = makeApp(3, depth2);198199 /* Special cases for alias paths. */200 root.use('/depth2', depth2);201 root.use('/depth3', depth3);202203 /*204 * The resulting structure resembles the following.205 *206 * / -> root207 *208 * /depth1 -> depth1209 * /depth1/depth2 -> depth2210 * /depth1/depth2/depth3 -> depth3211 *212 * /depth2 -> depth2213 * /depth3 -> depth3214 */215216 it('should not contain redundant leading slashes in the location header', function (done) {217 request(root)218 .get('/')219 .end(function (err, res) {220 res.headers.location.search(/^\/{2}/).should.equal(-1);221 done();222 })223 })224225 it('should preserve context when redirecting nested applications at any depth', function(done){226 request(root)227 .get('/depth1')228 .end(function(err, res) {229 res.headers.should.have.property('location', '/depth1/./index');230 });231232 request(root)233 .get('/depth1/depth2')234 .end(function(err, res) {235 res.headers.should.have.property('location', '/depth1/depth2/./index');236 })237238 request(root)239 .get('/depth1/depth2/depth3')240 .end(function(err, res) {241 res.headers.should.have.property('location', '/depth1/depth2/depth3/./index');242 done();243 })244 })245246 it('should redirect correctly for nested applications that have been remounted', function (done) {247 request(root)248 .get('/depth2')249 .end(function (err, res) {250 res.headers.should.have.property('location', '/depth2/./index');251 })252253 request(root)254 .get('/depth3')255 .end(function (err, res) {256 res.headers.should.have.property('location', '/depth3/./index');257 done();258 })259 })260261 })262})