Skip to content

Commit

Permalink
Refactored request sending to no use JSON.stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
jtillmann committed Dec 14, 2021
1 parent cbf3a10 commit 2c94a69
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 25 deletions.
9 changes: 3 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 2 additions & 11 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -56,7 +47,7 @@ function request(path, method, data, headers = '', callback) {
debug('Request error', error);
});

req.write(postData);
req.write(body);

req.end();
}
Expand Down
15 changes: 7 additions & 8 deletions test/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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]));

Expand All @@ -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);

Expand All @@ -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();
});
Expand Down

0 comments on commit 2c94a69

Please sign in to comment.