diff --git a/core/server/services/redirects/index.js b/core/server/services/redirects/index.js index 1387f0d54d..126a1681b3 100644 --- a/core/server/services/redirects/index.js +++ b/core/server/services/redirects/index.js @@ -8,16 +8,20 @@ const redirectManager = new DynamicRedirectManager({ permanentMaxAge: config.get('caching:customRedirects:maxAge') }, urlUtils); -const customRedirectsAPI = new CustomRedirectsAPI({ - basePath: config.getContentPath('data') -}, redirectManager); +let customRedirectsAPI; module.exports = { init() { + customRedirectsAPI = new CustomRedirectsAPI({ + basePath: config.getContentPath('data') + }, redirectManager); + return customRedirectsAPI.init(); }, - api: customRedirectsAPI, + get api() { + return customRedirectsAPI; + }, middleware: redirectManager.handleRequest }; diff --git a/test/regression/api/canary/admin/redirects.test.js b/test/regression/api/canary/admin/redirects.test.js index 6040cc1172..69da00d539 100644 --- a/test/regression/api/canary/admin/redirects.test.js +++ b/test/regression/api/canary/admin/redirects.test.js @@ -42,11 +42,17 @@ describe('Redirects API', function () { configUtils.config.set('paths:contentPath', originalContentPath); }); - it('file does not exist', function () { + it('file does not exist', async function () { // Just set any content folder, which does not contain a redirects file. - configUtils.set('paths:contentPath', path.join(__dirname, '../../../utils/fixtures/data')); + // configUtils.set('paths:contentPath', path.join(__dirname, '../../../utils/fixtures/data')); - return request + await startGhost({ + redirectsFile: true, + redirectsFileExt: null, + forceStart: true + }); + + await request .get(localUtils.API.getApiQuery('redirects/download/')) .set('Origin', config.get('url')) .expect(200) @@ -59,8 +65,14 @@ describe('Redirects API', function () { }); }); - it('file exists', function () { - return request + it('file exists', async function () { + await startGhost({ + redirectsFile: true, + redirectsFileExt: '.json', + forceStart: true + }); + + await request .get(localUtils.API.getApiQuery('redirects/download/')) .set('Origin', config.get('url')) .expect('Content-Type', /application\/json/) @@ -76,19 +88,25 @@ describe('Redirects API', function () { }); describe('Download yaml', function () { - beforeEach(function () { - testUtils.setupRedirectsFile(config.get('paths:contentPath'), '.yaml'); - }); + // beforeEach(function () { + // testUtils.setupRedirectsFile(config.get('paths:contentPath'), '.yaml'); + // }); - afterEach(function () { - testUtils.setupRedirectsFile(config.get('paths:contentPath'), '.json'); - }); + // afterEach(function () { + // testUtils.setupRedirectsFile(config.get('paths:contentPath'), '.json'); + // }); // 'file does not exist' doesn't have to be tested because it always returns .json file. // TODO: But it should be written when the default redirects file type is changed to yaml. - it('file exists', function () { - return request + it('file exists', async function () { + await startGhost({ + redirectsFile: true, + redirectsFileExt: '.yaml', + forceStart: true + }); + + await request .get(localUtils.API.getApiQuery('redirects/download/')) .set('Origin', config.get('url')) .expect('Content-Type', /text\/html/) @@ -143,28 +161,25 @@ describe('Redirects API', function () { }); describe('Ensure re-registering redirects works', function () { - it('no redirects file exists', function () { - return startGhost({redirectsFile: false, forceStart: true}) - .then(() => { - return request - .get('/my-old-blog-post/') - .expect(404); - }) - .then(() => { - // Provide a redirects file in the root directory of the content test folder - fs.writeFileSync(path.join(config.get('paths:contentPath'), 'redirects-init.json'), JSON.stringify([{ - from: 'k', - to: 'l' - }])); - }) - .then(() => { - return request - .post(localUtils.API.getApiQuery('redirects/upload/')) - .set('Origin', config.get('url')) - .attach('redirects', path.join(config.get('paths:contentPath'), 'redirects-init.json')) - .expect('Content-Type', /application\/json/) - .expect(200); - }) + it('no redirects file exists', async function () { + await startGhost({ + redirectsFile: true, + redirectsFileExt: null, + forceStart: true + }); + + // Provide a redirects file in the root directory of the content test folder + await fs.writeFileSync(path.join(config.get('paths:contentPath'), 'redirects-init.json'), JSON.stringify([{ + from: 'k', + to: 'l' + }])); + + await request + .post(localUtils.API.getApiQuery('redirects/upload/')) + .set('Origin', config.get('url')) + .attach('redirects', path.join(config.get('paths:contentPath'), 'redirects-init.json')) + .expect('Content-Type', /application\/json/) + .expect(200) .then((res) => { res.headers['x-cache-invalidate'].should.eql('/*');