Skip to content

Commit

Permalink
feat(core): if provided options.request use instead (#3944)
Browse files Browse the repository at this point in the history
Co-authored-by: rstachof <[email protected]>
  • Loading branch information
robstax and rstachof authored Nov 2, 2024
1 parent a807933 commit 1715035
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
17 changes: 13 additions & 4 deletions packages/@webex/http-core/src/request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
1 change: 0 additions & 1 deletion packages/@webex/http-core/test/unit/spec/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
58 changes: 58 additions & 0 deletions packages/@webex/http-core/test/unit/spec/request/index.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 1715035

Please sign in to comment.