From 9365d38c784cb4a79947f1d12189a5374d166e54 Mon Sep 17 00:00:00 2001 From: Kevin Fitzgerald Date: Tue, 6 Jul 2021 23:09:13 -0500 Subject: [PATCH] Added support for `application/vnd.api+json` content types --- lib/parsers.js | 3 ++- test/parsing_spec.js | 55 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/lib/parsers.js b/lib/parsers.js index 57f33ecc0..3da728710 100644 --- a/lib/parsers.js +++ b/lib/parsers.js @@ -97,7 +97,8 @@ function buildParser(name, types, fn) { buildParser('json', [ 'application/json', - 'text/javascript' + 'text/javascript', + 'application/vnd.api+json' ], function(buffer, cb) { var err, data; try { data = JSON.parse(buffer); } catch (e) { err = e; } diff --git a/test/parsing_spec.js b/test/parsing_spec.js index 1e117dc8a..1286737e8 100644 --- a/test/parsing_spec.js +++ b/test/parsing_spec.js @@ -421,7 +421,6 @@ describe('parsing', function(){ }) - describe('valid XML, using xml2js', function() { var parsers, origParser; @@ -490,5 +489,59 @@ describe('parsing', function(){ }) + describe('when response is a JSON API flavored JSON string', function () { + + var json_string = '{"data":[{"type":"articles","id":"1","attributes":{"title":"Needle","body":"The leanest and most handsome HTTP client in the Nodelands."}}],"included":[{"type":"people","id":"42","attributes":{"name":"Tomás"}}]}'; + + before(function(done){ + server = http.createServer(function(req, res) { + res.setHeader('Content-Type', 'application/vnd.api+json'); + res.end(json_string); + }).listen(port, done); + }); + + after(function(done){ + server.close(done); + }); + + describe('and parse option is not passed', function() { + + describe('with default parse_response', function() { + + before(function() { + needle.defaults().parse_response.should.eql('all') + }) + + it('should return object', function(done){ + needle.get('localhost:' + port, function(err, response, body){ + should.ifError(err); + body.should.deepEqual({ + "data": [{ + "type": "articles", + "id": "1", + "attributes": { + "title": "Needle", + "body": "The leanest and most handsome HTTP client in the Nodelands." + } + }], + "included": [ + { + "type": "people", + "id": "42", + "attributes": { + "name": "Tomás" + } + } + ] + }); + done(); + }); + }); + + }); + + }) + + }); })