Skip to content

Commit 3002182

Browse files
authoredJul 8, 2021
fix: format falsy constants properly in json formatter (#1792)
1 parent 19b8662 commit 3002182

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed
 

‎lib/formatters/json.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var errors = require('restify-errors');
1919
*/
2020
function formatJSON(req, res, body) {
2121
var data = 'null';
22-
if (body) {
22+
if (body !== undefined) {
2323
try {
2424
data = JSON.stringify(body);
2525
} catch (e) {

‎test/response.test.js

+80
Original file line numberDiff line numberDiff line change
@@ -680,3 +680,83 @@ test('GH-1607: should send numbers with explicit status code', function(t) {
680680
});
681681
});
682682
});
683+
684+
test('GH-1791: should send 0 as 0 with application/json', function(t) {
685+
SERVER.get('/zero', function(req, res, next) {
686+
res.contentType = 'application/json';
687+
res.send(200, 0);
688+
return next();
689+
});
690+
691+
STRING_CLIENT.get(join(LOCALHOST, '/zero'), function(err, req, res, data) {
692+
t.equal(data, '0');
693+
t.end();
694+
});
695+
});
696+
697+
test('GH-1791: should send false as false with application/json', function(t) {
698+
SERVER.get('/false', function(req, res, next) {
699+
res.contentType = 'application/json';
700+
res.send(200, false);
701+
return next();
702+
});
703+
704+
STRING_CLIENT.get(join(LOCALHOST, '/false'), function(err, req, res, data) {
705+
t.equal(data, 'false');
706+
t.end();
707+
});
708+
});
709+
710+
// eslint-disable-next-line
711+
test('GH-1791: should send empty string as "" with application/json', function(t) {
712+
SERVER.get('/empty', function(req, res, next) {
713+
res.contentType = 'application/json';
714+
res.send(200, '');
715+
return next();
716+
});
717+
718+
STRING_CLIENT.get(join(LOCALHOST, '/empty'), function(err, req, res, data) {
719+
t.equal(data, '""');
720+
t.end();
721+
});
722+
});
723+
724+
test('GH-1791: should send null as null with application/json', function(t) {
725+
SERVER.get('/null', function(req, res, next) {
726+
res.contentType = 'application/json';
727+
res.send(200, null);
728+
return next();
729+
});
730+
731+
STRING_CLIENT.get(join(LOCALHOST, '/null'), function(err, req, res, data) {
732+
t.equal(data, 'null');
733+
t.end();
734+
});
735+
});
736+
737+
// eslint-disable-next-line
738+
test('GH-1791: should send undefined as empty with application/json', function(t) {
739+
SERVER.get('/undef', function(req, res, next) {
740+
res.contentType = 'application/json';
741+
res.send(200, undefined);
742+
return next();
743+
});
744+
745+
STRING_CLIENT.get(join(LOCALHOST, '/undef'), function(err, req, res, data) {
746+
t.equal(data, '');
747+
t.end();
748+
});
749+
});
750+
751+
test('GH-1791: should send NaN as null with application/json', function(t) {
752+
SERVER.get('/nan', function(req, res, next) {
753+
res.contentType = 'application/json';
754+
res.send(200, NaN);
755+
return next();
756+
});
757+
758+
STRING_CLIENT.get(join(LOCALHOST, '/nan'), function(err, req, res, data) {
759+
t.equal(data, 'null');
760+
t.end();
761+
});
762+
});

0 commit comments

Comments
 (0)
Please sign in to comment.