Skip to content
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

Merged
merged 7 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 7.4.2

### Bug Fix

- [#1448](https://github.com/okta/okta-auth-js/pull/1448) Fix: UA string in Node no longer continuously extends

## 7.4.1

### Bug Fix
Expand Down
2 changes: 1 addition & 1 deletion lib/http/OktaUserAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ export class OktaUserAgent {
constructor() {
// add base sdk env
this.environments = [`okta-auth-js/${SDK_VERSION}`];
this.maybeAddNodeEnvironment();
}

addEnvironment(env: string) {
this.environments.push(env);
}

getHttpHeader() {
this.maybeAddNodeEnvironment();
return { 'X-Okta-User-Agent-Extended': this.environments.join(' ') };
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": true,
"name": "@okta/okta-auth-js",
"description": "The Okta Auth SDK",
"version": "7.4.1",
"version": "7.4.2",
"homepage": "https://github.com/okta/okta-auth-js",
"license": "Apache-2.0",
"main": "build/cjs/exports/default.js",
Expand Down
93 changes: 74 additions & 19 deletions test/spec/OktaUserAgent.ts
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;
Comment on lines +1 to +2
Copy link
Contributor

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 to any?


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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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 process.versions.node

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That line actually does nothing, I just tested it 🤦

Copy link
Contributor

Choose a reason for hiding this comment

The 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);
});
});
9 changes: 8 additions & 1 deletion test/support/jest/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@
var OktaAuth = '<rootDir>/lib/exports/default';
var SDK_VERSION = require('../../../package.json').version;

const { node: NODE_VERSION } = process.versions;

module.exports = {
'coverageDirectory': '<rootDir>/build2/reports/coverage-browser',
'collectCoverage': true,
'collectCoverageFrom': ['./lib/**','!./test/**'],
'globals': {
SDK_VERSION
SDK_VERSION,
NODE_VERSION,
'ts-jest': {
SDK_VERSION,
NODE_VERSION
}
},
'transform': {
'^.+\\.(js)$': 'babel-jest',
Expand Down