2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-05-11 03:30:32 +00:00
|
|
|
const assert = require('assert');
|
2019-09-28 04:49:57 +00:00
|
|
|
const request = require('supertest');
|
2015-10-12 04:59:30 +00:00
|
|
|
const context = require('../helpers/context');
|
2019-09-28 04:49:57 +00:00
|
|
|
const Koa = require('../..');
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('ctx.redirect(url)', () => {
|
|
|
|
it('should redirect to the given url', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.redirect('http://google.com');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.response.header.location, 'http://google.com');
|
|
|
|
assert.equal(ctx.status, 302);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2019-09-28 04:49:57 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('with "back"', () => {
|
|
|
|
it('should redirect to Referrer', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.req.headers.referrer = '/login';
|
|
|
|
ctx.redirect('back');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.response.header.location, '/login');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should redirect to Referer', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.req.headers.referer = '/login';
|
|
|
|
ctx.redirect('back');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.response.header.location, '/login');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should default to alt', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.redirect('back', '/index.html');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.response.header.location, '/index.html');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should default redirect to /', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.redirect('back');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.response.header.location, '/');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when html is accepted', () => {
|
|
|
|
it('should respond with html', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
|
|
|
const url = 'http://google.com';
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.header.accept = 'text/html';
|
|
|
|
ctx.redirect(url);
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.response.header['content-type'], 'text/html; charset=utf-8');
|
|
|
|
assert.equal(ctx.body, `Redirecting to <a href="${url}">${url}</a>.`);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
it('should escape the url', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2015-10-22 22:46:47 +00:00
|
|
|
let url = '<script>';
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.header.accept = 'text/html';
|
|
|
|
ctx.redirect(url);
|
|
|
|
url = escape(url);
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.response.header['content-type'], 'text/html; charset=utf-8');
|
|
|
|
assert.equal(ctx.body, `Redirecting to <a href="${url}">${url}</a>.`);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when text is accepted', () => {
|
|
|
|
it('should respond with text', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
|
|
|
const url = 'http://google.com';
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.header.accept = 'text/plain';
|
|
|
|
ctx.redirect(url);
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.body, `Redirecting to ${url}.`);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when status is 301', () => {
|
|
|
|
it('should not change the status code', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
|
|
|
const url = 'http://google.com';
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.status = 301;
|
|
|
|
ctx.header.accept = 'text/plain';
|
|
|
|
ctx.redirect('http://google.com');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.status, 301);
|
|
|
|
assert.equal(ctx.body, `Redirecting to ${url}.`);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when status is 304', () => {
|
|
|
|
it('should change the status code', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
|
|
|
const url = 'http://google.com';
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.status = 304;
|
|
|
|
ctx.header.accept = 'text/plain';
|
|
|
|
ctx.redirect('http://google.com');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.status, 302);
|
|
|
|
assert.equal(ctx.body, `Redirecting to ${url}.`);
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
2014-11-27 17:01:15 +00:00
|
|
|
|
2015-10-25 07:54:57 +00:00
|
|
|
describe('when content-type was present', () => {
|
|
|
|
it('should overwrite content-type', () => {
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2014-11-27 17:01:15 +00:00
|
|
|
ctx.body = {};
|
2015-10-05 18:23:47 +00:00
|
|
|
const url = 'http://google.com';
|
2014-11-27 17:01:15 +00:00
|
|
|
ctx.header.accept = 'text/plain';
|
|
|
|
ctx.redirect('http://google.com');
|
2017-05-11 03:30:32 +00:00
|
|
|
assert.equal(ctx.status, 302);
|
|
|
|
assert.equal(ctx.body, `Redirecting to ${url}.`);
|
|
|
|
assert.equal(ctx.type, 'text/plain');
|
2015-10-12 20:36:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-12 20:36:41 +00:00
|
|
|
function escape(html){
|
2013-11-13 17:01:15 +00:00
|
|
|
return String(html)
|
|
|
|
.replace(/&/g, '&')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>');
|
2014-03-24 18:21:15 +00:00
|
|
|
}
|