From 07a6fdd1b4b45c142e7bb722d39cd7242b11b405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20D=C3=B6brei?= Date: Mon, 27 Jul 2015 12:57:42 +0200 Subject: [PATCH] feat(trace-reporter): use JSON to send data to Trace Change content-type to JSON Update tests Catch error on invalid server response --- lib/reporters/trace.js | 19 +++++++++++++------ lib/reporters/trace.spec.js | 11 ++++++----- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/reporters/trace.js b/lib/reporters/trace.js index e20b030..75c61c6 100644 --- a/lib/reporters/trace.js +++ b/lib/reporters/trace.js @@ -1,6 +1,5 @@ var http = require('http'); var url = require('url'); -var qs = require('qs'); var config = require('../config'); @@ -31,7 +30,8 @@ TraceReporter.prototype.send = function (data, callback) { path: opts.path, method: 'POST', headers: { - 'Authorization': 'Bearer ' + this.apiKey + 'Authorization': 'Bearer ' + this.apiKey, + 'Content-Type': 'application/json' } }, function () { return callback(null); @@ -40,7 +40,7 @@ TraceReporter.prototype.send = function (data, callback) { return callback(err); }); - req.write(qs.stringify(data)); + req.write(JSON.stringify(data)); req.end(); }; @@ -55,12 +55,19 @@ TraceReporter.prototype.getService = function(callback) { path: opts.path, method: 'POST', headers: { - 'Authorization': 'Bearer ' + this.apiKey + 'Authorization': 'Bearer ' + this.apiKey, + 'Content-Type': 'application/json' } }, function (res) { res.setEncoding('utf8'); res.on('data', function (chunk) { - return callback(null, JSON.parse(chunk)); + var res; + try { + res = JSON.parse(chunk); + } catch (ex) { + return callback(ex); + } + return callback(null, res); }); }) .on('error', function (err) { @@ -71,7 +78,7 @@ TraceReporter.prototype.getService = function(callback) { } }); - req.write(qs.stringify({ + req.write(JSON.stringify({ name: _this.appName })); req.end(); diff --git a/lib/reporters/trace.spec.js b/lib/reporters/trace.spec.js index ff077d7..dd490fe 100644 --- a/lib/reporters/trace.spec.js +++ b/lib/reporters/trace.spec.js @@ -2,7 +2,6 @@ var TraceReporter = require('./trace'); var expect = require('chai').expect; var nock = require('nock'); -var qs = require('qs'); var config = require('../config'); @@ -65,10 +64,11 @@ describe('The Trace reporter module', function () { nock(collectorApi, { reqheaders: { - 'Authorization': 'Bearer testApiKey' + 'Authorization': 'Bearer testApiKey', + 'Content-Type': 'application/json' } }) - .post(collectorApiSampleEndpoint, qs.stringify(data)) + .post(collectorApiSampleEndpoint, JSON.stringify(data)) .reply(201); var traceReporter = TraceReporter.create(options); @@ -91,10 +91,11 @@ describe('The Trace reporter module', function () { nock(collectorApi, { reqheaders: { - 'Authorization': 'Bearer testApiKey' + 'Authorization': 'Bearer testApiKey', + 'Content-Type': 'application/json' } }) - .post(collectorApiServiceEndpoint, qs.stringify(data)) + .post(collectorApiServiceEndpoint, JSON.stringify(data)) .reply(201, { key: 1 });