Extracted duplicate class initialization in test suite
refs https://github.com/TryGhost/Toolbox/issues/139 - The DynamicRedirectManager was initialized witht the same set of parameters throughout the test suite, so it made sense to initialize it once for all the tests. The sibiling describe block will have a similar setup for a redirects manager that has a subdirectory configured
This commit is contained in:
parent
77e2d550c8
commit
08e2056f6c
@ -1,14 +1,9 @@
|
||||
const should = require('should');
|
||||
const DynamicRedirectManager = require('../');
|
||||
|
||||
const urlUtils = {
|
||||
getSubdir() {
|
||||
return '';
|
||||
},
|
||||
urlJoin(...parts) {
|
||||
let url = parts.join('/');
|
||||
return url.replace(/(^|[^:])\/\/+/g, '$1/');
|
||||
}
|
||||
const urlJoin = (...parts) => {
|
||||
let url = parts.join('/');
|
||||
return url.replace(/(^|[^:])\/\/+/g, '$1/');
|
||||
};
|
||||
|
||||
describe('DynamicRedirectManager', function () {
|
||||
@ -38,172 +33,140 @@ describe('DynamicRedirectManager', function () {
|
||||
};
|
||||
});
|
||||
|
||||
it('Prioritizes the query params of the redirect', function () {
|
||||
const manager = new DynamicRedirectManager({
|
||||
permanentMaxAge: 100,
|
||||
getSubdirectoryURL: (pathname) => {
|
||||
return urlUtils.urlJoin(urlUtils.getSubdir(), pathname);
|
||||
}
|
||||
});
|
||||
describe('no subdirectory configuration', function () {
|
||||
let manager;
|
||||
|
||||
manager.addRedirect('/test-params', '/result?q=abc', {
|
||||
permanent: true
|
||||
});
|
||||
|
||||
req.url = '/test-params/?q=123&lang=js';
|
||||
|
||||
manager.handleRequest(req, res, function next() {
|
||||
should.fail(true, false, 'next should NOT have been called');
|
||||
});
|
||||
|
||||
should.equal(headers['Cache-Control'], 'public, max-age=100');
|
||||
should.equal(status, 301);
|
||||
should.equal(location, '/result?q=abc&lang=js');
|
||||
});
|
||||
|
||||
it('Allows redirects to be removed', function () {
|
||||
const manager = new DynamicRedirectManager({
|
||||
permanentMaxAge: 100,
|
||||
getSubdirectoryURL: (pathname) => {
|
||||
return urlUtils.urlJoin(urlUtils.getSubdir(), pathname);
|
||||
}
|
||||
});
|
||||
const id = manager.addRedirect('/test-params', '/result?q=abc', {permanent: true});
|
||||
manager.removeRedirect(id);
|
||||
|
||||
req.url = '/test-params/?q=123&lang=js';
|
||||
|
||||
manager.handleRequest(req, res, function next() {
|
||||
should.ok(true, 'next should have been called');
|
||||
});
|
||||
|
||||
should.equal(headers, null);
|
||||
should.equal(status, null);
|
||||
should.equal(location, null);
|
||||
});
|
||||
|
||||
it('The routing works when passed an invalid regexp for the from parameter', function () {
|
||||
const manager = new DynamicRedirectManager({
|
||||
permanentMaxAge: 100,
|
||||
getSubdirectoryURL: (pathname) => {
|
||||
return urlUtils.urlJoin(urlUtils.getSubdir(), pathname);
|
||||
}
|
||||
});
|
||||
const from = '/invalid_regex/(/size/[a-zA-Z0-9_-.]*/[a-zA-Z0-9_-.]*/[0-9]*/[0-9]*/)([a-zA-Z0-9_-.]*)';
|
||||
const to = '/';
|
||||
|
||||
manager.addRedirect(from , to, {
|
||||
permanent: false
|
||||
});
|
||||
|
||||
req.url = '/test-params/';
|
||||
|
||||
manager.handleRequest(req, res, function next() {
|
||||
should.ok(true, 'next should have been called');
|
||||
});
|
||||
|
||||
should.equal(headers, null);
|
||||
should.equal(status, null);
|
||||
should.equal(location, null);
|
||||
});
|
||||
|
||||
describe('Substitution regex redirects', function () {
|
||||
it('Works with substitution redirect case and no trailing slash', function (){
|
||||
const manager = new DynamicRedirectManager({
|
||||
beforeEach(function () {
|
||||
manager = new DynamicRedirectManager({
|
||||
permanentMaxAge: 100,
|
||||
getSubdirectoryURL: (pathname) => {
|
||||
return urlUtils.urlJoin(urlUtils.getSubdir(), pathname);
|
||||
return urlJoin('', pathname);
|
||||
}
|
||||
});
|
||||
const from = '^/post/[0-9]+/([a-z0-9\\-]+)';
|
||||
const to = '/$1';
|
||||
});
|
||||
|
||||
manager.addRedirect(from , to);
|
||||
it('Prioritizes the query params of the redirect', function () {
|
||||
manager.addRedirect('/test-params', '/result?q=abc', {
|
||||
permanent: true
|
||||
});
|
||||
|
||||
req.url = '/post/10/a-nice-blog-post';
|
||||
req.url = '/test-params/?q=123&lang=js';
|
||||
|
||||
manager.handleRequest(req, res, function next() {
|
||||
should.fail(true, 'next should NOT have been called');
|
||||
should.fail(true, false, 'next should NOT have been called');
|
||||
});
|
||||
|
||||
// NOTE: max-age is "0" because it's not a permanent redirect
|
||||
should.equal(headers['Cache-Control'], 'public, max-age=0');
|
||||
should.equal(status, 302);
|
||||
should.equal(location, '/a-nice-blog-post');
|
||||
should.equal(headers['Cache-Control'], 'public, max-age=100');
|
||||
should.equal(status, 301);
|
||||
should.equal(location, '/result?q=abc&lang=js');
|
||||
});
|
||||
|
||||
it('Works with substitution redirect case and a trailing slash', function (){
|
||||
const manager = new DynamicRedirectManager({
|
||||
permanentMaxAge: 100,
|
||||
getSubdirectoryURL: (pathname) => {
|
||||
return urlUtils.urlJoin(urlUtils.getSubdir(), pathname);
|
||||
}
|
||||
});
|
||||
const from = '^/post/[0-9]+/([a-z0-9\\-]+)';
|
||||
const to = '/$1';
|
||||
it('Allows redirects to be removed', function () {
|
||||
const id = manager.addRedirect('/test-params', '/result?q=abc', {permanent: true});
|
||||
manager.removeRedirect(id);
|
||||
|
||||
manager.addRedirect(from , to);
|
||||
|
||||
req.url = '/post/10/a-nice-blog-post/';
|
||||
req.url = '/test-params/?q=123&lang=js';
|
||||
|
||||
manager.handleRequest(req, res, function next() {
|
||||
should.fail(true, 'next should NOT have been called');
|
||||
should.ok(true, 'next should have been called');
|
||||
});
|
||||
|
||||
// NOTE: max-age is "0" because it's not a permanent redirect
|
||||
should.equal(headers['Cache-Control'], 'public, max-age=0');
|
||||
should.equal(status, 302);
|
||||
should.equal(location, '/a-nice-blog-post');
|
||||
should.equal(headers, null);
|
||||
should.equal(status, null);
|
||||
should.equal(location, null);
|
||||
});
|
||||
|
||||
it('Redirects keeping the query params for substitution regexp', function (){
|
||||
const manager = new DynamicRedirectManager({
|
||||
permanentMaxAge: 100,
|
||||
getSubdirectoryURL: (pathname) => {
|
||||
return urlUtils.urlJoin(urlUtils.getSubdir(), pathname);
|
||||
}
|
||||
});
|
||||
|
||||
const from = '^/post/[0-9]+/([a-z0-9\\-]+)';
|
||||
const to = '/$1';
|
||||
|
||||
manager.addRedirect(from , to);
|
||||
|
||||
req.url = '/post/10/a-nice-blog-post?a=b';
|
||||
|
||||
manager.handleRequest(req, res, function next() {
|
||||
should.fail(true, 'next should NOT have been called');
|
||||
});
|
||||
|
||||
// NOTE: max-age is "0" because it's not a permanent redirect
|
||||
should.equal(headers['Cache-Control'], 'public, max-age=0');
|
||||
should.equal(status, 302);
|
||||
should.equal(location, '/a-nice-blog-post?a=b');
|
||||
});
|
||||
|
||||
it('Redirects keeping the query params', function (){
|
||||
const manager = new DynamicRedirectManager({
|
||||
permanentMaxAge: 100,
|
||||
getSubdirectoryURL: (pathname) => {
|
||||
return urlUtils.urlJoin(urlUtils.getSubdir(), pathname);
|
||||
}
|
||||
});
|
||||
|
||||
const from = '^\\/topic\\/';
|
||||
it('The routing works when passed an invalid regexp for the from parameter', function () {
|
||||
const from = '/invalid_regex/(/size/[a-zA-Z0-9_-.]*/[a-zA-Z0-9_-.]*/[0-9]*/[0-9]*/)([a-zA-Z0-9_-.]*)';
|
||||
const to = '/';
|
||||
|
||||
manager.addRedirect(from , to);
|
||||
|
||||
req.url = '/topic?something=good';
|
||||
|
||||
manager.handleRequest(req, res, function next() {
|
||||
should.fail(true, 'next should NOT have been called');
|
||||
manager.addRedirect(from , to, {
|
||||
permanent: false
|
||||
});
|
||||
|
||||
// NOTE: max-age is "0" because it's not a permanent redirect
|
||||
should.equal(headers['Cache-Control'], 'public, max-age=0');
|
||||
should.equal(status, 302);
|
||||
should.equal(location, '/?something=good');
|
||||
req.url = '/test-params/';
|
||||
|
||||
manager.handleRequest(req, res, function next() {
|
||||
should.ok(true, 'next should have been called');
|
||||
});
|
||||
|
||||
should.equal(headers, null);
|
||||
should.equal(status, null);
|
||||
should.equal(location, null);
|
||||
});
|
||||
|
||||
describe('Substitution regex redirects', function () {
|
||||
it('Works with substitution redirect case and no trailing slash', function (){
|
||||
const from = '^/post/[0-9]+/([a-z0-9\\-]+)';
|
||||
const to = '/$1';
|
||||
|
||||
manager.addRedirect(from , to);
|
||||
|
||||
req.url = '/post/10/a-nice-blog-post';
|
||||
|
||||
manager.handleRequest(req, res, function next() {
|
||||
should.fail(true, 'next should NOT have been called');
|
||||
});
|
||||
|
||||
// NOTE: max-age is "0" because it's not a permanent redirect
|
||||
should.equal(headers['Cache-Control'], 'public, max-age=0');
|
||||
should.equal(status, 302);
|
||||
should.equal(location, '/a-nice-blog-post');
|
||||
});
|
||||
|
||||
it('Works with substitution redirect case and a trailing slash', function (){
|
||||
const from = '^/post/[0-9]+/([a-z0-9\\-]+)';
|
||||
const to = '/$1';
|
||||
|
||||
manager.addRedirect(from , to);
|
||||
|
||||
req.url = '/post/10/a-nice-blog-post/';
|
||||
|
||||
manager.handleRequest(req, res, function next() {
|
||||
should.fail(true, 'next should NOT have been called');
|
||||
});
|
||||
|
||||
// NOTE: max-age is "0" because it's not a permanent redirect
|
||||
should.equal(headers['Cache-Control'], 'public, max-age=0');
|
||||
should.equal(status, 302);
|
||||
should.equal(location, '/a-nice-blog-post');
|
||||
});
|
||||
|
||||
it('Redirects keeping the query params for substitution regexp', function (){
|
||||
const from = '^/post/[0-9]+/([a-z0-9\\-]+)';
|
||||
const to = '/$1';
|
||||
|
||||
manager.addRedirect(from , to);
|
||||
|
||||
req.url = '/post/10/a-nice-blog-post?a=b';
|
||||
|
||||
manager.handleRequest(req, res, function next() {
|
||||
should.fail(true, 'next should NOT have been called');
|
||||
});
|
||||
|
||||
// NOTE: max-age is "0" because it's not a permanent redirect
|
||||
should.equal(headers['Cache-Control'], 'public, max-age=0');
|
||||
should.equal(status, 302);
|
||||
should.equal(location, '/a-nice-blog-post?a=b');
|
||||
});
|
||||
|
||||
it('Redirects keeping the query params', function (){
|
||||
const from = '^\\/topic\\/';
|
||||
const to = '/';
|
||||
|
||||
manager.addRedirect(from , to);
|
||||
|
||||
req.url = '/topic?something=good';
|
||||
|
||||
manager.handleRequest(req, res, function next() {
|
||||
should.fail(true, 'next should NOT have been called');
|
||||
});
|
||||
|
||||
// NOTE: max-age is "0" because it's not a permanent redirect
|
||||
should.equal(headers['Cache-Control'], 'public, max-age=0');
|
||||
should.equal(status, 302);
|
||||
should.equal(location, '/?something=good');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user