forked from webex/webex-js-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
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): hostmap interceptor (webex#3789)
- Loading branch information
Showing
7 changed files
with
201 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ export { | |
Services, | ||
ServiceHost, | ||
ServiceUrl, | ||
HostMapInterceptor, | ||
} from './lib/services'; | ||
|
||
export { | ||
|
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
36 changes: 36 additions & 0 deletions
36
packages/@webex/webex-core/src/lib/services/interceptors/hostmap.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,36 @@ | ||
/*! | ||
* Copyright (c) 2015-2024 Cisco Systems, Inc. See LICENSE file. | ||
*/ | ||
|
||
import {Interceptor} from '@webex/http-core'; | ||
|
||
/** | ||
* This interceptor replaces the host in the request uri with the host from the hostmap | ||
* It will attempt to do this for every request, but not all URIs will be in the hostmap | ||
* URIs with hosts that are not in the hostmap will be left unchanged | ||
*/ | ||
export default class HostMapInterceptor extends Interceptor { | ||
/** | ||
* @returns {HostMapInterceptor} | ||
*/ | ||
static create() { | ||
return new HostMapInterceptor({webex: this}); | ||
} | ||
|
||
/** | ||
* @see Interceptor#onRequest | ||
* @param {Object} options | ||
* @returns {Object} | ||
*/ | ||
onRequest(options) { | ||
if (options.uri) { | ||
try { | ||
options.uri = this.webex.internal.services.replaceHostFromHostmap(options.uri); | ||
} catch (error) { | ||
/* empty */ | ||
} | ||
} | ||
|
||
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
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
79 changes: 79 additions & 0 deletions
79
packages/@webex/webex-core/test/unit/spec/services/interceptors/hostmap.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,79 @@ | ||
/*! | ||
* Copyright (c) 2015-2024 Cisco Systems, Inc. See LICENSE file. | ||
*/ | ||
|
||
/* eslint-disable camelcase */ | ||
|
||
import sinon from 'sinon'; | ||
import {assert} from '@webex/test-helper-chai'; | ||
import MockWebex from '@webex/test-helper-mock-webex'; | ||
import {HostMapInterceptor, config, Credentials} from '@webex/webex-core'; | ||
import {cloneDeep} from 'lodash'; | ||
|
||
describe('webex-core', () => { | ||
describe('Interceptors', () => { | ||
describe('HostMapInterceptor', () => { | ||
let interceptor, webex; | ||
|
||
beforeEach(() => { | ||
webex = new MockWebex({ | ||
children: { | ||
credentials: Credentials, | ||
}, | ||
config: cloneDeep(config), | ||
request: sinon.spy(), | ||
}); | ||
|
||
webex.internal.services = { | ||
replaceHostFromHostmap: sinon.stub().returns('http://replaceduri.com'), | ||
} | ||
|
||
interceptor = Reflect.apply(HostMapInterceptor.create, webex, []); | ||
}); | ||
|
||
describe('#onRequest', () => { | ||
it('calls replaceHostFromHostmap if options.uri is defined', () => { | ||
const options = { | ||
uri: 'http://example.com', | ||
}; | ||
|
||
interceptor.onRequest(options); | ||
|
||
sinon.assert.calledWith( | ||
webex.internal.services.replaceHostFromHostmap, | ||
'http://example.com' | ||
); | ||
|
||
assert.equal(options.uri, 'http://replaceduri.com'); | ||
}); | ||
|
||
it('does not call replaceHostFromHostmap if options.uri is not defined', () => { | ||
const options = {}; | ||
|
||
interceptor.onRequest(options); | ||
|
||
sinon.assert.notCalled(webex.internal.services.replaceHostFromHostmap); | ||
|
||
assert.isUndefined(options.uri); | ||
}); | ||
|
||
it('does not modify options.uri if replaceHostFromHostmap throws an error', () => { | ||
const options = { | ||
uri: 'http://example.com', | ||
}; | ||
|
||
webex.internal.services.replaceHostFromHostmap.throws(new Error('replaceHostFromHostmap error')); | ||
|
||
interceptor.onRequest(options); | ||
|
||
sinon.assert.calledWith( | ||
webex.internal.services.replaceHostFromHostmap, | ||
'http://example.com' | ||
); | ||
|
||
assert.equal(options.uri, 'http://example.com'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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