Added relative redirect support. Closes #920
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent a1e325a · Browse files · 2 files·+71 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+71 added−20 removed
- tests:+59 added−13 removed
- other:+12 added−7 removed
Provenance
- Commit
43c8764- Tree
d8b60aa- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
a1e325a
2 files changed+71 added−20 removed
modified:lib/response.js+12 added-7 removed
478 unmodified lines479 status = url;480 url = arguments[1];481 }482 483- // Setup redirect map483+ // setup redirect map484 var map = {485 back: req.header('Referrer', root)486 , home: root487 };488 489- // Support custom redirect map489+ // support custom redirect map490 map.__proto__ = app.redirects;491 492- // Attempt mapped redirect492+ // attempt mapped redirect493 var mapped = 'function' == typeof map[url]494 ? map[url](req, this)495 : map[url];496 497- // Perform redirect497+ // perform redirect498 url = mapped || url;499 500- // Relative500+ // relative501 if (!~url.indexOf('://')) {502 var path = app.path() || root;503 504- // Respect mount-point505- if (0 != url.indexOf(path)) url = path + url;504+ // relative to host505+ if ('/' == url[0]) {506 507+ // relative to mount-point508+ } else if (1 != url.indexOf(path)) {509+ url = path + '/' + url;510+ }511+512 // Absolute513 var host = req.header('Host')514 , tls = false // TODO: req.secure;515 88 unmodified linesmodified:test/res.redirect.js+59 added-13 removed
37 unmodified lines38394041424243444344454647464748484950515052535452535455565755565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810973 unmodified lines37 unmodified lines }) }) describe('when mounted', function(){ describe('deeply', function(){ it('should respect the mount-point', function(done){ var app = express() , admin = express(); it('should respect the mount-point', function(done){ var app = express() , blog = express() , admin = express();
admin.use(function(req, res){ res.redirect('/admin/login'); }); admin.use(function(req, res){ res.redirect('login'); });
app.use('/blog', admin); app.use('/blog', blog); blog.use('/admin', admin);
request(app) .get('/blog') .set('Host', 'example.com') .end(function(res){ res.headers.should.have.property('location', 'http://example.com/blog/admin/login'); done(); request(app) .get('/blog/admin') .set('Host', 'example.com') .end(function(res){ res.headers.should.have.property('location', 'http://example.com/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.redirect('admin/login'); });
app.use('/blog', admin);
request(app) .get('/blog') .set('Host', 'example.com') .end(function(res){ res.headers.should.have.property('location', 'http://example.com/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.redirect('/admin/login'); });
app.use('/blog', admin);
request(app) .get('/blog') .set('Host', 'example.com') .end(function(res){ res.headers.should.have.property('location', 'http://example.com/admin/login'); done(); }) }) }) }) })
describe('.redirect(status, url)', function(){73 unmodified lines