From 0daed36366cc41e3fea78770b9767929b51e8c3d Mon Sep 17 00:00:00 2001 From: Naz Date: Mon, 29 Nov 2021 17:20:45 +0400 Subject: [PATCH] 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 - https://github.com/TryGhost/Ghost/blob/fedbfb3c67ec99e7ec6085f04589e4ef9956834d/test/regression/site/redirects.test.js - This changeset covers redirects to external URLs --- .../test/DynamicRedirectManager.test.js | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/ghost/express-dynamic-redirects/test/DynamicRedirectManager.test.js b/ghost/express-dynamic-redirects/test/DynamicRedirectManager.test.js index 4cf8f0a5b7..4cd613297a 100644 --- a/ghost/express-dynamic-redirects/test/DynamicRedirectManager.test.js +++ b/ghost/express-dynamic-redirects/test/DynamicRedirectManager.test.js @@ -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'); + }); + }); }); });