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
Provenance
- Commit
aac1d52- Tree
ab7d2cf- Author
Jonathan Ong <jonathanrichardong@gmail.com>- Committer
Jonathan Ong <jonathanrichardong@gmail.com>- Parent
f41d09a
4 files changed+6 added−273 removed
modified:lib/response.js+2 added-31 removed
611 unmodified lines612 * 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 */621 622 res.location = function(url){636- var app = this.app637- , req = this.req638- , path;623+ var req = this.req;624 625 // "back" is an alias for the referrer626 if ('back' == url) url = req.get('Referrer') || '/';627 643- // 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- }656-628 // Respond629 this.set('Location', url);630 return this;631 };140 unmodified linesmodified:test/acceptance/auth.js+3 added-3 removed
16 unmodified lines171819202121222324252627282929303132333435363738393940414216 unmodified lines describe('GET /',function(){ it('should redirect to /login', function(done){ request(app) .get('/') .end(redirects(/\/login$/, done)) .end(redirects(/login$/, done)) }) })
describe('GET /restricted (w/o cookie)',function(){ it('should redirect to /login', function(done){ request(app) .get('/restricted') .end(redirects(/\/login$/,done)) .end(redirects(/login$/,done)) }) })
describe('POST /login', function(){ it('should fail without proper credentials', function(done){ request(app) .post('/login') .type('urlencoded') .send('username=not-tj&password=foobar') .end(redirects(/\/login$/, done)) .end(redirects(/login$/, done)) }) })})No newline at end of fileNo newline at end of filemodified:test/res.location.js-166 removed
16 unmodified lines1718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186212216 unmodified lines res.headers.should.have.property('location', 'http://google.com'); done(); }) }) describe('with leading //', function(){ it('should pass through scheme-relative urls', function(done){ var app = express();
app.use(function(req, res){ res.location('//cuteoverload.com').end(); });
request(app) .get('/') .end(function(err, res){ res.headers.should.have.property('location', '//cuteoverload.com'); done(); }) }) }) describe('with leading /', function(){ it('should construct scheme-relative urls', function(done){ var app = express();
app.use(function(req, res){ res.location('/login').end(); });
request(app) .get('/') .end(function(err, res){ res.headers.should.have.property('location', '/login'); done(); }) }) })
describe('with leading ./', function(){ it('should construct path-relative urls', function(done){ var app = express();
app.use(function(req, res){ res.location('./edit').end(); });
request(app) .get('/post/1') .end(function(err, res){ res.headers.should.have.property('location', '/post/1/edit'); done(); }) }) })
describe('with leading ../', function(){ it('should construct path-relative urls', function(done){ var app = express();
app.use(function(req, res){ res.location('../new').end(); });
request(app) .get('/post/1') .end(function(err, res){ res.headers.should.have.property('location', '/post/new'); done(); }) }) })
describe('with leading ./ and containing ..', function(){ it('should construct path-relative urls', function(done){ var app = express();
app.use(function(req, res){ res.location('./skip/../../new').end(); });
request(app) .get('/post/1') .end(function(err, res){ res.headers.should.have.property('location', '/post/new'); done(); }) }) }) describe('without leading /', function(){ it('should construct mount-point relative urls', function(done){ var app = express();
app.use(function(req, res){ res.location('login').end(); });
request(app) .get('/') .end(function(err, res){ res.headers.should.have.property('location', '/login'); done(); }) }) }) describe('when mounted', function(){ describe('deeply', function(){ it('should respect the mount-point', function(done){ var app = express() , blog = express() , admin = express();
admin.use(function(req, res){ res.location('login').end(); });
app.use('/blog', blog); blog.use('/admin', admin);
request(app) .get('/blog/admin') .end(function(err, res){ res.headers.should.have.property('location', '/blog/admin/login'); done(); }) }) })
describe('omitting leading /', function(){ it('should respect the mount-point', function(done){ var app = express() , admin = express();
admin.use(function(req, res){ res.location('admin/login').end(); });
app.use('/blog', admin);
request(app) .get('/blog') .end(function(err, res){ res.headers.should.have.property('location', '/blog/admin/login'); done(); }) }) })
describe('providing leading /', function(){ it('should ignore mount-point', function(done){ var app = express() , admin = express();
admin.use(function(req, res){ res.location('/admin/login').end(); });
app.use('/blog', admin);
request(app) .get('/blog') .end(function(err, res){ res.headers.should.have.property('location', '/admin/login'); done(); }) }) }) }) })})modified:test/res.redirect.js+1 added-73 removed
103 unmodified lines104 .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="/<lame>">/<lame></a></p>');108+ res.text.should.equal('<p>Moved Temporarily. Redirecting to <a href="<lame>"><lame></a></p>');109 done();110 })111 })112 })55 unmodified lines168 done();169 })170 })171 })172-173- describe('responses redirected to relative paths', function(){174- function create(depth, parent) {175- var app = express();176-177- if (parent) {178- parent.use('/depth' + depth, app);179- }180-181- app.get('/', function(req, res){182- res.redirect('./index');183- });184-185- app.get('/index', function(req, res){186- res.json({ depth: depth, content: 'index' });187- });188-189- return app;190- }191-192- var root = create(0);193- var depth1 = create(1, root);194- var depth2 = create(2, depth1);195- var depth3 = create(3, depth2);196-197- root.use('/depth2', depth2);198- root.use('/depth3', depth3);199-200- 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- })208-209- 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');214-215- request(root)216- .get('/depth1/depth2')217- .end(function(err, res){218- res.headers.should.have.property('location', '/depth1/depth2/index');219-220- 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- })229-230- 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 })