diff --git a/lib/context.js b/lib/context.js index aa3c0ee..ebdc9b5 100644 --- a/lib/context.js +++ b/lib/context.js @@ -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')) { diff --git a/test/context.js b/test/context.js index 1713918..1b1f499 100644 --- a/test/context.js +++ b/test/context.js @@ -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) {