Ghost/ghost/express-dynamic-redirects/test/DynamicRedirectManager.test.js
Fabien 'egg' O'Carroll 8c92f5744c Added express-dynamic-redirects module (#337)
refs https://github.com/TryGhost/Team/issues/1091

The Offers feature needs to be able to add and remove redirects to Ghost
- which is very similar to the custom redirects functionality. Here we've
pulled out the core of the dynamic redirect part of custom redirects so
that it can be used by both features and have code shared between them.
2021-09-29 12:01:40 +02:00

80 lines
2.2 KiB
JavaScript

const should = require('should');
const DynamicRedirectManager = require('../');
const urlUtils = {
getSubdir() {
return '';
},
urlJoin(...parts) {
let url = parts.join('/');
return url.replace(/(^|[^:])\/\/+/g, '$1/');
}
};
describe('DynamicRedirectManager', function () {
it('Prioritises the query params of the redirect', function () {
const manager = new DynamicRedirectManager({permanentMaxAge: 100}, urlUtils);
manager.addRedirect('/test-params', '/result?q=abc', {permanent: true});
const req = {
method: 'GET',
url: '/test-params/?q=123&lang=js'
};
let headers = null;
let status = null;
let location = null;
const res = {
set(_headers) {
headers = _headers;
},
redirect(_status, _location) {
status = _status;
location = _location;
}
};
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}, urlUtils);
const id = manager.addRedirect('/test-params', '/result?q=abc', {permanent: true});
manager.removeRedirect(id);
const req = {
method: 'GET',
url: '/test-params/?q=123&lang=js'
};
let headers = null;
let status = null;
let location = null;
const res = {
set(_headers) {
headers = _headers;
},
redirect(_status, _location) {
status = _status;
location = _location;
}
};
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);
});
});