Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: format falsy constants properly in json formatter #1792

Merged
merged 2 commits into from Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/formatters/json.js
Expand Up @@ -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) {
Expand Down
80 changes: 80 additions & 0 deletions test/response.test.js
Expand Up @@ -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();
});
});