Added external URL coverage to DynamiRedirectsManager suites

refs https://github.com/TryGhost/Toolbox/issues/139

- These unit tests come directly from equivalent regression tests in Ghost repository - fedbfb3c67/test/regression/site/redirects.test.js
- This changeset covers redirects to external URLs
This commit is contained in:
Naz 2021-11-29 17:20:45 +04:00
parent a6d86c85b6
commit 0daed36366

View File

@ -257,5 +257,61 @@ describe('DynamicRedirectManager', function () {
should.equal(location, null);
});
});
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');
});
});
});
});