From 7419ff2c4f726086b3d313fd4524b5e21d21b24e Mon Sep 17 00:00:00 2001 From: Naz Date: Mon, 9 May 2022 15:33:40 +0800 Subject: [PATCH] Fixed requestURL value passed to the APIVersionCompatibilityService refs https://github.com/TryGhost/Toolbox/issues/292 - There was a typo in the variable name - req.originalURL is NOT does not exist on express' reqest object - Added tests to avoid similar mistake again --- .../lib/mw-api-version-mismatch.js | 2 +- .../test/mw-api-version-mismatch.test.js | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/ghost/mw-api-version-mismatch/lib/mw-api-version-mismatch.js b/ghost/mw-api-version-mismatch/lib/mw-api-version-mismatch.js index 021cee1aae..4a4c5eaae4 100644 --- a/ghost/mw-api-version-mismatch/lib/mw-api-version-mismatch.js +++ b/ghost/mw-api-version-mismatch/lib/mw-api-version-mismatch.js @@ -5,7 +5,7 @@ const versionMismatchHandler = (APIVersionCompatibilityService) => { await APIVersionCompatibilityService.handleMismatch({ acceptVersion: req.headers['accept-version'], contentVersion: `v${res.locals.safeVersion}`, - requestURL: req.originalURL, + requestURL: req.originalUrl, userAgent: req.headers['user-agent'] }); } diff --git a/ghost/mw-api-version-mismatch/test/mw-api-version-mismatch.test.js b/ghost/mw-api-version-mismatch/test/mw-api-version-mismatch.test.js index 97f771e563..cfa54d4047 100644 --- a/ghost/mw-api-version-mismatch/test/mw-api-version-mismatch.test.js +++ b/ghost/mw-api-version-mismatch/test/mw-api-version-mismatch.test.js @@ -10,16 +10,27 @@ describe('mw-api-version-mismatch', function () { handleMismatch: sinon.stub().resolves() }; const req = { - headers: {} + originalUrl: '/api/admin/posts/1', + headers: { + 'accept-version': 'v3.28', + 'user-agent': 'Zapier/2.1 GhostAdminSDK/3.28' + } }; const res = { - locals: {} + locals: { + safeVersion: '4.46' + } }; versionMismatchMW(APIVersionCompatibilityService)(new errors.RequestNotAcceptableError({ code: 'UPDATE_CLIENT' }), req, res, () => { assert.equal(APIVersionCompatibilityService.handleMismatch.called, true); + assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].acceptVersion, 'v3.28'); + assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].contentVersion, 'v4.46'); + assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].requestURL, '/api/admin/posts/1'); + assert.equal(APIVersionCompatibilityService.handleMismatch.args[0][0].userAgent, 'Zapier/2.1 GhostAdminSDK/3.28'); + done(); }); });