diff --git a/packages/@webex/http-core/src/request/index.js b/packages/@webex/http-core/src/request/index.js index d16992e23a4..0d6bfa29317 100644 --- a/packages/@webex/http-core/src/request/index.js +++ b/packages/@webex/http-core/src/request/index.js @@ -23,8 +23,17 @@ export default function request(options) { options.upload = new EventEmitter(); return intercept(options, options.interceptors, 'Request') - .then((...args) => _request(options, ...args)) - .then((...args) => - intercept(options, options.interceptors.slice().reverse(), 'Response', ...args) - ); + .then((...args) => { + // if provided own request function, use that instead + // there are use cases where developer may want to provide whatever request promise they wish + // for example, may even use window.postMessages for parent iframe uses cases + if (options.request) { + return options.request(options, ...args); + } + + return _request(options, ...args); + }) + .then((...args) => { + return intercept(options, options.interceptors.slice().reverse(), 'Response', ...args); + }); } diff --git a/packages/@webex/http-core/test/unit/spec/index.js b/packages/@webex/http-core/test/unit/spec/index.js index 8aab49b630d..c730a7ad361 100644 --- a/packages/@webex/http-core/test/unit/spec/index.js +++ b/packages/@webex/http-core/test/unit/spec/index.js @@ -1,7 +1,6 @@ import {assert} from '@webex/test-helper-chai'; import sinon from 'sinon'; -import * as utils from '@webex/http-core/src/request/utils'; import WebexTrackingIdInterceptor from '@webex/webex-core/src/interceptors/webex-tracking-id'; import UserAgentInterceptor from '@webex/webex-core/src/interceptors/webex-user-agent'; import {protoprepareFetchOptions, setTimingsAndFetch} from '@webex/http-core/src/index'; diff --git a/packages/@webex/http-core/test/unit/spec/request/index.js b/packages/@webex/http-core/test/unit/spec/request/index.js new file mode 100644 index 00000000000..d281c0e492f --- /dev/null +++ b/packages/@webex/http-core/test/unit/spec/request/index.js @@ -0,0 +1,58 @@ +import {assert} from '@webex/test-helper-chai'; +import sinon from 'sinon'; +import {EventEmitter} from 'events'; +import request from '@webex/http-core/src/request'; +import * as requestModule from '../../../../src/request/request'; +import * as utils from '../../../../src/request/utils'; + +describe('request', () => { + let interceptStub; + let requestStub; + + beforeEach(() => { + interceptStub = sinon.spy(utils, 'intercept'); + requestStub = sinon.stub(requestModule, 'default'); + }); + + afterEach(() => { + sinon.restore(); + }); + + it('should modify options and call _request if no custom request function is provided', async () => { + const options = { + url: 'http://example.com', + headers: {}, + interceptors: [], + }; + + requestStub.resolves('response'); + + const result = await request(options); + + assert.strictEqual(result, 'response'); + assert.strictEqual(options.uri, 'http://example.com'); + assert.isNull(options.url); + assert.deepEqual(options.headers, {}); + assert.instanceOf(options.download, EventEmitter); + assert.instanceOf(options.upload, EventEmitter); + assert.isTrue(interceptStub.calledTwice); + assert.isTrue(requestStub.calledOnceWith(options)); + }); + + it('should use custom request function if provided', async () => { + const customRequest = sinon.stub().resolves('custom response'); + const options = { + url: 'http://example.com', + headers: {}, + interceptors: [], + request: customRequest, + }; + + const result = await request(options); + + assert.strictEqual(result, 'custom response'); + assert.isTrue(customRequest.calledOnceWith(options)); + assert.isTrue(interceptStub.calledTwice); + assert.isFalse(requestStub.called); + }); +});