From a6d86c85b6dc6b16649aadde1d652e5c15a67036 Mon Sep 17 00:00:00 2001 From: Naz Date: Mon, 29 Nov 2021 17:15:14 +0400 Subject: [PATCH] Added case sensitivity 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 case sensitivity cases in regexes --- .../test/DynamicRedirectManager.test.js | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/ghost/express-dynamic-redirects/test/DynamicRedirectManager.test.js b/ghost/express-dynamic-redirects/test/DynamicRedirectManager.test.js index 7553145d83..4cf8f0a5b7 100644 --- a/ghost/express-dynamic-redirects/test/DynamicRedirectManager.test.js +++ b/ghost/express-dynamic-redirects/test/DynamicRedirectManager.test.js @@ -168,5 +168,94 @@ describe('DynamicRedirectManager', function () { should.equal(location, '/?something=good'); }); }); + + describe('Case sensitivity', function () { + it('with case insensitive', function () { + const from = '/^\\/case-insensitive/i'; + const to = '/redirected-insensitive'; + + manager.addRedirect(from , to); + + req.url = '/CaSe-InSeNsItIvE'; + + 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, '/redirected-insensitive'); + }); + + it('with case sensitive', function () { + const from = '^\\/Case-Sensitive'; + const to = '/redirected-sensitive'; + + manager.addRedirect(from , to); + + req.url = '/Case-Sensitive'; + + 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, '/redirected-sensitive'); + }); + + it('defaults to case sensitive', function () { + const from = '^\\/Default-Sensitive'; + const to = '/redirected-default'; + + manager.addRedirect(from , to); + + req.url = '/Default-Sensitive'; + + manager.handleRequest(req, res, function next() { + should.fail(true, 'next should NOT have been called'); + }); + + should.equal(headers['Cache-Control'], 'public, max-age=0'); + should.equal(status, 302); + should.equal(location, '/redirected-default'); + }); + + it('should not redirect with case sensitive', function () { + const from = '^\\/Case-Sensitive'; + const to = '/redirected-insensitive'; + + manager.addRedirect(from , to); + + req.url = '/casE-sensitivE'; + + 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('should not redirect with default case sensitive', function () { + const from = '^\\/Default-Sensitive'; + const to = '/redirected-default'; + + manager.addRedirect(from , to); + + req.url = '/defaulT-sensitivE'; + + 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); + }); + }); }); });