Skip to content

Commit 683d24d

Browse files
committed
Allowed disabling setting of 'UserAgent' header
refs https://github.com/TryGhost/Toolbox/issues/348 refs https://github.com/TryGhost/Toolbox/issues/301 - The behavior is symmetric to the one in Conte API SDK allowing to optionaly disable sending User-Agent header. Gives more control to the client to skip sending the header for whatever reason
1 parent 39d539d commit 683d24d

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

packages/admin-api/lib/admin-api.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const resolveAPIPrefix = (version) => {
4141
* @param {String} options.url
4242
* @param {String} [options.ghostPath]
4343
* @param {String|Boolean} options.version - a version string like v3.2, v4.1, v5.8 or boolean value identifying presence of Accept-Version header
44+
* @param {Boolean} [options.userAgent] - flag controlling if the 'User-Agent' header should be sent with a request
4445
* @param {Function} [options.makeRequest]
4546
* @param {Function} [options.generateToken]
4647
*/
@@ -51,6 +52,7 @@ module.exports = function GhostAdminAPI(options) {
5152

5253
const defaultConfig = {
5354
ghostPath: 'ghost',
55+
userAgent: true,
5456
generateToken: token,
5557
makeRequest({url, method, data, params = {}, headers = {}}) {
5658
return axios({
@@ -411,10 +413,13 @@ module.exports = function GhostAdminAPI(options) {
411413
authorizationHeader = `Ghost ${config.generateToken(key, audience)}`;
412414

413415
const ghostHeaders = {
414-
Authorization: authorizationHeader,
415-
'User-Agent': `GhostAdminSDK/${packageVersion}`
416+
Authorization: authorizationHeader
416417
};
417418

419+
if (config.userAgent) {
420+
ghostHeaders['User-Agent'] = `GhostAdminSDK/${packageVersion}`;
421+
}
422+
418423
if (config.acceptVersionHeader) {
419424
ghostHeaders['Accept-Version'] = config.acceptVersionHeader;
420425
}

packages/admin-api/test/lib/admin-api.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,5 +290,29 @@ describe('GhostAdminAPI general', function () {
290290
should.equal(generateTokenSpy.args[0][0], '5c73def7a21ad85eda5d4faa:d9a3e5b2d6c2a4afb094655c4dc543220be60b3561fa9622e3891213cb4357d0');
291291
should.equal(generateTokenSpy.args[0][1], '/admin/');
292292
});
293+
294+
it('does not set "User-Agent" header when disabled', async function () {
295+
const makeRequestStub = sinon.stub().returns(Promise.resolve({
296+
config: {}
297+
}));
298+
const generateTokenSpy = sinon.spy();
299+
300+
const api = new GhostAdminAPI({
301+
version: true,
302+
url: `http://ghost.local`,
303+
key: '5c73def7a21ad85eda5d4faa:d9a3e5b2d6c2a4afb094655c4dc543220be60b3561fa9622e3891213cb4357d0',
304+
makeRequest: makeRequestStub,
305+
generateToken: generateTokenSpy,
306+
userAgent: false
307+
});
308+
309+
await api.config.read();
310+
311+
makeRequestStub.calledOnce.should.be.true();
312+
should.equal(makeRequestStub.args[0][0].headers['Accept-Version'], 'v5.0');
313+
should.equal(makeRequestStub.args[0][0].headers['User-Agent'], undefined);
314+
should.equal(generateTokenSpy.args[0][0], '5c73def7a21ad85eda5d4faa:d9a3e5b2d6c2a4afb094655c4dc543220be60b3561fa9622e3891213cb4357d0');
315+
should.equal(generateTokenSpy.args[0][1], '/admin/');
316+
});
293317
});
294318
});

0 commit comments

Comments
 (0)