Merge branch 'integrate'
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 · parents 0ebebd8, 49abd7b · Browse files · 3 files·+231 added−189 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+231 added−189 removed
- tests:+171 added−158 removed
- other:+60 added−31 removed
Provenance
- Commit
6b1d7a9- Tree
cb009d4- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent 1
0ebebd8- Parent 2
49abd7b
3 files changed+231 added−189 removed
modified:lib/response.js+60 added-31 removed
586 unmodified lines587 this.set('Set-Cookie', cookie.serialize(name, String(val), options));588 return this;589 };590 591+592 /**592- * Redirect to the given `url` with optional response `status`593- * defaulting to 302.593+ * Set the location header to `url`.594 *595 * The given `url` can also be the name of a mapped url, for596 * example by default express supports "back" which redirects597 * to the _Referrer_ or _Referer_ headers or "/".598 *599 * Examples:600 *601- * res.redirect('/foo/bar');602- * res.redirect('http://example.com');603- * res.redirect(301, 'http://example.com');604- * res.redirect('http://example.com', 301);605- * res.redirect('../login'); // /blog/post/1 -> /blog/login601+ * res.location('/foo/bar').;602+ * res.location('http://example.com');603+ * res.location('../login'); // /blog/post/1 -> /blog/login604 *605 * Mounting:606 *609- * When an application is mounted, and `res.redirect()`610- * is given a path that does _not_ lead with "/". For611- * example suppose a "blog" app is mounted at "/blog",612- * the following redirect would result in "/blog/login":607+ * When an application is mounted and `res.location()`608+ * is given a path that does _not_ lead with "/" it becomes609+ * relative to the mount-point. For example if the application610+ * is mounted at "/blog", the following would become "/blog/login".611 *614- * res.redirect('login');612+ * res.location('login');613 *616- * While the leading slash would result in a redirect to "/login":614+ * While the leading slash would result in a location of "/login":615 *618- * res.redirect('/login');616+ * res.location('/login');617 *618 * @param {String} url621- * @param {Number} code619 * @api public620 */621 625-res.redirect = function(url){622+res.location = function(url){623 var app = this.app627- , req = this.req628- , head = 'HEAD' == req.method629- , status = 302630- , body;624+ , req = this.req;625 632- // allow status / url633- if (2 == arguments.length) {634- if ('number' == typeof url) {635- status = url;636- url = arguments[1];637- } else {638- status = arguments[1];639- }640- }641-626 // setup redirect map627 var map = { back: req.get('Referrer') || '/' };628 629 // perform redirect11 unmodified lines641 url = path + '/' + url;642 }643 }644 645+ // Respond646+ this.set('Location', url);647+ return this;648+};649+650+/**651+ * Redirect to the given `url` with optional response `status`652+ * defaulting to 302.653+ *654+ * The resulting `url` is determined by `res.location()`, so655+ * it will play nicely with mounted apps, relative paths,656+ * `"back"` etc.657+ *658+ * Examples:659+ *660+ * res.redirect('/foo/bar');661+ * res.redirect('http://example.com');662+ * res.redirect(301, 'http://example.com');663+ * res.redirect('http://example.com', 301);664+ * res.redirect('../login'); // /blog/post/1 -> /blog/login665+ *666+ * @param {String} url667+ * @param {Number} code668+ * @api public669+ */670+671+res.redirect = function(url){672+ var app = this.app673+ , head = 'HEAD' == this.req.method674+ , status = 302675+ , body;676+677+ // allow status / url678+ if (2 == arguments.length) {679+ if ('number' == typeof url) {680+ status = url;681+ url = arguments[1];682+ } else {683+ status = arguments[1];684+ }685+ }686+687+ // Set location header688+ this.location(url);689+ url = this.get('Location');690+691 // Support text/{plain,html} by default692 this.format({693 text: function(){694 body = statusCodes[status] + '. Redirecting to ' + encodeURI(url);10 unmodified lines705 });706 707 // Respond708 this.statusCode = status;679- this.set('Location', url);709 this.set('Content-Length', Buffer.byteLength(body));710 this.end(head ? null : body);711 };712 39 unmodified linesadded:test/res.location.js+171 added
1+2+var express = require('../')3+ , request = require('./support/http');4+5+describe('res', function(){6+ describe('.location(url)', function(){7+ it('should set the header', function(done){8+ var app = express();9+10+ app.use(function(req, res){11+ res.location('http://google.com').end();12+ });13+14+ request(app)15+ .get('/')16+ .end(function(err, res){17+ res.headers.should.have.property('location', 'http://google.com');18+ done();19+ })20+ })21+ 22+ describe('with leading //', function(){23+ it('should pass through scheme-relative urls', function(done){24+ var app = express();25+26+ app.use(function(req, res){27+ res.location('//cuteoverload.com').end();28+ });29+30+ request(app)31+ .get('/')32+ .end(function(err, res){33+ res.headers.should.have.property('location', '//cuteoverload.com');34+ done();35+ })36+ })37+ })38+ 39+ describe('with leading /', function(){40+ it('should construct scheme-relative urls', function(done){41+ var app = express();42+43+ app.use(function(req, res){44+ res.location('/login').end();45+ });46+47+ request(app)48+ .get('/')49+ .end(function(err, res){50+ res.headers.should.have.property('location', '/login');51+ done();52+ })53+ })54+ })55+56+ describe('with leading ./', function(){57+ it('should construct path-relative urls', function(done){58+ var app = express();59+60+ app.use(function(req, res){61+ res.location('./edit').end();62+ });63+64+ request(app)65+ .get('/post/1')66+ .end(function(err, res){67+ res.headers.should.have.property('location', '/post/1/./edit');68+ done();69+ })70+ })71+ })72+73+ describe('with leading ../', function(){74+ it('should construct path-relative urls', function(done){75+ var app = express();76+77+ app.use(function(req, res){78+ res.location('../new').end();79+ });80+81+ request(app)82+ .get('/post/1')83+ .end(function(err, res){84+ res.headers.should.have.property('location', '/post/1/../new');85+ done();86+ })87+ })88+ })89+ 90+ describe('without leading /', function(){91+ it('should construct mount-point relative urls', function(done){92+ var app = express();93+94+ app.use(function(req, res){95+ res.location('login').end();96+ });97+98+ request(app)99+ .get('/')100+ .end(function(err, res){101+ res.headers.should.have.property('location', '/login');102+ done();103+ })104+ })105+ })106+ 107+ describe('when mounted', function(){108+ describe('deeply', function(){109+ it('should respect the mount-point', function(done){110+ var app = express()111+ , blog = express()112+ , admin = express();113+114+ admin.use(function(req, res){115+ res.location('login').end();116+ });117+118+ app.use('/blog', blog);119+ blog.use('/admin', admin);120+121+ request(app)122+ .get('/blog/admin')123+ .end(function(err, res){124+ res.headers.should.have.property('location', '/blog/admin/login');125+ done();126+ })127+ })128+ })129+130+ describe('omitting leading /', function(){131+ it('should respect the mount-point', function(done){132+ var app = express()133+ , admin = express();134+135+ admin.use(function(req, res){136+ res.location('admin/login').end();137+ });138+139+ app.use('/blog', admin);140+141+ request(app)142+ .get('/blog')143+ .end(function(err, res){144+ res.headers.should.have.property('location', '/blog/admin/login');145+ done();146+ })147+ })148+ })149+150+ describe('providing leading /', function(){151+ it('should ignore mount-point', function(done){152+ var app = express()153+ , admin = express();154+155+ admin.use(function(req, res){156+ res.location('/admin/login').end();157+ });158+159+ app.use('/blog', admin);160+161+ request(app)162+ .get('/blog')163+ .end(function(err, res){164+ res.headers.should.have.property('location', '/admin/login');165+ done();166+ })167+ })168+ })169+ })170+ })171+})modified:test/res.redirect.js-158 removed
17 unmodified lines18 res.headers.should.have.property('location', 'http://google.com');19 done();20 })21 })22-23- describe('with leading //', function(){24- it('should pass through scheme-relative urls', function(done){25- var app = express();26-27- app.use(function(req, res){28- res.redirect('//cuteoverload.com');29- });30-31- request(app)32- .get('/')33- .set('Host', 'example.com')34- .end(function(err, res){35- res.headers.should.have.property('location', '//cuteoverload.com');36- done();37- })38- })39- })40-41- 42- describe('with leading /', function(){43- it('should construct scheme-relative urls', function(done){44- var app = express();45-46- app.use(function(req, res){47- res.redirect('/login');48- });49-50- request(app)51- .get('/')52- .set('Host', 'example.com')53- .end(function(err, res){54- res.headers.should.have.property('location', '/login');55- done();56- })57- })58- })59-60- describe('with leading ./', function(){61- it('should construct path-relative urls', function(done){62- var app = express();63-64- app.use(function(req, res){65- res.redirect('./edit');66- });67-68- request(app)69- .get('/post/1')70- .set('Host', 'example.com')71- .end(function(err, res){72- res.headers.should.have.property('location', '/post/1/./edit');73- done();74- })75- })76- })77-78- describe('with leading ../', function(){79- it('should construct path-relative urls', function(done){80- var app = express();81-82- app.use(function(req, res){83- res.redirect('../new');84- });85-86- request(app)87- .get('/post/1')88- .set('Host', 'example.com')89- .end(function(err, res){90- res.headers.should.have.property('location', '/post/1/../new');91- done();92- })93- })94- })95- 96- describe('without leading /', function(){97- it('should construct mount-point relative urls', function(done){98- var app = express();99-100- app.use(function(req, res){101- res.redirect('login');102- });103-104- request(app)105- .get('/')106- .set('Host', 'example.com')107- .end(function(err, res){108- res.headers.should.have.property('location', '/login');109- done();110- })111- })112- })113- 114- describe('when mounted', function(){115- describe('deeply', function(){116- it('should respect the mount-point', function(done){117- var app = express()118- , blog = express()119- , admin = express();120-121- admin.use(function(req, res){122- res.redirect('login');123- });124-125- app.use('/blog', blog);126- blog.use('/admin', admin);127-128- request(app)129- .get('/blog/admin')130- .set('Host', 'example.com')131- .end(function(err, res){132- res.headers.should.have.property('location', '/blog/admin/login');133- done();134- })135- })136- })137-138- describe('omitting leading /', function(){139- it('should respect the mount-point', function(done){140- var app = express()141- , admin = express();142-143- admin.use(function(req, res){144- res.redirect('admin/login');145- });146-147- app.use('/blog', admin);148-149- request(app)150- .get('/blog')151- .set('Host', 'example.com')152- .end(function(err, res){153- res.headers.should.have.property('location', '/blog/admin/login');154- done();155- })156- })157- })158-159- describe('providing leading /', function(){160- it('should ignore mount-point', function(done){161- var app = express()162- , admin = express();163-164- admin.use(function(req, res){165- res.redirect('/admin/login');166- });167-168- app.use('/blog', admin);169-170- request(app)171- .get('/blog')172- .set('Host', 'example.com')173- .end(function(err, res){174- res.headers.should.have.property('location', '/admin/login');175- done();176- })177- })178- })179- })22 })23 24 describe('.redirect(status, url)', function(){25 it('should set the response status', function(done){147 unmodified lines