koa-lite/test/response/redirect.js

118 lines
3.5 KiB
JavaScript
Raw Normal View History

'use strict';
const context = require('../helpers/context');
describe('ctx.redirect(url)', function(){
it('should redirect to the given url', function(){
const ctx = context();
ctx.redirect('http://google.com');
ctx.response.header.location.should.equal('http://google.com');
ctx.status.should.equal(302);
2015-10-12 20:36:41 +00:00
});
describe('with "back"', function(){
it('should redirect to Referrer', function(){
const ctx = context();
ctx.req.headers.referrer = '/login';
ctx.redirect('back');
ctx.response.header.location.should.equal('/login');
2015-10-12 20:36:41 +00:00
});
it('should redirect to Referer', function(){
const ctx = context();
ctx.req.headers.referer = '/login';
ctx.redirect('back');
ctx.response.header.location.should.equal('/login');
2015-10-12 20:36:41 +00:00
});
it('should default to alt', function(){
const ctx = context();
ctx.redirect('back', '/index.html');
ctx.response.header.location.should.equal('/index.html');
2015-10-12 20:36:41 +00:00
});
it('should default redirect to /', function(){
const ctx = context();
ctx.redirect('back');
ctx.response.header.location.should.equal('/');
2015-10-12 20:36:41 +00:00
});
});
describe('when html is accepted', function(){
it('should respond with html', function(){
const ctx = context();
const 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 <a href="${url}">${url}</a>.`);
2015-10-12 20:36:41 +00:00
});
it('should escape the url', function(){
const ctx = context();
2015-10-22 22:46:47 +00:00
let 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');
ctx.body.should.equal(`Redirecting to <a href="${url}">${url}</a>.`);
2015-10-12 20:36:41 +00:00
});
});
describe('when text is accepted', function(){
it('should respond with text', function(){
const ctx = context();
const url = 'http://google.com';
ctx.header.accept = 'text/plain';
ctx.redirect(url);
ctx.body.should.equal(`Redirecting to ${url}.`);
2015-10-12 20:36:41 +00:00
});
});
describe('when status is 301', function(){
it('should not change the status code', function(){
const ctx = context();
const 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}.`);
2015-10-12 20:36:41 +00:00
});
});
describe('when status is 304', function(){
it('should change the status code', function(){
const ctx = context();
const 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}.`);
2015-10-12 20:36:41 +00:00
});
});
describe('when content-type was present', function(){
2015-10-12 20:36:41 +00:00
it('should overwrite content-type', function(){
const ctx = context();
ctx.body = {};
const url = 'http://google.com';
ctx.header.accept = 'text/plain';
ctx.redirect('http://google.com');
ctx.status.should.equal(302);
ctx.body.should.equal(`Redirecting to ${url}.`);
ctx.type.should.equal('text/plain');
2015-10-12 20:36:41 +00:00
});
});
});
2015-10-12 20:36:41 +00:00
function escape(html){
return String(html)
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
2014-03-24 18:21:15 +00:00
}