From 8c1abb188b0a556cd7fa59517d5af47c763194eb Mon Sep 17 00:00:00 2001 From: Embbnux Ji Date: Wed, 3 Aug 2022 03:55:32 +0800 Subject: [PATCH] feat: support to pass additional user agent (#190) --- sdk/src/platform/Platform-spec.ts | 113 ++++++++++++++++++++++++++++++ sdk/src/platform/Platform.ts | 4 +- 2 files changed, 116 insertions(+), 1 deletion(-) diff --git a/sdk/src/platform/Platform-spec.ts b/sdk/src/platform/Platform-spec.ts index fea1237..f9fffe1 100644 --- a/sdk/src/platform/Platform-spec.ts +++ b/sdk/src/platform/Platform-spec.ts @@ -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; @@ -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', diff --git a/sdk/src/platform/Platform.ts b/sdk/src/platform/Platform.ts index a86f84c..303df9a 100644 --- a/sdk/src/platform/Platform.ts +++ b/sdk/src/platform/Platform.ts @@ -96,6 +96,7 @@ export default class Platform extends EventEmitter { clearCacheOnRefreshError = true, appName = '', appVersion = '', + additionalUserAgent = '', externals, cache, client, @@ -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; @@ -847,6 +848,7 @@ export interface PlatformOptions extends AuthOptions { clearCacheOnRefreshError?: boolean; appName?: string; appVersion?: string; + additionalUserAgent?: string; tokenEndpoint?: string; revokeEndpoint?: string; authorizeEndpoint?: string;