-
Notifications
You must be signed in to change notification settings - Fork 265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: UA string in Node no longer continuously extends #1448
Changes from 2 commits
29c4e58
e1d45c8
45c5e6c
5d845b2
6c56b19
3153368
dcda5f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,118 @@ | ||
const SDK_VERSION = (global as any).SDK_VERSION; | ||
const NODE_VERSION = (global as any).NODE_VERSION; | ||
|
||
import { OktaUserAgent } from '../../lib/http/OktaUserAgent'; | ||
|
||
jest.mock('../../lib/features', () => { | ||
const mocked = { | ||
isBrowser: jest.fn() | ||
}; | ||
jest.mock('lib/features', () => { | ||
return { | ||
isBrowser: () => {} | ||
__esModule: true, | ||
isBrowser: () => mocked.isBrowser() | ||
}; | ||
}); | ||
|
||
const mocked = { | ||
features: require('../../lib/features') | ||
}; | ||
|
||
describe('OktaUserAgent', () => { | ||
let oktaUserAgent; | ||
let sdkVersion; | ||
beforeEach(async () => { | ||
sdkVersion = (await import('../../package.json')).version; | ||
oktaUserAgent = new OktaUserAgent(); | ||
const context: any = {}; | ||
|
||
beforeEach(() => { | ||
context.oktaUserAgent = new OktaUserAgent(); | ||
}); | ||
|
||
describe('browser env', () => { | ||
beforeEach(() => { | ||
jest.spyOn(mocked.features, 'isBrowser').mockReturnValue(true); | ||
mocked.isBrowser.mockReturnValue(true); | ||
context.expected = `okta-auth-js/${SDK_VERSION}`; | ||
context.oktaUserAgent = new OktaUserAgent(); | ||
}); | ||
|
||
it('gets okta-auth-js and node info in the Okta UA by default', () => { | ||
const { oktaUserAgent, expected } = context; | ||
const httpHeader = oktaUserAgent.getHttpHeader(); | ||
expect(httpHeader).toEqual({ | ||
'X-Okta-User-Agent-Extended': `okta-auth-js/${sdkVersion}` | ||
'X-Okta-User-Agent-Extended': expected | ||
}); | ||
}); | ||
|
||
it('can add extra environment', () => { | ||
const { oktaUserAgent, expected } = context; | ||
oktaUserAgent.addEnvironment('fake/x.y'); | ||
const httpHeader = oktaUserAgent.getHttpHeader(); | ||
expect(httpHeader).toEqual({ | ||
'X-Okta-User-Agent-Extended': `okta-auth-js/${sdkVersion} fake/x.y` | ||
'X-Okta-User-Agent-Extended': `${expected} fake/x.y` | ||
}); | ||
}); | ||
|
||
// Reason: OKTA-641280 | ||
it('should return same header after multiple calls', () => { | ||
const { oktaUserAgent, expected } = context; | ||
expect(oktaUserAgent.getHttpHeader()).toEqual({ | ||
'X-Okta-User-Agent-Extended': expected | ||
}); | ||
expect(oktaUserAgent.getHttpHeader()).toEqual({ | ||
'X-Okta-User-Agent-Extended': expected | ||
}); | ||
expect(oktaUserAgent.getHttpHeader()).toEqual({ | ||
'X-Okta-User-Agent-Extended': expected | ||
}); | ||
expect(oktaUserAgent.getHttpHeader()).toEqual({ | ||
'X-Okta-User-Agent-Extended': expected | ||
}); | ||
expect(oktaUserAgent.getHttpHeader()).toEqual({ | ||
'X-Okta-User-Agent-Extended': expected | ||
}); | ||
}); | ||
}); | ||
|
||
describe('node env', () => { | ||
let nodeVersion = process.versions.node; | ||
beforeEach(() => { | ||
jest.spyOn(mocked.features, 'isBrowser').mockReturnValue(false); | ||
mocked.isBrowser.mockReturnValue(false); | ||
(global as any).node = NODE_VERSION; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this line do? As I understand, Node version is being read from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That line actually does nothing, I just tested it 🤦 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove before merging if it's not doing anything |
||
context.expected = `okta-auth-js/${SDK_VERSION} nodejs/${NODE_VERSION}`; | ||
context.oktaUserAgent = new OktaUserAgent(); | ||
}); | ||
|
||
it('gets okta-auth-js and node info in the Okta UA by default', () => { | ||
const { oktaUserAgent, expected } = context; | ||
const httpHeader = oktaUserAgent.getHttpHeader(); | ||
expect(httpHeader).toEqual({ | ||
'X-Okta-User-Agent-Extended': `okta-auth-js/${sdkVersion} nodejs/${nodeVersion}` | ||
'X-Okta-User-Agent-Extended': expected | ||
}); | ||
}); | ||
|
||
it('can add extra environment', () => { | ||
const { oktaUserAgent, expected } = context; | ||
oktaUserAgent.addEnvironment('fake/x.y'); | ||
const httpHeader = oktaUserAgent.getHttpHeader(); | ||
expect(httpHeader).toEqual({ | ||
'X-Okta-User-Agent-Extended': `okta-auth-js/${sdkVersion} fake/x.y nodejs/${nodeVersion}` | ||
'X-Okta-User-Agent-Extended': `${expected} fake/x.y` | ||
}); | ||
}); | ||
|
||
// Reason: OKTA-641280 | ||
it('should return same header after multiple calls', () => { | ||
const { oktaUserAgent, expected } = context; | ||
expect(oktaUserAgent.getHttpHeader()).toEqual({ | ||
'X-Okta-User-Agent-Extended': expected | ||
}); | ||
expect(oktaUserAgent.getHttpHeader()).toEqual({ | ||
'X-Okta-User-Agent-Extended': expected | ||
}); | ||
expect(oktaUserAgent.getHttpHeader()).toEqual({ | ||
'X-Okta-User-Agent-Extended': expected | ||
}); | ||
expect(oktaUserAgent.getHttpHeader()).toEqual({ | ||
'X-Okta-User-Agent-Extended': expected | ||
}); | ||
expect(oktaUserAgent.getHttpHeader()).toEqual({ | ||
'X-Okta-User-Agent-Extended': expected | ||
}); | ||
}); | ||
}); | ||
|
||
it('can get sdk version', () => { | ||
expect(oktaUserAgent.getVersion()).toBe(sdkVersion); | ||
const { oktaUserAgent } = context; | ||
expect(oktaUserAgent.getVersion()).toBe(SDK_VERSION); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we augment the
global
type locally with these variables so we don't need to cast toany
?