2021-09-29 13:01:40 +03:00
|
|
|
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 () {
|
2021-10-12 14:51:20 +03:00
|
|
|
it('Prioritizes the query params of the redirect', function () {
|
2021-10-12 18:25:52 +03:00
|
|
|
const manager = new DynamicRedirectManager({permanentMaxAge: 100, getSubdirectoryURL: (pathname) => {
|
|
|
|
return urlUtils.urlJoin(urlUtils.getSubdir(), pathname);
|
|
|
|
}});
|
2021-09-29 13:01:40 +03:00
|
|
|
|
|
|
|
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 () {
|
2021-10-12 18:25:52 +03:00
|
|
|
const manager = new DynamicRedirectManager({permanentMaxAge: 100, getSubdirectoryURL: (pathname) => {
|
|
|
|
return urlUtils.urlJoin(urlUtils.getSubdir(), pathname);
|
|
|
|
}});
|
2021-09-29 13:01:40 +03:00
|
|
|
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);
|
|
|
|
});
|
2021-10-12 15:32:38 +03:00
|
|
|
|
|
|
|
it('The routing works when passed an invalid regexp for the from parameter', function () {
|
2021-10-12 18:25:52 +03:00
|
|
|
const manager = new DynamicRedirectManager({permanentMaxAge: 100, getSubdirectoryURL: (pathname) => {
|
|
|
|
return urlUtils.urlJoin(urlUtils.getSubdir(), pathname);
|
|
|
|
}});
|
2021-10-12 15:32:38 +03:00
|
|
|
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);
|
|
|
|
});
|
2021-09-29 13:01:40 +03:00
|
|
|
});
|