From 9899f8d4e7b0268fba6a7a94ffc138252b7fb97a Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Fri, 18 Dec 2015 10:15:24 +0100 Subject: [PATCH] Fix non-idempotent Ghost API helper - Add test - Don't override apiUrl closes #6239 --- core/shared/ghost-url.js | 7 ++++--- core/test/unit/ghost_url_spec.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/core/shared/ghost-url.js b/core/shared/ghost-url.js index 6dea7045ef..956842c6f1 100644 --- a/core/shared/ghost-url.js +++ b/core/shared/ghost-url.js @@ -30,7 +30,8 @@ url = { api: function () { var args = Array.prototype.slice.call(arguments), - queryOptions; + queryOptions, + requestUrl = apiUrl; if (args.length && typeof args[args.length - 1] === 'object') { queryOptions = args.pop(); @@ -43,11 +44,11 @@ if (args.length) { args.forEach(function (el) { - apiUrl += el.replace(/^\/|\/$/g, '') + '/'; + requestUrl += el.replace(/^\/|\/$/g, '') + '/'; }); } - return apiUrl + generateQueryString(queryOptions); + return requestUrl + generateQueryString(queryOptions); } }; diff --git a/core/test/unit/ghost_url_spec.js b/core/test/unit/ghost_url_spec.js index dcf603ac89..3a2f9a5f0b 100644 --- a/core/test/unit/ghost_url_spec.js +++ b/core/test/unit/ghost_url_spec.js @@ -127,4 +127,20 @@ describe('Ghost Ajax Helper', function () { rendered.should.match(/include=tags%2Ctests/); rendered.should.match(/page=2/); }); + + it('should be idempotent', function () { + configUtils.set({ + url: 'https://testblog.com/blog/' + }); + ghostUrl.init({ + clientId: 'ghost-frontend', + clientSecret: 'notasecret', + url: configUtils.config.apiUrl() + }); + + var rendered = ghostUrl.url.api('posts', {limit: 3}), + rendered2 = ghostUrl.url.api('posts', {limit: 3}); + + rendered.should.equal(rendered2); + }); });