-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webex-core): adding support for a custom proxy
- Loading branch information
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
packages/@webex/webex-core/test/unit/spec/interceptors/proxy.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |