2021-09-29 13:01:40 +03:00
|
|
|
|
const should = require('should');
|
|
|
|
|
const DynamicRedirectManager = require('../');
|
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
const urlJoin = (...parts) => {
|
|
|
|
|
let url = parts.join('/');
|
|
|
|
|
return url.replace(/(^|[^:])\/\/+/g, '$1/');
|
2021-09-29 13:01:40 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
describe('DynamicRedirectManager', function () {
|
2021-11-29 15:23:52 +03:00
|
|
|
|
let headers;
|
|
|
|
|
let status;
|
|
|
|
|
let location;
|
|
|
|
|
let req;
|
|
|
|
|
let res;
|
|
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
|
headers = null;
|
|
|
|
|
status = null;
|
|
|
|
|
location = null;
|
|
|
|
|
|
|
|
|
|
req = {
|
|
|
|
|
method: 'GET'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
res = {
|
|
|
|
|
set(_headers) {
|
|
|
|
|
headers = _headers;
|
|
|
|
|
},
|
|
|
|
|
redirect(_status, _location) {
|
|
|
|
|
status = _status;
|
|
|
|
|
location = _location;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
describe('no subdirectory configuration', function () {
|
|
|
|
|
let manager;
|
2021-09-29 13:01:40 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
beforeEach(function () {
|
|
|
|
|
manager = new DynamicRedirectManager({
|
|
|
|
|
permanentMaxAge: 100,
|
|
|
|
|
getSubdirectoryURL: (pathname) => {
|
|
|
|
|
return urlJoin('', pathname);
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-09-29 13:01:40 +03:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
it('Prioritizes the query params of the redirect', function () {
|
|
|
|
|
manager.addRedirect('/test-params', '/result?q=abc', {
|
|
|
|
|
permanent: true
|
|
|
|
|
});
|
2021-09-29 13:01:40 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
req.url = '/test-params/?q=123&lang=js';
|
2021-09-29 13:01:40 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
manager.handleRequest(req, res, function next() {
|
|
|
|
|
should.fail(true, false, 'next should NOT have been called');
|
|
|
|
|
});
|
2021-09-29 13:01:40 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
should.equal(headers['Cache-Control'], 'public, max-age=100');
|
|
|
|
|
should.equal(status, 301);
|
|
|
|
|
should.equal(location, '/result?q=abc&lang=js');
|
2021-09-29 13:01:40 +03:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
it('Allows redirects to be removed', function () {
|
|
|
|
|
const id = manager.addRedirect('/test-params', '/result?q=abc', {permanent: true});
|
|
|
|
|
manager.removeRedirect(id);
|
2021-10-12 15:32:38 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
req.url = '/test-params/?q=123&lang=js';
|
2021-10-12 15:32:38 +03:00
|
|
|
|
|
2022-03-24 11:54:19 +03:00
|
|
|
|
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('Can add same redirect multiple times and remove it once', function () {
|
|
|
|
|
manager.addRedirect('/test-params', '/result?q=abc', {permanent: true});
|
|
|
|
|
const id = manager.addRedirect('/test-params', '/result?q=abc', {permanent: true});
|
|
|
|
|
manager.removeRedirect(id);
|
|
|
|
|
|
|
|
|
|
req.url = '/test-params/?q=123&lang=js';
|
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
manager.handleRequest(req, res, function next() {
|
|
|
|
|
should.ok(true, 'next should have been called');
|
|
|
|
|
});
|
2021-10-12 15:32:38 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
should.equal(headers, null);
|
|
|
|
|
should.equal(status, null);
|
|
|
|
|
should.equal(location, null);
|
2021-10-12 15:32:38 +03:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
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 = '/';
|
2021-11-29 15:13:29 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
manager.addRedirect(from , to, {
|
|
|
|
|
permanent: false
|
2021-11-29 15:13:29 +03:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
req.url = '/test-params/';
|
2021-11-29 15:13:29 +03:00
|
|
|
|
|
|
|
|
|
manager.handleRequest(req, res, function next() {
|
2021-11-29 15:31:40 +03:00
|
|
|
|
should.ok(true, 'next should have been called');
|
2021-11-29 15:13:29 +03:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
should.equal(headers, null);
|
|
|
|
|
should.equal(status, null);
|
|
|
|
|
should.equal(location, null);
|
2021-11-29 15:13:29 +03:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-29 16:43:39 +03:00
|
|
|
|
it('Throws an error if unexpected internal component throws unknown error', function () {
|
|
|
|
|
// override internal behavior to throw an unknown error
|
|
|
|
|
manager.setupRedirect = () => {
|
|
|
|
|
throw new Error('Unknown error');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const from = '/match-me';
|
|
|
|
|
const to = '/redirect-fails';
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
manager.addRedirect(from , to);
|
|
|
|
|
should.fail(false, 'Should have thrown an error');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
e.message.should.equal('Unknown error');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('removes all redirects', function () {
|
|
|
|
|
const from = '/redirect-me';
|
|
|
|
|
const to = '/redirected';
|
|
|
|
|
|
|
|
|
|
manager.addRedirect(from , to);
|
|
|
|
|
|
|
|
|
|
req.url = '/redirect-me';
|
|
|
|
|
|
|
|
|
|
manager.removeAllRedirects();
|
|
|
|
|
manager.redirectIds.should.be.empty();
|
|
|
|
|
manager.redirects.should.be.empty();
|
|
|
|
|
|
|
|
|
|
manager.handleRequest(req, res, function next() {
|
|
|
|
|
should.ok(true, 'next should have been called');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
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';
|
2021-11-29 15:13:29 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
manager.addRedirect(from , to);
|
2021-11-29 15:13:29 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
req.url = '/post/10/a-nice-blog-post';
|
2021-11-29 15:13:29 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
manager.handleRequest(req, res, function next() {
|
|
|
|
|
should.fail(true, 'next should NOT have been called');
|
|
|
|
|
});
|
2021-11-29 15:13:29 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
// 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');
|
2021-11-29 15:13:29 +03:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
it('Works with substitution redirect case and a trailing slash', function (){
|
|
|
|
|
const from = '^/post/[0-9]+/([a-z0-9\\-]+)';
|
|
|
|
|
const to = '/$1';
|
2021-11-29 15:13:29 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
manager.addRedirect(from , to);
|
2021-11-29 15:13:29 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
req.url = '/post/10/a-nice-blog-post/';
|
2021-11-29 15:13:29 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
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');
|
2021-11-29 15:13:29 +03:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
it('Redirects keeping the query params for substitution regexp', function (){
|
|
|
|
|
const from = '^/post/[0-9]+/([a-z0-9\\-]+)';
|
|
|
|
|
const to = '/$1';
|
2021-11-29 15:13:29 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
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');
|
2021-11-29 15:13:29 +03:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
it('Redirects keeping the query params', function (){
|
|
|
|
|
const from = '^\\/topic\\/';
|
|
|
|
|
const to = '/';
|
2021-11-29 15:13:29 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
manager.addRedirect(from , to);
|
2021-11-29 15:13:29 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
req.url = '/topic?something=good';
|
2021-11-29 15:13:29 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
manager.handleRequest(req, res, function next() {
|
|
|
|
|
should.fail(true, 'next should NOT have been called');
|
|
|
|
|
});
|
2021-11-29 15:13:29 +03:00
|
|
|
|
|
2021-11-29 15:31:40 +03:00
|
|
|
|
// 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');
|
|
|
|
|
});
|
2021-11-29 15:13:29 +03:00
|
|
|
|
});
|
2021-11-29 16:15:14 +03:00
|
|
|
|
|
|
|
|
|
describe('Case sensitivity', function () {
|
|
|
|
|
it('with case insensitive', function () {
|
|
|
|
|
const from = '/^\\/case-insensitive/i';
|
|
|
|
|
const to = '/redirected-insensitive';
|
|
|
|
|
|
|
|
|
|
manager.addRedirect(from , to);
|
|
|
|
|
|
|
|
|
|
req.url = '/CaSe-InSeNsItIvE';
|
|
|
|
|
|
|
|
|
|
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, '/redirected-insensitive');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('with case sensitive', function () {
|
|
|
|
|
const from = '^\\/Case-Sensitive';
|
|
|
|
|
const to = '/redirected-sensitive';
|
|
|
|
|
|
|
|
|
|
manager.addRedirect(from , to);
|
|
|
|
|
|
|
|
|
|
req.url = '/Case-Sensitive';
|
|
|
|
|
|
|
|
|
|
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, '/redirected-sensitive');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('defaults to case sensitive', function () {
|
|
|
|
|
const from = '^\\/Default-Sensitive';
|
|
|
|
|
const to = '/redirected-default';
|
|
|
|
|
|
|
|
|
|
manager.addRedirect(from , to);
|
|
|
|
|
|
|
|
|
|
req.url = '/Default-Sensitive';
|
|
|
|
|
|
|
|
|
|
manager.handleRequest(req, res, function next() {
|
|
|
|
|
should.fail(true, 'next should NOT have been called');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
should.equal(headers['Cache-Control'], 'public, max-age=0');
|
|
|
|
|
should.equal(status, 302);
|
|
|
|
|
should.equal(location, '/redirected-default');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not redirect with case sensitive', function () {
|
|
|
|
|
const from = '^\\/Case-Sensitive';
|
|
|
|
|
const to = '/redirected-insensitive';
|
|
|
|
|
|
|
|
|
|
manager.addRedirect(from , to);
|
|
|
|
|
|
|
|
|
|
req.url = '/casE-sensitivE';
|
|
|
|
|
|
|
|
|
|
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('should not redirect with default case sensitive', function () {
|
|
|
|
|
const from = '^\\/Default-Sensitive';
|
|
|
|
|
const to = '/redirected-default';
|
|
|
|
|
|
|
|
|
|
manager.addRedirect(from , to);
|
|
|
|
|
|
|
|
|
|
req.url = '/defaulT-sensitivE';
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-11-29 16:20:45 +03:00
|
|
|
|
|
|
|
|
|
describe('External url redirect', function () {
|
|
|
|
|
it('with trailing slash', function () {
|
|
|
|
|
const from = '/external-url';
|
|
|
|
|
const to = 'https://ghost.org';
|
|
|
|
|
|
|
|
|
|
manager.addRedirect(from , to);
|
|
|
|
|
|
|
|
|
|
req.url = '/external-url/';
|
|
|
|
|
|
|
|
|
|
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, 'https://ghost.org/');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('without trailing slash', function () {
|
|
|
|
|
const from = '/external-url';
|
|
|
|
|
const to = 'https://ghost.org';
|
|
|
|
|
|
|
|
|
|
manager.addRedirect(from , to);
|
|
|
|
|
|
|
|
|
|
req.url = '/external-url';
|
|
|
|
|
|
|
|
|
|
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, 'https://ghost.org/');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('with capturing group', function () {
|
|
|
|
|
const from = '/external-url/(.*)';
|
|
|
|
|
const to = 'https://ghost.org/$1';
|
|
|
|
|
|
|
|
|
|
manager.addRedirect(from , to);
|
|
|
|
|
|
|
|
|
|
req.url = '/external-url/docs';
|
|
|
|
|
|
|
|
|
|
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, 'https://ghost.org/docs');
|
|
|
|
|
});
|
|
|
|
|
});
|
2022-10-13 13:41:20 +03:00
|
|
|
|
|
|
|
|
|
describe('Url with special character redirect', function () {
|
|
|
|
|
it('redirects urls with special characters', function () {
|
|
|
|
|
const from = '/joloonii-surgaltuud/а-анлал/';
|
|
|
|
|
const to = '/joloonii-angilal/а-ангилал';
|
|
|
|
|
|
|
|
|
|
manager.addRedirect(from, to);
|
|
|
|
|
|
|
|
|
|
req.url = from;
|
|
|
|
|
|
|
|
|
|
manager.handleRequest(req, res, function next() {
|
|
|
|
|
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, '/joloonii-angilal/а-ангилал');
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-11-29 15:13:29 +03:00
|
|
|
|
});
|
2021-11-29 16:30:53 +03:00
|
|
|
|
|
|
|
|
|
describe('with subdirectory configuration', function () {
|
|
|
|
|
let manager;
|
|
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
|
manager = new DynamicRedirectManager({
|
|
|
|
|
permanentMaxAge: 100,
|
|
|
|
|
getSubdirectoryURL: (pathname) => {
|
|
|
|
|
return urlJoin('', pathname);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should include the subdirectory', function () {
|
|
|
|
|
const from = '/my-old-blog-post/';
|
|
|
|
|
const to = '/revamped-url/';
|
|
|
|
|
|
|
|
|
|
manager.addRedirect(from , to, {permanent: true});
|
|
|
|
|
|
|
|
|
|
req.url = '/blog/my-old-blog-post/';
|
|
|
|
|
|
|
|
|
|
manager.handleRequest(req, res, function next() {
|
|
|
|
|
should.fail(true, 'next should NOT have been called');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
should.equal(headers['Cache-Control'], 'public, max-age=100');
|
|
|
|
|
should.equal(status, 301);
|
|
|
|
|
should.equal(location, '/blog/revamped-url/');
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-09-29 13:01:40 +03:00
|
|
|
|
});
|