Skip to content
Myr

res.location: remove resolving relative urls

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

closes #1804 this is an unnecessary maintenance burden (see the number of removed tests), especially when supporting mounting. browsers handle relative locations, and so should all clients. a regression could be absolute locations on a mounted app, but 1. we can fix that later when someone complains and 2) code-smell

authored by Jonathan Ong · committed by Jonathan Ong · parent f41d09a · Browse files · 4 files+6 added−273 removed
This commit

State

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

Diff stats

4 files+6 added−273 removed

  • tests:+4 added−242 removed
  • other:+2 added−31 removed

4 files+6 added−273 removed

modified:lib/response.js+2 added-31 removed
611 unmodified lines
612 * Examples:613 *614 * res.location('/foo/bar').;615 * res.location('http://example.com');616 * res.location('../login'); // /blog/post/1 -> /blog/login616 * res.location('../login');617 *618 * Mounting:619 *620 * When an application is mounted and `res.location()`621 * is given a path that does _not_ lead with "/" it becomes622 * relative to the mount-point. For example if the application623 * is mounted at "/blog", the following would become "/blog/login".624 *625 * res.location('login');626 *627 * While the leading slash would result in a location of "/login":628 *629 * res.location('/login');630 *618 * @param {String} url619 * @api public620 */621622res.location = function(url){636 var app = this.app637 , req = this.req638 , path;623 var req = this.req;624625 // "back" is an alias for the referrer626 if ('back' == url) url = req.get('Referrer') || '/';627643 // relative644 if (!~url.indexOf('://') && 0 != url.indexOf('//')) {645 // relative to path646 if ('.' == url[0]) {647 path = req.originalUrl.split('?')[0];648 path = path + ('/' == path[path.length - 1] ? '' : '/');649 url = resolve(path, url);650 // relative to mount-point651 } else if ('/' != url[0]) {652 path = app.path();653 url = path + '/' + url;654 }655 }656628 // Respond629 this.set('Location', url);630 return this;631};
140 unmodified lines
modified:test/acceptance/auth.js+3 added-3 removed
modified:test/res.location.js-166 removed
modified:test/res.redirect.js+1 added-73 removed
103 unmodified lines
104 .get('/')105 .set('Host', 'http://example.com')106 .set('Accept', 'text/html')107 .end(function(err, res){108 res.text.should.equal('<p>Moved Temporarily. Redirecting to <a href="/&lt;lame&gt;">/&lt;lame&gt;</a></p>');108 res.text.should.equal('<p>Moved Temporarily. Redirecting to <a href="&lt;lame&gt;">&lt;lame&gt;</a></p>');109 done();110 })111 })112 })
55 unmodified lines
168 done();169 })170 })171 })172173 describe('responses redirected to relative paths', function(){174 function create(depth, parent) {175 var app = express();176177 if (parent) {178 parent.use('/depth' + depth, app);179 }180181 app.get('/', function(req, res){182 res.redirect('./index');183 });184185 app.get('/index', function(req, res){186 res.json({ depth: depth, content: 'index' });187 });188189 return app;190 }191192 var root = create(0);193 var depth1 = create(1, root);194 var depth2 = create(2, depth1);195 var depth3 = create(3, depth2);196197 root.use('/depth2', depth2);198 root.use('/depth3', depth3);199200 it('should not contain redundant leading slashes in the location header', function(done){201 request(root)202 .get('/')203 .end(function(err, res){204 res.headers.location.search(/^\/{2}/).should.equal(-1);205 done();206 })207 })208209 it('should preserve context when redirecting nested applications at any depth', function(done){210 request(root)211 .get('/depth1')212 .end(function(err, res){213 res.headers.should.have.property('location', '/depth1/index');214215 request(root)216 .get('/depth1/depth2')217 .end(function(err, res){218 res.headers.should.have.property('location', '/depth1/depth2/index');219220 request(root)221 .get('/depth1/depth2/depth3')222 .end(function(err, res){223 res.headers.should.have.property('location', '/depth1/depth2/depth3/index');224 done();225 })226 })227 });228 })229230 it('should redirect correctly for nested applications that have been remounted', function(done){231 request(root)232 .get('/depth2')233 .end(function(err, res){234 res.headers.should.have.property('location', '/depth2/index');235 request(root)236 .get('/depth3')237 .end(function(err, res){238 res.headers.should.have.property('location', '/depth3/index');239 done();240 })241 })242 })243 })172})