@webex/http-core 2.59.8 → 2.60.0-next.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,78 @@
1
+ /* eslint-disable import/prefer-default-export */
2
+ import {EventEmitter} from 'events';
3
+ import Interceptor from '../lib/interceptor';
4
+
5
+ /**
6
+ * @param {Object} options
7
+ * @param {Array<Object>} interceptors
8
+ * @param {string} key
9
+ * @param {Object | undefined} res
10
+ * @private
11
+ * @returns {Promise}
12
+ */
13
+ export function intercept(
14
+ options: object,
15
+ interceptors: Array<Interceptor>,
16
+ key: string,
17
+ res: object = undefined
18
+ ): Promise<any> {
19
+ const successKey = `on${key}`;
20
+ const errorKey = `on${key}Error`;
21
+
22
+ return interceptors.reduce(
23
+ (promise, interceptor) =>
24
+ promise.then(
25
+ (result) => {
26
+ interceptor.logOptions(options);
27
+ if (interceptor[successKey]) {
28
+ return interceptor[successKey](options, result);
29
+ }
30
+
31
+ return Promise.resolve(result);
32
+ },
33
+ (reason) => {
34
+ interceptor.logOptions(options);
35
+ if (interceptor[errorKey]) {
36
+ return interceptor[errorKey](options, reason);
37
+ }
38
+
39
+ return Promise.reject(reason);
40
+ }
41
+ ),
42
+ Promise.resolve(res)
43
+ );
44
+ }
45
+
46
+ /**
47
+ * Prepare options for a fetch.
48
+ * @param {object} options
49
+ * @returns {Promise}
50
+ */
51
+ export async function prepareFetchOptions(options: any): Promise<any> {
52
+ if (options.url) {
53
+ options.uri = options.url;
54
+ options.url = null;
55
+ }
56
+
57
+ options.headers = options.headers || {};
58
+
59
+ if (options.json) {
60
+ // don't override existing accept header declared by user
61
+ options.headers.accept = options.headers.accept || options.headers.Accept || 'application/json';
62
+
63
+ // don't override existing content-type header declared by user
64
+ if (options.method !== 'GET' && options.method !== 'HEAD') {
65
+ options.headers['content-type'] =
66
+ options.headers['content-type'] || options.headers['Content-Type'] || 'application/json';
67
+ options.body = JSON.stringify(options.json === true ? options.body : options.json);
68
+ }
69
+ } else if (options.json !== undefined) {
70
+ Reflect.deleteProperty(options, 'json');
71
+ }
72
+
73
+ options.download = new EventEmitter();
74
+ options.upload = new EventEmitter();
75
+ options.keepalive = true;
76
+
77
+ return intercept(options, options.interceptors, 'Request').then(() => options);
78
+ }
@@ -78,7 +78,8 @@ describe('http-core', function () {
78
78
  }));
79
79
  });
80
80
 
81
- it('makes CORS compatible calls', () =>
81
+ // SPARK-413317
82
+ it.skip('makes CORS compatible calls', () =>
82
83
  request({
83
84
  uri: 'https://ds.ciscospark.com/v1/region/',
84
85
  }).then((res) => {
@@ -0,0 +1,58 @@
1
+ import {assert} from '@webex/test-helper-chai';
2
+ import sinon from 'sinon';
3
+
4
+ import * as utils from '@webex/http-core/src/request/utils';
5
+ import WebexTrackingIdInterceptor from '@webex/webex-core/src/interceptors/webex-tracking-id';
6
+ import UserAgentInterceptor from '@webex/webex-core/src/interceptors/webex-user-agent';
7
+ import {protoprepareFetchOptions, setTimingsAndFetch} from '@webex/http-core/src/index';
8
+
9
+ describe('http-core index tests', () => {
10
+ describe('#protoprepareFetchOptions()', () => {
11
+ it('uses default options and adds expected options', async () => {
12
+ const defaultOptions = {
13
+ interceptors: [WebexTrackingIdInterceptor.create(), UserAgentInterceptor.create()],
14
+ };
15
+ const options = {};
16
+
17
+ const prepareFetchOptions = protoprepareFetchOptions(defaultOptions);
18
+
19
+ await prepareFetchOptions(options);
20
+
21
+ assert.deepEqual(options, {
22
+ headers: {
23
+ trackingid: 'undefined_1',
24
+ 'spark-user-agent': 'webex-js-sdk/development (node)',
25
+ },
26
+ keepalive: true,
27
+ });
28
+
29
+ assert.equal(typeof options.logger, 'object');
30
+ assert.equal(typeof options.upload, 'object');
31
+ assert.equal(typeof options.download, 'object');
32
+ assert.isArray(options.interceptors);
33
+ });
34
+ });
35
+
36
+ describe('#setTimingsAndFetch()', () => {
37
+ const now = Date.now();
38
+ let stubbedFetch;
39
+
40
+ beforeEach(() => {
41
+ global.fetch = sinon.stub();
42
+ });
43
+
44
+ it('calls fetch with expected options', async () => {
45
+ sinon.useFakeTimers(now);
46
+ const options = {uri: 'foo'};
47
+
48
+ const newOptions = setTimingsAndFetch(options);
49
+
50
+ sinon.assert.calledOnce(global.fetch);
51
+ sinon.assert.calledWith(global.fetch, 'foo', {
52
+ uri: 'foo',
53
+ $timings: {requestStart: now, networkStart: now},
54
+ });
55
+ sinon.restore();
56
+ });
57
+ });
58
+ });
@@ -0,0 +1,23 @@
1
+ import {assert} from '@webex/test-helper-chai';
2
+ import window from 'global/window';
3
+ import request from '@webex/http-core/src/request/request.shim';
4
+ import {EventEmitter} from 'events';
5
+
6
+ describe('Request shim', () => {
7
+ describe('#setAuth()', () => {
8
+ it('sets auth header', () => {
9
+
10
+ class DummyXMLHttpRequest {
11
+ upload = new EventEmitter();
12
+ }
13
+
14
+ window.XMLHttpRequest = DummyXMLHttpRequest;
15
+
16
+ const options = {upload: new EventEmitter(), headers: [], method: 'post', ...options, auth: {user: 'test', pass: 'pw'}};
17
+
18
+ request(options);
19
+
20
+ assert.equal(options.headers.Authorization, 'Basic dGVzdDpwdw==');
21
+ });
22
+ });
23
+ });
@@ -0,0 +1,77 @@
1
+ import {assert} from '@webex/test-helper-chai';
2
+ import sinon from 'sinon';
3
+
4
+ import * as utils from '@webex/http-core/src/request/utils';
5
+ import WebexTrackingIdInterceptor from '@webex/webex-core/src/interceptors/webex-tracking-id';
6
+ import UserAgentInterceptor from '@webex/webex-core/src/interceptors/webex-user-agent';
7
+
8
+ describe('Request utils', () => {
9
+ describe('#intercept()', () => {
10
+ it('updates options from interceptors', async () => {
11
+ const options = {};
12
+ const interceptors = [WebexTrackingIdInterceptor.create(), UserAgentInterceptor.create()];
13
+
14
+ return utils.intercept(options, interceptors, 'Request').then(() => {
15
+ assert.equal(Object.keys(options.headers).length, 2);
16
+ assert.equal(options.headers.trackingid, 'undefined_1');
17
+ assert.equal(options.headers['spark-user-agent'], 'webex-js-sdk/development (node)');
18
+ });
19
+ });
20
+ });
21
+
22
+ describe('#prepareFetchOptions()', () => {
23
+ it('updates options as expected', async () => {
24
+ const options = {
25
+ json: true,
26
+ body: {foo: 'bar'},
27
+ headers: {},
28
+ interceptors: [WebexTrackingIdInterceptor.create(), UserAgentInterceptor.create()],
29
+ };
30
+
31
+ return utils.prepareFetchOptions(options).then(() => {
32
+ assert.deepEqual(options.headers, {
33
+ accept: 'application/json',
34
+ 'content-type': 'application/json',
35
+ trackingid: 'undefined_1',
36
+ 'spark-user-agent': 'webex-js-sdk/development (node)',
37
+ });
38
+
39
+ assert.equal(options.body, '{"foo":"bar"}');
40
+
41
+ assert.equal(options.download != undefined, true);
42
+ assert.equal(options.upload != undefined, true);
43
+ assert.equal(options.keepalive, true);
44
+ });
45
+ });
46
+
47
+ it('updates options as expected when accept and content-type exist', async () => {
48
+ const options = {
49
+ json: true,
50
+ body: {foo: 'bar'},
51
+ headers: {accept: 'foo', 'content-type': 'bar'},
52
+ interceptors: [WebexTrackingIdInterceptor.create(), UserAgentInterceptor.create()],
53
+ };
54
+
55
+ return utils.prepareFetchOptions(options).then(() => {
56
+ assert.deepEqual(options.headers, {
57
+ accept: 'foo',
58
+ 'content-type': 'bar',
59
+ trackingid: 'undefined_1',
60
+ 'spark-user-agent': 'webex-js-sdk/development (node)',
61
+ });
62
+ });
63
+ });
64
+
65
+ it('updates body as expected when json = some object', async () => {
66
+ const options = {
67
+ json: {bar: 'baz'},
68
+ headers: {accept: 'foo', 'content-type': 'bar'},
69
+ interceptors: [WebexTrackingIdInterceptor.create(), UserAgentInterceptor.create()],
70
+ };
71
+
72
+ return utils.prepareFetchOptions(options).then(() => {
73
+ assert.equal(options.body, '{"bar":"baz"}');
74
+ });
75
+ });
76
+ });
77
+ });