Skip to content

Commit 2cc511c

Browse files
author
Umit Coskun Aydinoglu
committed
rejectUnauthorized option is fixed
1 parent f2c5f27 commit 2cc511c

File tree

2 files changed

+103
-10
lines changed

2 files changed

+103
-10
lines changed

api/utils/countly-request/index.js

100644100755
+33-10
Original file line numberDiff line numberDiff line change
@@ -42,41 +42,62 @@ var convertOptionsToGot = function(options) {
4242

4343
var requestOptions = {};
4444

45-
//define for got and request differences
45+
// Define for got and request differences
4646
var keyMap = {
4747
"qs": "searchParams",
48-
"strictSSL": "rejectUnauthorized",
48+
"strictSSL": "https.rejectUnauthorized",
4949
"gzip": "decompress",
5050
"jar": "cookieJar",
5151
"baseUrl": "prefixUrl",
5252
"uri": "url"
5353
};
5454

55+
// Helper function to assign value to nested keys
56+
function assignDeep(obj, keyPath, value) {
57+
var keys = keyPath.split('.');
58+
var lastKey = keys.pop();
59+
var nestedObj = obj;
60+
61+
keys.forEach(function(key) {
62+
if (!nestedObj[key] || typeof nestedObj[key] !== 'object') {
63+
nestedObj[key] = {};
64+
}
65+
nestedObj = nestedObj[key];
66+
});
67+
68+
nestedObj[lastKey] = value;
69+
}
70+
5571
for (let key in options) {
5672
if (!Object.prototype.hasOwnProperty.call(requestOptions, key) && keyMap[key]) {
57-
requestOptions[keyMap[key]] = options[key];
58-
}
59-
else {
73+
var mappedKey = keyMap[key];
74+
if (mappedKey.includes('.')) {
75+
assignDeep(requestOptions, mappedKey, options[key]);
76+
} else {
77+
requestOptions[mappedKey] = options[key];
78+
}
79+
} else {
6080
requestOptions[key] = options[key];
6181
}
6282
}
6383

64-
//backward compatability. in got json is not boolean. it is the object.
65-
//request body and json are mutally exclusive. if request.json and body exists one of them must be deleted
84+
// Backward compatibility: in got, json is not a boolean, it is an object.
85+
// Request body and json are mutually exclusive.
86+
// If request.json and body exist, one of them must be deleted.
6687
if (requestOptions.json && typeof requestOptions.json === 'boolean' && requestOptions.body) {
6788
requestOptions.json = requestOptions.body;
68-
delete requestOptions.json;
89+
delete requestOptions.body;
6990
}
7091

7192
if (requestOptions.prefixUrl && options.uri && requestOptions.url) {
7293
requestOptions.uri = options.uri;
7394
delete requestOptions.url;
74-
7595
}
7696

7797
return requestOptions;
7898
};
7999

100+
80101
module.exports = function(uri, options, callback) {
81102

82103
if (typeof uri === 'undefined') {
@@ -144,4 +165,6 @@ module.exports.post = function(uri, options, callback) {
144165
//Add a get method to the request object
145166
module.exports.get = function(uri, options, callback) {
146167
module.exports(uri, options, callback);
147-
};
168+
};
169+
170+
module.exports.convertOptionsToGot = convertOptionsToGot;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var request = require('../index.js');
2+
var should = require('should');
3+
4+
describe('Countly Request', () => {
5+
6+
it('Make sure options converted correctly', () => {
7+
const optionsWithoutBaseURL ={
8+
"qs": "searchParams",
9+
"strictSSL": true ,
10+
"gzip": true,
11+
"jar": "cookieJar",
12+
//"baseUrl": "prefixUrl",
13+
"uri": "url"
14+
};
15+
16+
const expectedOptionsWithoutBaseURL = {
17+
"searchParams": "searchParams",
18+
"https": {
19+
"rejectUnauthorized": true
20+
},
21+
"decompress": true,
22+
"cookieJar": "cookieJar",
23+
//"prefixUrl": "prefixUrl",
24+
"url": "url"
25+
};
26+
27+
const optionsWithBaseURL ={
28+
"qs": "searchParams",
29+
"strictSSL": true ,
30+
"gzip": true,
31+
"jar": "cookieJar",
32+
"baseUrl": "prefixUrl",
33+
"uri": "url"
34+
};
35+
36+
const expetedOptionsWithBaseURL ={
37+
"searchParams": "searchParams",
38+
"https": {
39+
"rejectUnauthorized": true
40+
},
41+
"decompress": true,
42+
"cookieJar": "cookieJar",
43+
"prefixUrl": "prefixUrl",
44+
"uri": "url"
45+
};
46+
47+
48+
request.convertOptionsToGot(optionsWithoutBaseURL).should.be.eql(expectedOptionsWithoutBaseURL);
49+
request.convertOptionsToGot(optionsWithBaseURL).should.be.eql(expetedOptionsWithBaseURL);
50+
});
51+
52+
53+
it('Make get request', () => {
54+
request.get('https://count.ly', {strictSSL:true}, function(err, res/*, body*/) {
55+
should.not.exist(err);
56+
should.exist(res);
57+
58+
});
59+
60+
});
61+
62+
it('Make post request', () => {
63+
request.post('https://countly',function(err, res/*, body*/) {
64+
should.not.exist(err);
65+
should.exist(res);
66+
67+
});
68+
});
69+
70+
});

0 commit comments

Comments
 (0)