Skip to content
Myr

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

3 files+231 added−189 removed

modified:lib/response.js+60 added-31 removed
586 unmodified lines
587 this.set('Set-Cookie', cookie.serialize(name, String(val), options));588 return this;589};590591592/**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 */621625res.redirect = function(url){622res.location = function(url){623 var app = this.app627 , req = this.req628 , head = 'HEAD' == req.method629 , status = 302630 , body;624 , req = this.req;625632 // 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 }641626 // setup redirect map627 var map = { back: req.get('Referrer') || '/' };628629 // perform redirect
11 unmodified lines
641 url = path + '/' + url;642 }643 }644645 // Respond646 this.set('Location', url);647 return this;648};649650/**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 */670671res.redirect = function(url){672 var app = this.app673 , head = 'HEAD' == this.req.method674 , status = 302675 , body;676677 // 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 }686687 // Set location header688 this.location(url);689 url = this.get('Location');690691 // Support text/{plain,html} by default692 this.format({693 text: function(){694 body = statusCodes[status] + '. Redirecting to ' + encodeURI(url);
10 unmodified lines
705 });706707 // 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 lines
added:test/res.location.js+171 added
12var express = require('../')3 , request = require('./support/http');45describe('res', function(){6 describe('.location(url)', function(){7 it('should set the header', function(done){8 var app = express();910 app.use(function(req, res){11 res.location('http://google.com').end();12 });1314 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();2526 app.use(function(req, res){27 res.location('//cuteoverload.com').end();28 });2930 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();4243 app.use(function(req, res){44 res.location('/login').end();45 });4647 request(app)48 .get('/')49 .end(function(err, res){50 res.headers.should.have.property('location', '/login');51 done();52 })53 })54 })5556 describe('with leading ./', function(){57 it('should construct path-relative urls', function(done){58 var app = express();5960 app.use(function(req, res){61 res.location('./edit').end();62 });6364 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 })7273 describe('with leading ../', function(){74 it('should construct path-relative urls', function(done){75 var app = express();7677 app.use(function(req, res){78 res.location('../new').end();79 });8081 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();9394 app.use(function(req, res){95 res.location('login').end();96 });9798 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();113114 admin.use(function(req, res){115 res.location('login').end();116 });117118 app.use('/blog', blog);119 blog.use('/admin', admin);120121 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 })129130 describe('omitting leading /', function(){131 it('should respect the mount-point', function(done){132 var app = express()133 , admin = express();134135 admin.use(function(req, res){136 res.location('admin/login').end();137 });138139 app.use('/blog', admin);140141 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 })149150 describe('providing leading /', function(){151 it('should ignore mount-point', function(done){152 var app = express()153 , admin = express();154155 admin.use(function(req, res){156 res.location('/admin/login').end();157 });158159 app.use('/blog', admin);160161 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 lines
18 res.headers.should.have.property('location', 'http://google.com');19 done();20 })21 })2223 describe('with leading //', function(){24 it('should pass through scheme-relative urls', function(done){25 var app = express();2627 app.use(function(req, res){28 res.redirect('//cuteoverload.com');29 });3031 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 })4041 42 describe('with leading /', function(){43 it('should construct scheme-relative urls', function(done){44 var app = express();4546 app.use(function(req, res){47 res.redirect('/login');48 });4950 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 })5960 describe('with leading ./', function(){61 it('should construct path-relative urls', function(done){62 var app = express();6364 app.use(function(req, res){65 res.redirect('./edit');66 });6768 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 })7778 describe('with leading ../', function(){79 it('should construct path-relative urls', function(done){80 var app = express();8182 app.use(function(req, res){83 res.redirect('../new');84 });8586 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();99100 app.use(function(req, res){101 res.redirect('login');102 });103104 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();120121 admin.use(function(req, res){122 res.redirect('login');123 });124125 app.use('/blog', blog);126 blog.use('/admin', admin);127128 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 })137138 describe('omitting leading /', function(){139 it('should respect the mount-point', function(done){140 var app = express()141 , admin = express();142143 admin.use(function(req, res){144 res.redirect('admin/login');145 });146147 app.use('/blog', admin);148149 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 })158159 describe('providing leading /', function(){160 it('should ignore mount-point', function(done){161 var app = express()162 , admin = express();163164 admin.use(function(req, res){165 res.redirect('/admin/login');166 });167168 app.use('/blog', admin);169170 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 })2324 describe('.redirect(status, url)', function(){25 it('should set the response status', function(done){
147 unmodified lines