Skip to content

Commit

Permalink
Not use rewire for internal/private clone function
Browse files Browse the repository at this point in the history
  • Loading branch information
Emman committed Nov 11, 2020
1 parent 917bfec commit f8cac13
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -64,7 +64,6 @@
"request": "^2.88.0",
"rsvp": "^4.8.4",
"sinon": "^6.1.4",
"updtr": "^3.1.0",
"rewire": "^5.0.0"
"updtr": "^3.1.0"
}
}
65 changes: 51 additions & 14 deletions test/clone.test.js
@@ -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();
});
});
});

0 comments on commit f8cac13

Please sign in to comment.