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

Add support for application/vnd.api+json content types #362

Merged
merged 1 commit into from
Jul 7, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
55 changes: 54 additions & 1 deletion test/parsing_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ describe('parsing', function(){

})


describe('valid XML, using xml2js', function() {

var parsers, origParser;
Expand Down Expand Up @@ -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();
});
});

});

})

});

})