Skip to content

Commit d427999

Browse files
Merge pull request #140 from splunk/DVPL-9898
Support added for output_mode for oneShotSearch
2 parents 2dc3db3 + 9206171 commit d427999

File tree

3 files changed

+82
-7
lines changed

3 files changed

+82
-7
lines changed

lib/http.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@
3030
var post = message.post || {};
3131
var outputMode = query.output_mode || post.output_mode || "json";
3232

33-
// If the output mode doesn't start with "json" (e.g. "csv" or
34-
// "xml"), we change it to "json".
35-
if (!utils.startsWith(outputMode, "json")) {
36-
outputMode = "json";
37-
}
38-
3933
query.output_mode = outputMode;
4034

4135
return query;
@@ -253,6 +247,7 @@
253247
method: message.method,
254248
headers: message.headers,
255249
timeout: message.timeout,
250+
query: message.query,
256251
body: body
257252
};
258253

lib/platform/node/node_http.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
// Get the byte-length of the content, which adjusts for multi-byte characters
4242
request_options.headers["Content-Length"] = Buffer.byteLength(request_options.body, "utf8");
4343

44+
if(message.query && ["xml", "csv"].includes(message.query.output_mode)){
45+
request_options.parse_response = false;
46+
}
47+
4448
var that = this;
4549
var req = needle.request(request_options.method, request_options.url, request_options.body, request_options,
4650
function (error, res, data)
@@ -55,7 +59,15 @@
5559
statusCode: res ? res.statusCode : 600
5660
};
5761

58-
var complete_response = that._buildResponse(error, response, JSON.stringify(data));
62+
var complete_response;
63+
64+
if(message.query && ["xml", "csv"].includes(message.query.output_mode)){
65+
complete_response = that._buildResponse(error, response, data);
66+
}
67+
else {
68+
complete_response = that._buildResponse(error, response, JSON.stringify(data));
69+
}
70+
5971
callback(complete_response);
6072
});
6173

tests/service_tests/job.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,74 @@ exports.setup = function (svc) {
615615
);
616616
});
617617

618+
it("Callback#Oneshot search with json results", function (done) {
619+
var sid = getNextId();
620+
var that = this;
621+
622+
Async.chain([
623+
function (done) {
624+
that.service.jobs().oneshotSearch('search index=_internal | head 1 | stats count', { id: sid, output_mode: 'json' }, done);
625+
},
626+
function (results, done) {
627+
assert.ok(results);
628+
assert.ok(results.fields);
629+
assert.strictEqual(results.fields.length, 1);
630+
done();
631+
}
632+
],
633+
function (err) {
634+
assert.ok(!err);
635+
done();
636+
}
637+
);
638+
});
639+
640+
it("Callback#Oneshot search with xml results", function (done) {
641+
var sid = getNextId();
642+
var that = this;
643+
644+
Async.chain([
645+
function (done) {
646+
that.service.jobs().oneshotSearch('search index=_internal | head 2 | stats count', { id: sid, output_mode: 'xml' }, done);
647+
},
648+
function (results, done) {
649+
assert.ok(results);
650+
assert.ok(results.includes('<field>count</field>'));
651+
assert.ok(results.includes('<value><text>2</text></value>'));
652+
done();
653+
}
654+
],
655+
function (err) {
656+
assert.ok(!err);
657+
done();
658+
}
659+
);
660+
});
661+
662+
it("Callback#Oneshot search with csv results", function (done) {
663+
var sid = getNextId();
664+
var that = this;
665+
666+
Async.chain([
667+
function (done) {
668+
that.service.jobs().oneshotSearch('makeresults count=3 | streamstats count | eval foo="bar" | fields - _time', { id: sid, output_mode: 'csv' }, done);
669+
},
670+
function (results, done) {
671+
assert.ok(results);
672+
assert.ok(results.includes('count,foo'));
673+
assert.ok(results.includes('1,bar'));
674+
assert.ok(results.includes('2,bar'));
675+
assert.ok(results.includes('3,bar'));
676+
done();
677+
}
678+
],
679+
function (err) {
680+
assert.ok(!err);
681+
done();
682+
}
683+
);
684+
});
685+
618686
it("Callback#Oneshot search with no results", function (done) {
619687
var sid = getNextId();
620688
var that = this;

0 commit comments

Comments
 (0)