-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Not use rewire for internal/private clone function
- Loading branch information
Emman
committed
Nov 11, 2020
1 parent
917bfec
commit f8cac13
Showing
2 changed files
with
52 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,58 @@ | ||
'use strict'; | ||
|
||
const rewire = require('rewire'); | ||
const _cloneOptions = rewire('../').__get__('_cloneOptions'); | ||
var request = require('../'); | ||
var t = require('chai').assert; | ||
const http = require('http'); | ||
const t = require('chai').assert; | ||
|
||
describe('Clone option function', function () { | ||
it('should not clone agent and non-own properties', function (done) { | ||
const options = Object.create({ parent: true }); | ||
options.cloneable = { a: 1 }; | ||
options.agent = new http.Agent({ keepAlive: true }); | ||
const cloned = _cloneOptions(options); | ||
t.strictEqual(options.agent, cloned.agent); | ||
t.notStrictEqual(options.cloneable, cloned.cloneable); | ||
t.equal(options.cloneable.a, cloned.cloneable.a) | ||
t.isUndefined(cloned.parent); | ||
t.isTrue(options.parent); | ||
done(); | ||
it('should not clone `options.agent`', function (done) { | ||
const agent = new http.Agent({ keepAlive: true }); | ||
const strategy = function (err, response, body, options) { | ||
options.url = 'http://www.filltext.com/?rows=1&err=200'; //overwrite url to return 200 | ||
t.strictEqual(agent, options.agent); | ||
return { | ||
mustRetry: true, | ||
options: options, | ||
}; | ||
}; | ||
|
||
request({ | ||
url: 'http://www.filltext.com/?rows=1&err=500', // returns a 500 status | ||
maxAttempts: 3, | ||
agent: agent, | ||
retryDelay: 100, | ||
retryStrategy: strategy | ||
}, function (err, response, body) { | ||
if (err) done(err); | ||
t.strictEqual(200, response.statusCode); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should not clone `non-own` property', function (done) { | ||
const originOptions = Object.create({ parent: true }); | ||
originOptions.url = 'http://www.filltext.com/?rows=1&err=500'; // returns a 500 status | ||
originOptions.maxAttempts = 3; | ||
originOptions.retryDelay = 100; | ||
originOptions.cloneable = { a: 1 }; | ||
|
||
const strategy = function (err, response, body, options) { | ||
t.isUndefined(options.parent); | ||
t.notStrictEqual(originOptions.cloneable, options.cloneable); | ||
t.equal(originOptions.cloneable.a, options.cloneable.a); | ||
options.url = 'http://www.filltext.com/?rows=1&err=200'; //overwrite url to return 200 | ||
return { | ||
mustRetry: true, | ||
options: options, | ||
}; | ||
}; | ||
|
||
originOptions.retryStrategy = strategy; | ||
|
||
request(originOptions, function (err, response, body) { | ||
if (err) done(err); | ||
t.strictEqual(200, response.statusCode); | ||
done(); | ||
}); | ||
}); | ||
}); |