2013-11-13 17:01:15 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-05 18:23:47 +00:00
|
|
|
const context = require('../context');
|
2013-11-13 17:01:15 +00:00
|
|
|
|
|
|
|
describe('ctx.redirect(url)', function(){
|
|
|
|
it('should redirect to the given url', function(){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
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(){
|
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');
|
|
|
|
ctx.response.header.location.should.equal('/login');
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should redirect to Referer', function(){
|
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');
|
|
|
|
ctx.response.header.location.should.equal('/login');
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should default to alt', function(){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.redirect('back', '/index.html');
|
|
|
|
ctx.response.header.location.should.equal('/index.html');
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should default redirect to /', function(){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
ctx.redirect('back');
|
|
|
|
ctx.response.header.location.should.equal('/');
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when html is accepted', function(){
|
|
|
|
it('should respond with html', function(){
|
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);
|
|
|
|
ctx.response.header['content-type'].should.equal('text/html; charset=utf-8');
|
2015-10-09 04:01:32 +00:00
|
|
|
ctx.body.should.equal(`Redirecting to <a href="${url}">${url}</a>.`);
|
2013-11-13 17:01:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should escape the url', function(){
|
2015-10-05 18:23:47 +00:00
|
|
|
const ctx = context();
|
2013-11-13 17:01:15 +00:00
|
|
|
var url = '<script>';
|
|
|
|
ctx.header.accept = 'text/html';
|
|
|
|
ctx.redirect(url);
|
|
|
|
url = escape(url);
|
|
|
|
ctx.response.header['content-type'].should.equal('text/html; charset=utf-8');
|
2015-10-09 04:01:32 +00:00
|
|
|
ctx.body.should.equal(`Redirecting to <a href="${url}">${url}</a>.`);
|
2013-11-13 17:01:15 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when text is accepted', function(){
|
|
|
|
it('should respond with text', function(){
|
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);
|
2015-10-09 04:01:32 +00:00
|
|
|
ctx.body.should.equal(`Redirecting to ${url}.`);
|
2013-11-13 17:01:15 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when status is 301', function(){
|
|
|
|
it('should not change the status code', function(){
|
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');
|
|
|
|
ctx.status.should.equal(301);
|
2015-10-09 04:01:32 +00:00
|
|
|
ctx.body.should.equal(`Redirecting to ${url}.`);
|
2013-11-13 17:01:15 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when status is 304', function(){
|
|
|
|
it('should change the status code', function(){
|
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');
|
|
|
|
ctx.status.should.equal(302);
|
2015-10-09 04:01:32 +00:00
|
|
|
ctx.body.should.equal(`Redirecting to ${url}.`);
|
2013-11-13 17:01:15 +00:00
|
|
|
})
|
|
|
|
})
|
2014-11-27 17:01:15 +00:00
|
|
|
|
|
|
|
describe('when content-type was present', function(){
|
|
|
|
it('should overwrite content-type', function() {
|
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');
|
|
|
|
ctx.status.should.equal(302);
|
2015-10-09 04:01:32 +00:00
|
|
|
ctx.body.should.equal(`Redirecting to ${url}.`);
|
2014-11-27 17:01:15 +00:00
|
|
|
ctx.type.should.equal('text/plain');
|
|
|
|
})
|
|
|
|
})
|
2013-11-13 17:01:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
function escape(html) {
|
|
|
|
return String(html)
|
|
|
|
.replace(/&/g, '&')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>');
|
2014-03-24 18:21:15 +00:00
|
|
|
}
|