Skip to content

Commit 95e7a3b

Browse files
committed
fix: breaking test suite
1 parent 42f7e79 commit 95e7a3b

File tree

2 files changed

+7019
-66
lines changed

2 files changed

+7019
-66
lines changed

index.js

+33-37
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,28 @@ function defaultPromiseFactory(resolver) {
2626
return new Promise(resolver);
2727
}
2828

29-
// Prevent Cookie & Authorization Headers from being forwarded
30-
// when the URL redirects to another domain (information leak) #137
29+
// Prevent Cookie & Authorization Headers from being forwarded
30+
// when the URL redirects to another domain (information leak) #137
3131
function sanitizeHeaders(options) {
32-
3332
const HEADERS_TO_IGNORE = ["cookie", "authorization"];
3433

35-
const urlObject = url.parse(options.url)
34+
const urlObject = url.parse(options.url || options.uri);
3635
const queryObject = querystring.parse(urlObject.query);
37-
38-
const hasExternalLink = Object.keys(queryObject).reduce(function(acc, cur) {
39-
40-
let qUrl = url.parse(queryObject[cur]);
36+
37+
const hasExternalLink = Object.keys(queryObject).some(function (queryParam) {
38+
const qUrl = url.parse(queryObject[queryParam]);
4139

4240
// external link if protocol || host || port is different
43-
if(!!qUrl.host && (qUrl.protocol !== urlObject.protocol || qUrl.host !== urlObject.host || qUrl.port !== urlObject.port) ) {
44-
acc = true;
45-
}
46-
47-
return acc;
41+
return !!qUrl.host && (qUrl.protocol !== urlObject.protocol || qUrl.host !== urlObject.host || qUrl.port !== urlObject.port);
42+
});
4843

49-
}, false);
44+
if (hasExternalLink && options.hasOwnProperty("headers") && typeof (options.headers) === "object") {
5045

51-
if (hasExternalLink && options.hasOwnProperty("headers") && typeof(options.headers) === "object") {
52-
5346
// if External Link: remove Cookie and Authorization from Headers
54-
Object.keys(options.headers).filter(function(key) {
55-
return HEADERS_TO_IGNORE.includes(key.toLowerCase())
56-
}).map(function(key) {
57-
return delete options.headers[key]
47+
Object.keys(options.headers).filter(function (key) {
48+
return HEADERS_TO_IGNORE.includes(key.toLowerCase());
49+
}).map(function (key) {
50+
return delete options.headers[key];
5851
});
5952

6053
}
@@ -93,22 +86,22 @@ function makePromise(requestInstance, promiseFactoryFn) {
9386

9487
function Request(url, options, f, retryConfig) {
9588
// ('url')
96-
if(_.isString(url)){
89+
if (_.isString(url)) {
9790
// ('url', f)
98-
if(_.isFunction(options)){
91+
if (_.isFunction(options)) {
9992
f = options;
10093
}
10194

102-
if(!_.isObject(options)){
95+
if (!_.isObject(options)) {
10396
options = {};
10497
}
10598

10699
// ('url', {object})
107100
options.url = url;
108101
}
109102

110-
if(_.isObject(url)){
111-
if(_.isFunction(options)){
103+
if (_.isObject(url)) {
104+
if (_.isFunction(options)) {
112105
f = options;
113106
}
114107
options = url;
@@ -123,7 +116,8 @@ function Request(url, options, f, retryConfig) {
123116
* Option object
124117
* @type {Object}
125118
*/
126-
this.options = sanitizeHeaders(options);
119+
// this.options = sanitizeHeaders(options);
120+
this.options = options;
127121

128122
/**
129123
* Return true if the request should be retried
@@ -135,7 +129,9 @@ function Request(url, options, f, retryConfig) {
135129
* Return a number representing how long request-retry should wait before trying again the request
136130
* @type {Boolean} (err, response, body) -> Number
137131
*/
138-
this.delayStrategy = _.isFunction(options.delayStrategy) ? options.delayStrategy : function() { return this.retryDelay; };
132+
this.delayStrategy = _.isFunction(options.delayStrategy) ? options.delayStrategy : function () {
133+
return this.retryDelay;
134+
};
139135

140136
this._timeout = null;
141137
this._req = null;
@@ -204,14 +200,14 @@ Request.prototype.abort = function () {
204200

205201
// expose request methods from RequestRetry
206202
['end', 'on', 'emit', 'once', 'setMaxListeners', 'start', 'removeListener', 'pipe', 'write', 'auth'].forEach(function (requestMethod) {
207-
Request.prototype[requestMethod] = function exposedRequestMethod () {
203+
Request.prototype[requestMethod] = function exposedRequestMethod() {
208204
return this._req[requestMethod].apply(this._req, arguments);
209205
};
210206
});
211207

212208
// expose promise methods
213209
['then', 'catch', 'finally', 'fail', 'done'].forEach(function (promiseMethod) {
214-
Request.prototype[promiseMethod] = function exposedPromiseMethod () {
210+
Request.prototype[promiseMethod] = function exposedPromiseMethod() {
215211
if (this._callback) {
216212
throw new Error('A callback was provided but waiting a promise, use only one pattern');
217213
}
@@ -230,22 +226,22 @@ function Factory(url, options, f) {
230226
function makeHelper(obj, verb) {
231227
obj[verb] = function helper(url, options, f) {
232228
// ('url')
233-
if(_.isString(url)){
229+
if (_.isString(url)) {
234230
// ('url', f)
235-
if(_.isFunction(options)){
231+
if (_.isFunction(options)) {
236232
f = options;
237233
}
238234

239-
if(!_.isObject(options)){
235+
if (!_.isObject(options)) {
240236
options = {};
241237
}
242238

243239
// ('url', {object})
244240
options.url = url;
245241
}
246242

247-
if(_.isObject(url)){
248-
if(_.isFunction(options)){
243+
if (_.isObject(url)) {
244+
if (_.isFunction(options)) {
249245
f = options;
250246
}
251247
options = url;
@@ -259,13 +255,13 @@ function makeHelper(obj, verb) {
259255
function defaults(defaultOptions, defaultF) {
260256
var factory = function (options, f) {
261257
if (typeof options === "string") {
262-
options = { uri: options };
258+
options = {uri: options};
263259
}
264-
return Factory.apply(null, [ extend(true, {}, defaultOptions, options), f || defaultF ]);
260+
return Factory.apply(null, [extend(true, {}, defaultOptions, options), f || defaultF]);
265261
};
266262

267263
factory.defaults = function (newDefaultOptions, newDefaultF) {
268-
return defaults.apply(null, [ extend(true, {}, defaultOptions, newDefaultOptions), newDefaultF || defaultF ]);
264+
return defaults.apply(null, [extend(true, {}, defaultOptions, newDefaultOptions), newDefaultF || defaultF]);
269265
};
270266

271267
factory.Request = Request;

0 commit comments

Comments
 (0)