diff --git a/lib/utils.js b/lib/utils.js index 2bec38a..7dd6f0a 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -4,6 +4,9 @@ import pkgJson from '../package.json' const Promise = window.Promise || NativePromise export function updateUserAgentWithAppId (headers, appId) { + if (!appId) { + return headers + } 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/spec/utils_spec.js b/spec/utils_spec.js index 033fca1..cc0bb5f 100644 --- a/spec/utils_spec.js +++ b/spec/utils_spec.js @@ -159,12 +159,51 @@ describe('Utils', () => { 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) + describe('with a valid app id', () => { + let appId + + beforeEach(() => { + appId = '1' + }) + + it('should append the app id to the user agent', () => { + const headers = { 'User-Agent': 'Mozilla/5.0' } + 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) + }) + + it('should handle an undefined headers object', () => { + const updatedHeaders = Utils.updateUserAgentWithAppId(undefined, appId) + const expectedUserAgent = `zendesk_app_framework_sdk/sdk_version:${pkgJson.version}/app_id:${appId}` + expect(updatedHeaders['User-Agent']).to.equal(expectedUserAgent) + }) + + it('should handle a headers object with no User-Agent', () => { + const headers = {} + const updatedHeaders = Utils.updateUserAgentWithAppId(headers, appId) + const expectedUserAgent = `zendesk_app_framework_sdk/sdk_version:${pkgJson.version}/app_id:${appId}` + expect(updatedHeaders['User-Agent']).to.equal(expectedUserAgent) + }) + }) + + describe('with an undefined app id', () => { + it('should not append the app id to the user agent', () => { + const headers = { 'User-Agent': 'Mozilla/5.0' } + const updatedHeaders = Utils.updateUserAgentWithAppId(headers) + expect(updatedHeaders['User-Agent']).to.equal('Mozilla/5.0') + }) + + it('should handle an undefined headers object', () => { + const updatedHeaders = Utils.updateUserAgentWithAppId() + expect(updatedHeaders).to.be.undefined() + }) + + it('should handle a headers object with no User-Agent', () => { + const headers = {} + const updatedHeaders = Utils.updateUserAgentWithAppId(headers) + expect(updatedHeaders['User-Agent']).to.equal(undefined) + }) }) }) })