Added unit test coverage for invalid redirects config

refs https://linear.app/tryghost/issue/CORE-86/fix-failing-site-instance-when-redirects-file-is-invalid
refs 260a47da83

- Refed commit was missing a unit test coverage.
- The approach here introduces a new pattern - using `supertest` in unit tests. I've found this to be the most expressive way to test an express app which receives certain middleware dynamically. Because there are very few moving parts the test is still extremely quick to run
This commit is contained in:
Naz 2021-10-04 16:44:23 +02:00
parent 260a47da83
commit 8f5186995d

View File

@ -5,6 +5,7 @@ const express = require('express');
const customRedirects = rewire('../../../../../core/server/web/shared/middlewares/custom-redirects');
const registerRoutes = customRedirects.__get__('_private.registerRoutes');
const supertest = require('supertest');
describe('UNIT: custom redirects', function () {
let res;
@ -17,7 +18,8 @@ describe('UNIT: custom redirects', function () {
};
res = {
redirect: sinon.spy(),
set: sinon.spy()
set: sinon.spy(),
writeHead: sinon.spy()
};
next = sinon.spy();
@ -46,4 +48,22 @@ describe('UNIT: custom redirects', function () {
'Cache-Control': `public, max-age=0`
});
});
it('the parent app functions even when the middleware gets an invalid redirects configuration', function (done) {
const redirectsConfig = [{
permanent: true,
from: '/invalid_regex/(/size/[a-zA-Z0-9_-.]*/[a-zA-Z0-9_-.]*/[0-9]*/[0-9]*/)([a-zA-Z0-9_-.]*)',
to: '/'
}];
const redirectsService = customRedirects.__get__('redirectsService');
sinon.stub(redirectsService, 'loadRedirectsFile').returns(redirectsConfig);
const app = express('test');
customRedirects.use(app);
app.get('/', (_req, _res) => _res.status(200).end());
supertest(app)
.get('/')
.expect(200, done);
});
});