'use strict'; const assert = require('assert'); const request = require('supertest'); const context = require('../helpers/context'); const Koa = require('../..'); describe('ctx.redirect(url)', () => { it('should redirect to the given url', () => { const ctx = context(); ctx.redirect('http://google.com'); assert.equal(ctx.response.header.location, 'http://google.com'); assert.equal(ctx.status, 302); }); it('should auto fix not encode url', done => { const app = new Koa(); app.use(ctx => { ctx.redirect('http://google.com/😓'); }); request(app.callback()) .get('/') .end((err, res) => { if (err) return done(err); assert.equal(res.status, 302); assert.equal(res.headers.location, 'http://google.com/%F0%9F%98%93'); done(); }); }); describe('with "back"', () => { it('should redirect to Referrer', () => { const ctx = context(); ctx.req.headers.referrer = '/login'; ctx.redirect('back'); assert.equal(ctx.response.header.location, '/login'); }); it('should redirect to Referer', () => { const ctx = context(); ctx.req.headers.referer = '/login'; ctx.redirect('back'); assert.equal(ctx.response.header.location, '/login'); }); it('should default to alt', () => { const ctx = context(); ctx.redirect('back', '/index.html'); assert.equal(ctx.response.header.location, '/index.html'); }); it('should default redirect to /', () => { const ctx = context(); ctx.redirect('back'); assert.equal(ctx.response.header.location, '/'); }); }); describe('when html is accepted', () => { it('should respond with html', () => { const ctx = context(); const url = 'http://google.com'; ctx.header.accept = 'text/html'; ctx.redirect(url); assert.equal(ctx.response.header['content-type'], 'text/html; charset=utf-8'); assert.equal(ctx.body, `Redirecting to ${url}.`); }); it('should escape the url', () => { const ctx = context(); let url = '