From 3002182cacc7a9334237a9284a339ba93d3f213c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20=C5=BDia=C4=8Dik?= Date: Thu, 8 Jul 2021 21:02:14 +0200 Subject: [PATCH] fix: format falsy constants properly in json formatter (#1792) --- lib/formatters/json.js | 2 +- test/response.test.js | 80 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/lib/formatters/json.js b/lib/formatters/json.js index c8b14dcbc..3824fd4bb 100644 --- a/lib/formatters/json.js +++ b/lib/formatters/json.js @@ -19,7 +19,7 @@ var errors = require('restify-errors'); */ function formatJSON(req, res, body) { var data = 'null'; - if (body) { + if (body !== undefined) { try { data = JSON.stringify(body); } catch (e) { diff --git a/test/response.test.js b/test/response.test.js index a3866af21..373bc2145 100644 --- a/test/response.test.js +++ b/test/response.test.js @@ -680,3 +680,83 @@ test('GH-1607: should send numbers with explicit status code', function(t) { }); }); }); + +test('GH-1791: should send 0 as 0 with application/json', function(t) { + SERVER.get('/zero', function(req, res, next) { + res.contentType = 'application/json'; + res.send(200, 0); + return next(); + }); + + STRING_CLIENT.get(join(LOCALHOST, '/zero'), function(err, req, res, data) { + t.equal(data, '0'); + t.end(); + }); +}); + +test('GH-1791: should send false as false with application/json', function(t) { + SERVER.get('/false', function(req, res, next) { + res.contentType = 'application/json'; + res.send(200, false); + return next(); + }); + + STRING_CLIENT.get(join(LOCALHOST, '/false'), function(err, req, res, data) { + t.equal(data, 'false'); + t.end(); + }); +}); + +// eslint-disable-next-line +test('GH-1791: should send empty string as "" with application/json', function(t) { + SERVER.get('/empty', function(req, res, next) { + res.contentType = 'application/json'; + res.send(200, ''); + return next(); + }); + + STRING_CLIENT.get(join(LOCALHOST, '/empty'), function(err, req, res, data) { + t.equal(data, '""'); + t.end(); + }); +}); + +test('GH-1791: should send null as null with application/json', function(t) { + SERVER.get('/null', function(req, res, next) { + res.contentType = 'application/json'; + res.send(200, null); + return next(); + }); + + STRING_CLIENT.get(join(LOCALHOST, '/null'), function(err, req, res, data) { + t.equal(data, 'null'); + t.end(); + }); +}); + +// eslint-disable-next-line +test('GH-1791: should send undefined as empty with application/json', function(t) { + SERVER.get('/undef', function(req, res, next) { + res.contentType = 'application/json'; + res.send(200, undefined); + return next(); + }); + + STRING_CLIENT.get(join(LOCALHOST, '/undef'), function(err, req, res, data) { + t.equal(data, ''); + t.end(); + }); +}); + +test('GH-1791: should send NaN as null with application/json', function(t) { + SERVER.get('/nan', function(req, res, next) { + res.contentType = 'application/json'; + res.send(200, NaN); + return next(); + }); + + STRING_CLIENT.get(join(LOCALHOST, '/nan'), function(err, req, res, data) { + t.equal(data, 'null'); + t.end(); + }); +});