var context = require('../context'); describe('ctx.redirect(url)', function(){ it('should redirect to the given url', function(){ var ctx = context(); ctx.redirect('http://google.com'); ctx.response.header.location.should.equal('http://google.com'); ctx.status.should.equal(302); }) describe('with "back"', function(){ it('should redirect to Referrer', function(){ var ctx = context(); ctx.req.headers.referrer = '/login'; ctx.redirect('back'); ctx.response.header.location.should.equal('/login'); }) it('should redirect to Referer', function(){ var ctx = context(); ctx.req.headers.referer = '/login'; ctx.redirect('back'); ctx.response.header.location.should.equal('/login'); }) it('should default to alt', function(){ var ctx = context(); ctx.redirect('back', '/index.html'); ctx.response.header.location.should.equal('/index.html'); }) it('should default redirect to /', function(){ var ctx = context(); ctx.redirect('back'); ctx.response.header.location.should.equal('/'); }) }) describe('when html is accepted', function(){ it('should respond with html', function(){ var ctx = context(); var url = 'http://google.com'; ctx.header.accept = 'text/html'; ctx.redirect(url); ctx.response.header['content-type'].should.equal('text/html; charset=utf-8'); ctx.body.should.equal('Redirecting to ' + url + '.'); }) it('should escape the url', function(){ var ctx = context(); var url = '