Skip to content

Commit

Permalink
Test updateUserAgentWithAppId more thoroughly + break early if appId …
Browse files Browse the repository at this point in the history
…undefined
  • Loading branch information
token-cjg committed Nov 6, 2024
1 parent 23e6df8 commit 1c88245
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 45 additions & 6 deletions spec/utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
})

0 comments on commit 1c88245

Please sign in to comment.