Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@
var post = message.post || {};
var outputMode = query.output_mode || post.output_mode || "json";

// If the output mode doesn't start with "json" (e.g. "csv" or
// "xml"), we change it to "json".
if (!utils.startsWith(outputMode, "json")) {
outputMode = "json";
}

query.output_mode = outputMode;

return query;
Expand Down Expand Up @@ -253,6 +247,7 @@
method: message.method,
headers: message.headers,
timeout: message.timeout,
query: message.query,
body: body
};

Expand Down
14 changes: 13 additions & 1 deletion lib/platform/node/node_http.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
// Get the byte-length of the content, which adjusts for multi-byte characters
request_options.headers["Content-Length"] = Buffer.byteLength(request_options.body, "utf8");

if(message.query && ["xml", "csv"].includes(message.query.output_mode)){
request_options.parse_response = false;
}

var that = this;
var req = needle.request(request_options.method, request_options.url, request_options.body, request_options,
function (error, res, data)
Expand All @@ -55,7 +59,15 @@
statusCode: res ? res.statusCode : 600
};

var complete_response = that._buildResponse(error, response, JSON.stringify(data));
var complete_response;

if(message.query && ["xml", "csv"].includes(message.query.output_mode)){
complete_response = that._buildResponse(error, response, data);
}
else {
complete_response = that._buildResponse(error, response, JSON.stringify(data));
}

callback(complete_response);
});

Expand Down
68 changes: 68 additions & 0 deletions tests/service_tests/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,74 @@ exports.setup = function (svc) {
);
});

it("Callback#Oneshot search with json results", function (done) {
var sid = getNextId();
var that = this;

Async.chain([
function (done) {
that.service.jobs().oneshotSearch('search index=_internal | head 1 | stats count', { id: sid, output_mode: 'json' }, done);
},
function (results, done) {
assert.ok(results);
assert.ok(results.fields);
assert.strictEqual(results.fields.length, 1);
done();
}
],
function (err) {
assert.ok(!err);
done();
}
);
});

it("Callback#Oneshot search with xml results", function (done) {
var sid = getNextId();
var that = this;

Async.chain([
function (done) {
that.service.jobs().oneshotSearch('search index=_internal | head 2 | stats count', { id: sid, output_mode: 'xml' }, done);
},
function (results, done) {
assert.ok(results);
assert.ok(results.includes('<field>count</field>'));
assert.ok(results.includes('<value><text>2</text></value>'));
done();
}
],
function (err) {
assert.ok(!err);
done();
}
);
});

it("Callback#Oneshot search with csv results", function (done) {
var sid = getNextId();
var that = this;

Async.chain([
function (done) {
that.service.jobs().oneshotSearch('makeresults count=3 | streamstats count | eval foo="bar" | fields - _time', { id: sid, output_mode: 'csv' }, done);
},
function (results, done) {
assert.ok(results);
assert.ok(results.includes('count,foo'));
assert.ok(results.includes('1,bar'));
assert.ok(results.includes('2,bar'));
assert.ok(results.includes('3,bar'));
done();
}
],
function (err) {
assert.ok(!err);
done();
}
);
});

it("Callback#Oneshot search with no results", function (done) {
var sid = getNextId();
var that = this;
Expand Down