From 99f3c477938f7d27afafe64caa32bb4c4285fb6c Mon Sep 17 00:00:00 2001 From: Michael Nisi Date: Fri, 19 Feb 2016 17:56:18 +0100 Subject: [PATCH] Check body length to prevent empty lines --- bin/bunyan | 3 ++- test/cli.test.js | 28 ++++++++++++++++++++++++++++ test/corpus/content-length-0-res.log | 3 +++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/corpus/content-length-0-res.log diff --git a/bin/bunyan b/bin/bunyan index 815bbb8..49a24e6 100755 --- a/bin/bunyan +++ b/bin/bunyan @@ -972,8 +972,9 @@ function emitRecord(rec, line, opts, stylize) { function (h) { return h + ': ' + headers[h]; }).join('\n'); } if (res.body !== undefined) { - s += '\n\n' + (typeof (res.body) === 'object' + var body = (typeof (res.body) === 'object' ? JSON.stringify(res.body, null, 2) : res.body); + if (body.length > 0) { s += '\n\n' + body }; delete res.body; } else { s = s.trimRight(); diff --git a/test/cli.test.js b/test/cli.test.js index 2362d9f..99ba9b9 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -502,3 +502,31 @@ test('client_req extra newlines, client_res={} (pull #252)', function (t) { t.end(); }); }); + +test('should only show nonempty response bodies', function (t) { + var expect = [ + /* BEGIN JSSTYLED */ + '[2016-02-10T07:28:41.419Z] INFO: myservice/123 on example.com: UnauthorizedError', + ' HTTP/1.1 401 Unauthorized', + ' content-type: text/plain', + ' date: Sat, 07 Mar 2015 06:58:43 GMT', + '[2016-02-10T07:28:41.419Z] INFO: myservice/123 on example.com: hello', + ' HTTP/1.1 200 OK', + ' content-type: text/plain', + ' content-length: 0', + ' date: Sat, 07 Mar 2015 06:58:43 GMT', + ' ', + ' hello', + '[2016-02-10T07:28:41.419Z] INFO: myservice/123 on example.com: UnauthorizedError', + ' HTTP/1.1 401 Unauthorized', + ' content-type: text/plain', + ' date: Sat, 07 Mar 2015 06:58:43 GMT' + /* END JSSTYLED */ + ].join('\n') + '\n'; + exec(_('%s %s/corpus/content-length-0-res.log', BUNYAN, __dirname), + function (err, stdout, stderr) { + t.ifError(err); + t.equal(stdout, expect); + t.end(); + }); +}); diff --git a/test/corpus/content-length-0-res.log b/test/corpus/content-length-0-res.log new file mode 100644 index 0000000..a754cce --- /dev/null +++ b/test/corpus/content-length-0-res.log @@ -0,0 +1,3 @@ +{"name":"myservice","hostname":"example.com","pid":123,"level":30,"client_res":{"statusCode":401,"headers":{"content-type":"text/plain","date":"Sat, 07 Mar 2015 06:58:43 GMT"},"body":""},"msg":"UnauthorizedError","time":"2016-02-10T07:28:41.419Z","v":0} +{"name":"myservice","hostname":"example.com","pid":123,"level":30,"client_res":{"statusCode":200,"headers":{"content-type":"text/plain","content-length":0,"date":"Sat, 07 Mar 2015 06:58:43 GMT"},"body":"hello"},"msg":"hello","time":"2016-02-10T07:28:41.419Z","v":0} +{"name":"myservice","hostname":"example.com","pid":123,"level":30,"client_res":{"statusCode":401,"headers":{"content-type":"text/plain","date":"Sat, 07 Mar 2015 06:58:43 GMT"}},"msg":"UnauthorizedError","time":"2016-02-10T07:28:41.419Z","v":0}