From f8cac1373fa927cf4045d2f55b147466b9213fff Mon Sep 17 00:00:00 2001 From: Emman Date: Wed, 11 Nov 2020 08:45:59 +0800 Subject: [PATCH] Not use rewire for internal/private clone function --- package.json | 3 +-- test/clone.test.js | 65 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index e78a986..df0e830 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/clone.test.js b/test/clone.test.js index ab51137..89c42a7 100644 --- a/test/clone.test.js +++ b/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(); + }); }); });