From 085731a620ff33e0347e2bbc7998a775044e0c69 Mon Sep 17 00:00:00 2001 From: wlbf Date: Sat, 3 Mar 2018 14:40:47 +0800 Subject: [PATCH] fix default option modifyed issue --- lib/defaultOptions.js | 20 ++++++++++++++++++++ lib/run.js | 23 +++-------------------- package.json | 1 + test/run.test.js | 15 +++++++++++++++ 4 files changed, 39 insertions(+), 20 deletions(-) create mode 100644 lib/defaultOptions.js diff --git a/lib/defaultOptions.js b/lib/defaultOptions.js new file mode 100644 index 00000000..4c1d40e1 --- /dev/null +++ b/lib/defaultOptions.js @@ -0,0 +1,20 @@ +const defaultOptions = { + headers: {}, + body: Buffer.alloc(0), + method: 'GET', + duration: 10, + connections: 10, + pipelining: 1, + timeout: 10, + maxConnectionRequests: 0, + maxOverallRequests: 0, + connectionRate: 0, + overallRate: 0, + amount: 0, + reconnectRate: 0, + forever: false, + idReplacement: false, + requests: [{}] +} + +module.exports = defaultOptions diff --git a/lib/run.js b/lib/run.js index b3f24124..bda58500 100644 --- a/lib/run.js +++ b/lib/run.js @@ -3,33 +3,16 @@ const EE = require('events').EventEmitter const URL = require('url') const hdr = require('hdr-histogram-js') +const clone = require('clone') const timestring = require('timestring') const Client = require('./httpClient') +const DefaultOptions = require('./defaultOptions') const xtend = require('xtend') const histUtil = require('hdr-histogram-percentiles-obj') const reInterval = require('reinterval') const histAsObj = histUtil.histAsObj const addPercentiles = histUtil.addPercentiles -const defaultOptions = { - headers: {}, - body: Buffer.alloc(0), - method: 'GET', - duration: 10, - connections: 10, - pipelining: 1, - timeout: 10, - maxConnectionRequests: 0, - maxOverallRequests: 0, - connectionRate: 0, - overallRate: 0, - amount: 0, - reconnectRate: 0, - forever: false, - idReplacement: false, - requests: [{}] -} - function run (opts, cb) { const cbPassedIn = (typeof cb === 'function') @@ -69,7 +52,7 @@ function run (opts, cb) { 0 // 5xx ] - opts = xtend(defaultOptions, opts) + opts = xtend(clone(DefaultOptions), opts) // do error checking, if error, return if (checkOptsForErrors()) return diff --git a/package.json b/package.json index 4d5bb195..519f1ebc 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ }, "dependencies": { "chalk": "^2.0.0", + "clone": "^2.1.1", "color-support": "^1.1.1", "deep-extend": "^0.5.0", "hdr-histogram-js": "^1.0.0", diff --git a/test/run.test.js b/test/run.test.js index e2c2a0ef..09367159 100644 --- a/test/run.test.js +++ b/test/run.test.js @@ -3,7 +3,9 @@ const os = require('os') const path = require('path') const test = require('tap').test +const clone = require('clone') const run = require('../lib/run') +const defaultOptions = require('../lib/defaultOptions') const helper = require('./helper') const server = helper.startServer() @@ -279,3 +281,16 @@ for (let i = 1; i <= 5; i++) { }) }) } + +test('run should not modify default options', (t) => { + const origin = clone(defaultOptions) + run({ + url: 'http://localhost:' + server.address().port, + connections: 2, + duration: 2 + }, function (err, result) { + t.error(err) + t.deepEqual(defaultOptions, origin, 'calling run function does not modify default options') + t.end() + }) +})