Skip to content

Commit 1913f20

Browse files
committed
feat(webex-core): adding support for a custom proxy
1 parent 7f1542c commit 1913f20

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

packages/@webex/webex-core/src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export {default as RequestEventInterceptor} from './interceptors/request-event';
5151
export {default as RequestLoggerInterceptor} from './interceptors/request-logger';
5252
export {default as RequestTimingInterceptor} from './interceptors/request-timing';
5353
export {default as UserAgentInterceptor} from './interceptors/user-agent';
54+
export {default as ProxyInterceptor} from './interceptors/proxy';
5455
export {default as WebexTrackingIdInterceptor} from './interceptors/webex-tracking-id';
5556
export {default as WebexUserAgentInterceptor} from './interceptors/webex-user-agent';
5657
export {default as RateLimitInterceptor} from './interceptors/rate-limit';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*!
2+
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3+
*/
4+
5+
import {inBrowser} from '@webex/common';
6+
import {Interceptor} from '@webex/http-core';
7+
import {get} from 'lodash';
8+
9+
const strings = new WeakMap();
10+
11+
/**
12+
* Sets a proxy on all requests if one is not present.
13+
* Defaults to none, though a custom proxy can be set
14+
* using the proxy configuration. e.g.
15+
*
16+
* webex = WebexSdk.init({
17+
* credentials: {
18+
* supertoken: superToken
19+
* },
20+
* config: {
21+
* credentials: {
22+
* client_id,
23+
* client_secret
24+
* },
25+
* proxy: 'http://myproxy.company.com'
26+
* }
27+
* });
28+
*/
29+
export default class ProxyInterceptor extends Interceptor {
30+
/**
31+
* @param {Object} [options={}]
32+
* @param {WebexCore} [options.webex]
33+
* @private
34+
* @returns {ProxyInterceptor}
35+
*/
36+
constructor(options = {}) {
37+
const proxy = get(options, 'webex.config.proxy');
38+
39+
super(options);
40+
if (proxy) {
41+
strings.set(this, proxy);
42+
}
43+
}
44+
45+
/**
46+
* @returns {ProxyInterceptor}
47+
*/
48+
static create() {
49+
return new ProxyInterceptor({webex: this});
50+
}
51+
52+
/**
53+
* @see Interceptor#onRequest
54+
* @param {Object} options
55+
* @returns {Object}
56+
*/
57+
onRequest(options) {
58+
// Do not set a proxy for browsers
59+
if (inBrowser) {
60+
return options;
61+
}
62+
63+
const proxy = strings.get(this);
64+
if (proxy) {
65+
options.proxy = proxy;
66+
}
67+
68+
return options;
69+
}
70+
}

packages/@webex/webex-core/src/webex-core.js

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import RequestTimingInterceptor from './interceptors/request-timing';
2626
import ResponseLoggerInterceptor from './interceptors/response-logger';
2727
import WebexHttpError from './lib/webex-http-error';
2828
import UserAgentInterceptor from './interceptors/user-agent';
29+
import ProxyInterceptor from './interceptors/proxy';
2930
import WebexTrackingIdInterceptor from './interceptors/webex-tracking-id';
3031
import WebexUserAgentInterceptor from './interceptors/webex-user-agent';
3132
import RateLimitInterceptor from './interceptors/rate-limit';
@@ -57,6 +58,7 @@ const interceptors = {
5758
RequestTimingInterceptor: RequestTimingInterceptor.create,
5859
ServiceInterceptor: undefined,
5960
UserAgentInterceptor: UserAgentInterceptor.create,
61+
ProxyInterceptor: ProxyInterceptor.create,
6062
WebexUserAgentInterceptor: WebexUserAgentInterceptor.create,
6163
AuthInterceptor: AuthInterceptor.create,
6264
KmsDryErrorInterceptor: undefined,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*!
2+
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3+
*/
4+
5+
import {assert} from '@webex/test-helper-chai';
6+
import {skipInBrowser} from '@webex/test-helper-mocha';
7+
import {ProxyInterceptor} from '@webex/webex-core';
8+
9+
import pkg from '../../../../package';
10+
11+
describe('webex-core', () => {
12+
describe('Interceptors', () => {
13+
describe('ProxyInterceptor', () => {
14+
// Do not set proxy for browsers
15+
skipInBrowser(describe)('#onRequest', () => {
16+
it('default proxy', () => {
17+
const interceptor = Reflect.apply(
18+
ProxyInterceptor.create,
19+
{
20+
version: pkg.version,
21+
},
22+
[]
23+
);
24+
const options = {};
25+
26+
interceptor.onRequest(options);
27+
28+
assert.isUndefined(options.proxy);
29+
});
30+
31+
it('custom proxy', () => {
32+
const interceptor = Reflect.apply(
33+
ProxyInterceptor.create,
34+
{
35+
version: pkg.version,
36+
config: {
37+
proxy: 'http://proxy.company.com'
38+
},
39+
},
40+
[]
41+
);
42+
const options = {};
43+
44+
interceptor.onRequest(options);
45+
46+
assert.property(options, 'proxy');
47+
assert.equal(options.proxy, 'http://proxy.company.com');
48+
});
49+
});
50+
});
51+
});
52+
});

0 commit comments

Comments
 (0)