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

Added support for automatic JSON response parsing. #6

Merged
merged 4 commits into from Mar 12, 2014
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
30 changes: 27 additions & 3 deletions lib/index.js
Expand Up @@ -211,7 +211,27 @@ exports.read = function (res /*, [options], callback */) {
res.removeListener('close', onClose);
res.on('error', Hoek.ignore);

return callback(err, buffer);
if (err || options.json !== true) {
return callback(err, buffer);
}

var contentType = ((res.headers && res.headers['content-type']) || '');
var mime = contentType.split(';')[0].trim().toLowerCase();
var json = null;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No empty line

if (mime !== 'application/json') {
return callback(err, buffer);
}

try {
json = JSON.parse(buffer.toString());
}
catch (err) {

return callback(err, null);
}

return callback(err, json);
};

finish = Hoek.once(finish);
Expand Down Expand Up @@ -364,9 +384,13 @@ internals.shortcut = function (method, uri /*, options, callback */) {
return callback(err);
}

exports.read(res, function (err, payload) {
exports.read(res, options, function (err, payload) {

if (payload instanceof Buffer) {
payload = payload.toString();
}

return callback(err, res, payload.toString());
return callback(err, res, payload);
});
});
};
Expand Down
116 changes: 116 additions & 0 deletions test/index.js
Expand Up @@ -817,4 +817,120 @@ describe('Nipple', function () {
});
});
});

describe('json', function () {

it('json requested and received', function (done) {

var server = Http.createServer(function (req, res) {

res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({
foo: 'bar'
}));
});

server.listen(0, function () {

var port = server.address().port;
var options = {
json: true
};

Nipple.get('http://localhost:' + port, options, function (err, res, payload) {

expect(err).to.not.exist;
expect(res.statusCode).to.equal(200);
expect(payload).to.not.equal(null);
expect(payload.foo).to.exist;
server.close();
done();
});
});
});

it('json requested but not received - flag is ignored', function (done) {

var server = Http.createServer(function (req, res) {

res.writeHead(200);
res.end('ok');
});

server.listen(0, function () {

var port = server.address().port;
var options = {
json: true
};

Nipple.get('http://localhost:' + port, options, function (err, res, payload) {

expect(err).to.not.exist;
expect(res.statusCode).to.equal(200);
expect(payload).to.not.equal(null);
server.close();
done();
});
});
});

it('invalid json received', function (done) {

var server = Http.createServer(function (req, res) {

res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end('ok');
});

server.listen(0, function () {

var port = server.address().port;
var options = {
json: true
};

Nipple.get('http://localhost:' + port, options, function (err, res, payload) {

expect(err).to.exist;
server.close();
done();
});
});
});

it('json not requested but received as string', function (done) {

var server = Http.createServer(function (req, res) {

res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({
foo: 'bar'
}));
});

server.listen(0, function () {

var port = server.address().port;
var options = {
json: false
};

Nipple.get('http://localhost:' + port, options, function (err, res, payload) {

expect(err).to.not.exist;
expect(res.statusCode).to.equal(200);
expect(payload).to.not.equal(null);
server.close();
done();
});
});
});
});
});