Skip to content

Commit cfc51be

Browse files
authored
Merge pull request #362 from kfitzgerald/json-api-support
Add support for `application/vnd.api+json` content types
2 parents 4134648 + 9365d38 commit cfc51be

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

lib/parsers.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ function buildParser(name, types, fn) {
9797

9898
buildParser('json', [
9999
'application/json',
100-
'text/javascript'
100+
'text/javascript',
101+
'application/vnd.api+json'
101102
], function(buffer, cb) {
102103
var err, data;
103104
try { data = JSON.parse(buffer); } catch (e) { err = e; }

test/parsing_spec.js

+54-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,6 @@ describe('parsing', function(){
421421

422422
})
423423

424-
425424
describe('valid XML, using xml2js', function() {
426425

427426
var parsers, origParser;
@@ -490,5 +489,59 @@ describe('parsing', function(){
490489

491490
})
492491

492+
describe('when response is a JSON API flavored JSON string', function () {
493+
494+
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"}}]}';
495+
496+
before(function(done){
497+
server = http.createServer(function(req, res) {
498+
res.setHeader('Content-Type', 'application/vnd.api+json');
499+
res.end(json_string);
500+
}).listen(port, done);
501+
});
502+
503+
after(function(done){
504+
server.close(done);
505+
});
506+
507+
describe('and parse option is not passed', function() {
508+
509+
describe('with default parse_response', function() {
510+
511+
before(function() {
512+
needle.defaults().parse_response.should.eql('all')
513+
})
514+
515+
it('should return object', function(done){
516+
needle.get('localhost:' + port, function(err, response, body){
517+
should.ifError(err);
518+
body.should.deepEqual({
519+
"data": [{
520+
"type": "articles",
521+
"id": "1",
522+
"attributes": {
523+
"title": "Needle",
524+
"body": "The leanest and most handsome HTTP client in the Nodelands."
525+
}
526+
}],
527+
"included": [
528+
{
529+
"type": "people",
530+
"id": "42",
531+
"attributes": {
532+
"name": "Tomás"
533+
}
534+
}
535+
]
536+
});
537+
done();
538+
});
539+
});
540+
541+
});
542+
543+
})
544+
545+
});
493546

494547
})

0 commit comments

Comments
 (0)