ctx.redirect(): only set status code if not already a valid redirect status code

specifically 3xx codes except for 304. closes #66
This commit is contained in:
Jonathan Ong 2013-11-08 16:25:03 -08:00
parent 22c035bbea
commit ee6dce83af
2 changed files with 25 additions and 1 deletions

View file

@ -788,7 +788,7 @@ Context.prototype = {
redirect: function(url, alt){
if ('back' == url) url = this.get('Referrer') || alt || '/';
this.set('Location', url);
this.status = 302;
if (!~[300, 301, 302, 303, 305, 307].indexOf(this.status)) this.status = 302;
// html
if (this.accepts('html')) {

View file

@ -849,6 +849,30 @@ describe('ctx.redirect(url)', function(){
ctx.body.should.equal('Redirecting to ' + url + '.');
})
})
describe('when status is 301', function(){
it('should not change the status code', function(){
var ctx = context();
var url = 'http://google.com';
ctx.status = 301;
ctx.header.accept = 'text/plain';
ctx.redirect('http://google.com');
ctx.status.should.equal(301);
ctx.body.should.equal('Redirecting to ' + url + '.');
})
})
describe('when status is 304', function(){
it('should change the status code', function(){
var ctx = context();
var url = 'http://google.com';
ctx.status = 304;
ctx.header.accept = 'text/plain';
ctx.redirect('http://google.com');
ctx.status.should.equal(302);
ctx.body.should.equal('Redirecting to ' + url + '.');
})
})
})
function escape(html) {