mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-19 00:11:49 +03:00
ba2a5df493
refs https://linear.app/tryghost/issue/CORE-84/have-a-look-at-the-eggs-redirects-refactor-branch refs8f5186995d
refs260a47da83
- The router should not stop working when an invalid redirect definition is added - Referenced commits solve this exact problem before this module was introduced
117 lines
3.3 KiB
JavaScript
117 lines
3.3 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('Prioritizes 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);
|
|
});
|
|
|
|
it('The routing works when passed an invalid regexp for the from parameter', function () {
|
|
const manager = new DynamicRedirectManager({permanentMaxAge: 100, urlUtils});
|
|
|
|
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
|
|
});
|
|
|
|
const req = {
|
|
method: 'GET',
|
|
url: '/test-params/'
|
|
};
|
|
|
|
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);
|
|
});
|
|
});
|