diff --git a/lib/client.js b/lib/client.js index 4beedfe..8b35a74 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1,6 +1,6 @@ /* global URL */ import pkgJson from '../package.json' -import { when, isObject, isString, debounce, addUserAgent } from './utils' +import { when, isObject, isString, debounce, updateUserAgentWithAppId } from './utils' import IdleState from './utils/idleState' import Tracker from './tracker' import NativePromise from 'native-promise-only' @@ -558,7 +558,7 @@ export default class Client { } const appId = this._metadata && this._metadata.appId - options.headers = addUserAgent(options.headers, appId) + options.headers = updateUserAgentWithAppId(options.headers, appId) const doneEventKey = `${requestKey}.done` const failEventKey = `${requestKey}.fail` diff --git a/lib/utils.js b/lib/utils.js index 5ade59c..2bec38a 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -3,7 +3,7 @@ import pkgJson from '../package.json' const Promise = window.Promise || NativePromise -export function addUserAgent (headers, appId) { +export function updateUserAgentWithAppId (headers, appId) { const originalUserAgent = headers && headers['User-Agent'] ? headers['User-Agent'] : '' const zafSdkUserAgentString = `zendesk_app_framework_sdk/sdk_version:${pkgJson.version}/app_id:${appId}` const userAgent = originalUserAgent ? `${originalUserAgent} ${zafSdkUserAgentString}` : zafSdkUserAgentString diff --git a/package.json b/package.json index 0d311d9..9a9e2a1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zendesk_app_framework_sdk", - "version": "2.0.37", + "version": "2.0.38", "main": "build/zaf_sdk.min.js", "description": "The Zendesk App Framework (ZAF) SDK is a JavaScript library that simplifies cross-frame communication between iframed apps and ZAF.", "homepage": "http://developer.zendesk.com", diff --git a/spec/utils_spec.js b/spec/utils_spec.js index 374caba..033fca1 100644 --- a/spec/utils_spec.js +++ b/spec/utils_spec.js @@ -1,6 +1,7 @@ /* eslint-env mocha */ /* global expect Promise */ import * as Utils from '../lib/utils' +import pkgJson from '../package.json' describe('Utils', () => { let params @@ -151,4 +152,19 @@ describe('Utils', () => { expect(Utils.isObject(['a'])).to.equal(true) }) }) + + describe('.updateUserAgentWithAppId', () => { + before(() => { + // Mock pkgJson + global.pkgJson = { version: '1.0.0' } + }) + + it('should append the app id to the user agent', () => { + const headers = { 'User-Agent': 'Mozilla/5.0' } + const appId = '1' + const updatedHeaders = Utils.updateUserAgentWithAppId(headers, appId) + const expectedUserAgent = `Mozilla/5.0 zendesk_app_framework_sdk/sdk_version:${pkgJson.version}/app_id:${appId}` + expect(updatedHeaders['User-Agent']).to.equal(expectedUserAgent) + }) + }) })