Skip to content

feat(webex-core): adding support for a custom proxy #3624

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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 @@ -52,6 +52,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 @@ -58,6 +59,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');
});
});
});
});
});
Loading