diff --git a/lib/index.js b/lib/index.js index 6bd9230..dfa5e73 100644 --- a/lib/index.js +++ b/lib/index.js @@ -435,16 +435,13 @@ Visitor.prototype = { var useBatchPath = params.length > 1; - var path = config.hostname + (useBatchPath ? config.batchPath :config.path); + var path = config.hostname + (useBatchPath ? config.batchPath : config.path); debug("%d: %o", count++, params); - var options = Object.assign({}, self.options.requestOptions, { - body: getBody(params), - headers: self.options.headers || {} - }); + var body = getBody(params); - request.post(path, options, nextIteration); + request.post(path, body, self.options.headers, nextIteration); } function nextIteration(err) { diff --git a/lib/request.js b/lib/request.js index 45d635b..a915ee0 100644 --- a/lib/request.js +++ b/lib/request.js @@ -28,16 +28,7 @@ function post(path, data, headers, callback) { * @param data a JSON Object or a string * @param method is the protocol used like POST GET DELETE PUT etc... */ -function request(path, method, data, headers = '', callback) { - if (typeof data === 'function') { - callback = data; - data = ''; - } else if (typeof headers === 'function') { - callback = headers; - headers = {}; - } - - const postData = typeof data === "object" ? JSON.stringify(data) : data; +function request(path, method, body, headers = {}, callback) { const { hostname, port, pathname } = url.parse(path); const options = { hostname, @@ -56,7 +47,7 @@ function request(path, method, data, headers = '', callback) { debug('Request error', error); }); - req.write(postData); + req.write(body); req.end(); } diff --git a/test/send.js b/test/send.js index 4efb11f..2c8081c 100644 --- a/test/send.js +++ b/test/send.js @@ -17,7 +17,7 @@ describe("ua", function () { var post; beforeEach(function () { - post = sinon.stub(request, "post").callsArg(2); + post = sinon.stub(request, "post").callsArg(3); }); afterEach(function () { @@ -57,7 +57,7 @@ describe("ua", function () { Math.random(); // I have absolutely no idea why it fails unless there was some processing to be done after url.parseā€¦ (parsedUrl.protocol + "//" + parsedUrl.host).should.equal(config.hostname); - args[1].body.should.equal(qs.stringify(params)); + args[1].should.equal(qs.stringify(params)); } done(); @@ -147,7 +147,7 @@ describe("ua", function () { var args = post.args[0]; var params = paramSets; - var formParams = args[1].body.split("\n"); + var formParams = args[1].split("\n"); formParams.should.have.lengthOf(3); formParams[0].should.equal(qs.stringify(params[0])); @@ -172,7 +172,7 @@ describe("ua", function () { fn.args[0].should.eql([null, 2], "no error, 2 requests"); - var body = post.args[0][1].body; + var body = post.args[0][1]; body.split("\n").should.have.lengthOf(2); @@ -198,13 +198,12 @@ describe("ua", function () { post.calledOnce.should.equal(true, "request should have been POSTed"); var parsedUrl = url.parse(post.args[0][0]); - var options = post.args[0][1]; + var headers = post.args[0][2]; (parsedUrl.protocol + "//" + parsedUrl.host).should.equal(config.hostname); - options.should.have.keys("headers","body") - options.headers.should.have.key("User-Agent"); - options.headers["User-Agent"].should.equal("Test User Agent"); + headers.should.have.key("User-Agent"); + headers["User-Agent"].should.equal("Test User Agent"); done(); });