0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Fix non-idempotent Ghost API helper

- Add test
- Don't override apiUrl

closes #6239
This commit is contained in:
Fabian Becker 2015-12-18 10:15:24 +01:00 committed by Fabian Becker
parent 48bc031ac1
commit 9899f8d4e7
2 changed files with 20 additions and 3 deletions

View file

@ -30,7 +30,8 @@
url = { url = {
api: function () { api: function () {
var args = Array.prototype.slice.call(arguments), var args = Array.prototype.slice.call(arguments),
queryOptions; queryOptions,
requestUrl = apiUrl;
if (args.length && typeof args[args.length - 1] === 'object') { if (args.length && typeof args[args.length - 1] === 'object') {
queryOptions = args.pop(); queryOptions = args.pop();
@ -43,11 +44,11 @@
if (args.length) { if (args.length) {
args.forEach(function (el) { args.forEach(function (el) {
apiUrl += el.replace(/^\/|\/$/g, '') + '/'; requestUrl += el.replace(/^\/|\/$/g, '') + '/';
}); });
} }
return apiUrl + generateQueryString(queryOptions); return requestUrl + generateQueryString(queryOptions);
} }
}; };

View file

@ -127,4 +127,20 @@ describe('Ghost Ajax Helper', function () {
rendered.should.match(/include=tags%2Ctests/); rendered.should.match(/include=tags%2Ctests/);
rendered.should.match(/page=2/); 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);
});
}); });