Skip to content

Commit

Permalink
feat(webex-core): adding support for a custom proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiuipuv committed Jul 31, 2024
1 parent 56a9a87 commit 7befb70
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/@webex/webex-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export {default as RequestEventInterceptor} from './interceptors/request-event';
export {default as RequestLoggerInterceptor} from './interceptors/request-logger';
export {default as RequestTimingInterceptor} from './interceptors/request-timing';
export {default as UserAgentInterceptor} from './interceptors/user-agent';
export {default as ProxyInterceptor} from './interceptors/proxy';
export {default as WebexTrackingIdInterceptor} from './interceptors/webex-tracking-id';
export {default as WebexUserAgentInterceptor} from './interceptors/webex-user-agent';
export {default as RateLimitInterceptor} from './interceptors/rate-limit';
Expand Down
70 changes: 70 additions & 0 deletions packages/@webex/webex-core/src/interceptors/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*!
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
*/

import {inBrowser} from '@webex/common';
import {Interceptor} from '@webex/http-core';
import {get} from 'lodash';

const strings = new WeakMap();

/**
* Sets a proxy on all requests if one is not present.
* Defaults to none, though a custom proxy can be set
* using the proxy configuration. e.g.
*
* webex = WebexSdk.init({
* credentials: {
* supertoken: superToken
* },
* config: {
* credentials: {
* client_id,
* client_secret
* },
* proxy: 'http://myproxy.company.com'
* }
* });
*/
export default class ProxyInterceptor extends Interceptor {
/**
* @param {Object} [options={}]
* @param {WebexCore} [options.webex]
* @private
* @returns {ProxyInterceptor}
*/
constructor(options = {}) {
const proxy = get(options, 'webex.config.proxy');

super(options);
if (proxy) {
strings.set(this, proxy);
}
}

/**
* @returns {ProxyInterceptor}
*/
static create() {
return new ProxyInterceptor({webex: this});
}

/**
* @see Interceptor#onRequest
* @param {Object} options
* @returns {Object}
*/
onRequest(options) {
// Do not set a proxy for browsers
if (inBrowser) {
return options;
}

const proxy = strings.get(this);
if (proxy) {
options.proxy = proxy;
}

return options;
}
}
2 changes: 2 additions & 0 deletions packages/@webex/webex-core/src/webex-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import RequestTimingInterceptor from './interceptors/request-timing';
import ResponseLoggerInterceptor from './interceptors/response-logger';
import WebexHttpError from './lib/webex-http-error';
import UserAgentInterceptor from './interceptors/user-agent';
import ProxyInterceptor from './interceptors/proxy';
import WebexTrackingIdInterceptor from './interceptors/webex-tracking-id';
import WebexUserAgentInterceptor from './interceptors/webex-user-agent';
import RateLimitInterceptor from './interceptors/rate-limit';
Expand Down Expand Up @@ -57,6 +58,7 @@ const interceptors = {
RequestTimingInterceptor: RequestTimingInterceptor.create,
ServiceInterceptor: undefined,
UserAgentInterceptor: UserAgentInterceptor.create,
ProxyInterceptor: ProxyInterceptor.create,
WebexUserAgentInterceptor: WebexUserAgentInterceptor.create,
AuthInterceptor: AuthInterceptor.create,
KmsDryErrorInterceptor: undefined,
Expand Down
52 changes: 52 additions & 0 deletions packages/@webex/webex-core/test/unit/spec/interceptors/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*!
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
*/

import {assert} from '@webex/test-helper-chai';
import {skipInBrowser} from '@webex/test-helper-mocha';
import {ProxyInterceptor} from '@webex/webex-core';

import pkg from '../../../../package';

describe('webex-core', () => {
describe('Interceptors', () => {
describe('ProxyInterceptor', () => {
// Do not set proxy for browsers
skipInBrowser(describe)('#onRequest', () => {
it('default proxy', () => {
const interceptor = Reflect.apply(
ProxyInterceptor.create,
{
version: pkg.version,
},
[]
);
const options = {};

interceptor.onRequest(options);

assert.isUndefined(options.proxy);
});

it('custom proxy', () => {
const interceptor = Reflect.apply(
ProxyInterceptor.create,
{
version: pkg.version,
config: {
proxy: 'http://proxy.company.com'
},
},
[]
);
const options = {};

interceptor.onRequest(options);

assert.property(options, 'proxy');
assert.equal(options.proxy, 'http://proxy.company.com');
});
});
});
});
});

0 comments on commit 7befb70

Please sign in to comment.