Skip to content

Commit

Permalink
feat: support to pass additional user agent (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
embbnux committed Aug 2, 2022
1 parent 2a58af1 commit 8c1abb1
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
113 changes: 113 additions & 0 deletions sdk/src/platform/Platform-spec.ts
Expand Up @@ -13,6 +13,8 @@ import {
getExternalDiscoveryMockData,
} from '../test/test';

import {version} from '../core/Constants';

const globalAny: any = global;
const windowAny: any = typeof window !== 'undefined' ? window : global;

Expand Down Expand Up @@ -43,6 +45,117 @@ describe('RingCentral.platform.Platform', () => {
);
});

describe('X-User-Agent', () => {
it(
'is added with default value',
asyncTest(async sdk => {
const platform = sdk.platform();
const client = sdk.client();
const path = `/restapi/v1.0/foo/get`;

apiCall('get', path, {foo: 'bar'});
let request;
client.on(client.events.requestSuccess, (_, r) => {
request = r;
});
await platform.get(path, null);
expect(request.headers.get('x-user-agent')).to.equal(`RCJSSDK/${version}`);
}),
);

it(
'is added with app name and version',
asyncTest(
async sdk => {
const platform = sdk.platform();
const client = sdk.client();
const path = `/restapi/v1.0/foo/get`;

apiCall('get', path, {foo: 'bar'});
let request;
client.on(client.events.requestSuccess, (_, r) => {
request = r;
});
await platform.get(path, null);
expect(request.headers.get('x-user-agent')).have.string('TestApp/1.0.0 ');
},
{
appName: 'TestApp',
appVersion: '1.0.0',
},
),
);

it(
'is added with app name',
asyncTest(
async sdk => {
const platform = sdk.platform();
const client = sdk.client();
const path = `/restapi/v1.0/foo/get`;

apiCall('get', path, {foo: 'bar'});
let request;
client.on(client.events.requestSuccess, (_, r) => {
request = r;
});
await platform.get(path, null);
expect(request.headers.get('x-user-agent')).have.string('TestApp ');
},
{
appName: 'TestApp',
},
),
);

it(
'is added with additional user agent',
asyncTest(
async sdk => {
const platform = sdk.platform();
const client = sdk.client();
const path = `/restapi/v1.0/foo/get`;

apiCall('get', path, {foo: 'bar'});
let request;
client.on(client.events.requestSuccess, (_, r) => {
request = r;
});
await platform.get(path, null);
expect(request.headers.get('x-user-agent')).have.string(' (build.1000; rev.149f00000)');
},
{
additionalUserAgent: '(build.1000; rev.149f00000)',
},
),
);

it(
'is added with app name, version and additional user agent',
asyncTest(
async sdk => {
const platform = sdk.platform();
const client = sdk.client();
const path = `/restapi/v1.0/foo/get`;

apiCall('get', path, {foo: 'bar'});
let request;
client.on(client.events.requestSuccess, (_, r) => {
request = r;
});
await platform.get(path, null);
expect(request.headers.get('x-user-agent')).have.string('TestApp/1.0.0 ');
expect(request.headers.get('x-user-agent')).have.string(' (build.1000; rev.149f00000)');
},
{
appName: 'TestApp',
appVersion: '1.0.0',
additionalUserAgent: '(build.1000; rev.149f00000)',
},
),
);
});

describe('authorized', () => {
it(
'initiates refresh if not authorized',
Expand Down
4 changes: 3 additions & 1 deletion sdk/src/platform/Platform.ts
Expand Up @@ -96,6 +96,7 @@ export default class Platform extends EventEmitter {
clearCacheOnRefreshError = true,
appName = '',
appVersion = '',
additionalUserAgent = '',
externals,
cache,
client,
Expand Down Expand Up @@ -125,7 +126,7 @@ export default class Platform extends EventEmitter {
this._urlPrefix = urlPrefix;
this._userAgent = `${appName ? `${appName + (appVersion ? `/${appVersion}` : '')} ` : ''}RCJSSDK/${
Constants.version
}`;
}${additionalUserAgent ? ` ${additionalUserAgent}` : ''}`;

this._externals = externals;
this._cache = cache;
Expand Down Expand Up @@ -847,6 +848,7 @@ export interface PlatformOptions extends AuthOptions {
clearCacheOnRefreshError?: boolean;
appName?: string;
appVersion?: string;
additionalUserAgent?: string;
tokenEndpoint?: string;
revokeEndpoint?: string;
authorizeEndpoint?: string;
Expand Down

0 comments on commit 8c1abb1

Please sign in to comment.