From 8247242610ff493fcf86e8377a988e6e21465471 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Fri, 12 Aug 2022 08:34:20 +0200 Subject: [PATCH] Added tests to HTTP module of api-framework - this file was mostly just missing tests for the other content disposition types, which are easily added - bumps coverage of this file to 100% --- ghost/api-framework/test/headers.test.js | 67 ++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/ghost/api-framework/test/headers.test.js b/ghost/api-framework/test/headers.test.js index 67336932b4..047e92cf7e 100644 --- a/ghost/api-framework/test/headers.test.js +++ b/ghost/api-framework/test/headers.test.js @@ -29,6 +29,46 @@ describe('Headers', function () { }); }); + it('csv with function', async function () { + const result = await shared.headers.get({}, { + disposition: { + type: 'csv', + value() { + // pretend we're doing some dynamic filename logic in this function + const filename = `awesome-data-2022-08-01.csv`; + return filename; + } + } + }); + result.should.eql({ + 'Content-Disposition': 'Attachment; filename="awesome-data-2022-08-01.csv"', + 'Content-Type': 'text/csv' + }); + }); + + it('file', async function () { + const result = await shared.headers.get({}, {disposition: {type: 'file', value: 'my.txt'}}); + result.should.eql({ + 'Content-Disposition': 'Attachment; filename="my.txt"' + }); + }); + + it('file with function', async function () { + const result = await shared.headers.get({}, { + disposition: { + type: 'file', + value() { + // pretend we're doing some dynamic filename logic in this function + const filename = `awesome-data-2022-08-01.txt`; + return filename; + } + } + }); + result.should.eql({ + 'Content-Disposition': 'Attachment; filename="awesome-data-2022-08-01.txt"' + }); + }); + it('yaml', function () { return shared.headers.get('yaml file', {disposition: {type: 'yaml', value: 'my.yaml'}}) .then((result) => { @@ -90,6 +130,33 @@ describe('Headers', function () { }); }); + it('respects HTTP redirects', async function () { + const apiResult = { + posts: [{ + id: 'id_value' + }] + }; + + const apiConfigHeaders = {}; + const frame = { + docName: 'posts', + method: 'add', + original: { + url: { + host: 'example.com', + pathname: `/api/content/posts/`, + secure: false + } + } + }; + + const result = await shared.headers.get(apiResult, apiConfigHeaders, frame); + result.should.eql({ + // NOTE: the backslash in the end is important to avoid unecessary 301s using the header + Location: 'http://example.com/api/content/posts/id_value/' + }); + }); + it('adds and resolves header to correct url when pathname does not contain backslash in the end', function () { const apiResult = { posts: [{