Skip to content

Commit 59b3231

Browse files
committed
add test case for invalid JSON response body
1 parent c67cbb5 commit 59b3231

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

lib/Client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ merge(Client.prototype, {
6565
try {
6666
wrapper.body = serializer.load(buffer, resources);
6767
} catch (err) {
68-
return reject(errors());
68+
reject(errors('client', 'failed to parse response: "' + buffer + '"'));
6969
return;
7070
}
7171
}

lib/Errors.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ var errors = {
1515
'406': constructError('UnsupportedFormatRequestedError'),
1616
'422': constructError('ResourceValidationError'),
1717
'500': constructError('GeneralAPIError'),
18-
'503': constructError('ServiceUnavailableError')
18+
'503': constructError('ServiceUnavailableError'),
19+
'client': constructError('GeneralClientError')
1920
};
2021

2122
module.exports = function(status, message) {

test/client_test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,15 @@ describe('Client', function() {
7979
expect(response.body).to.eql(undefined);
8080
});
8181
});
82+
83+
it('rejects invalid JSON response body', function() {
84+
var client = new Client(helper.config);
85+
86+
return client.get('/invalid_json').then(function(_) {
87+
expect(false).to.be(true);
88+
}, function(error) {
89+
expect(error.type).to.eq('GeneralClientError');
90+
});
91+
});
8292
});
8393
});

test/test_helper.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ var requests = {
9191
'GET /empty_body': {
9292
status: 200,
9393
body_raw: ''
94+
},
95+
96+
'GET /invalid_json': {
97+
status: 200,
98+
body_raw: '{"invalid_json":123'
9499
}
95100
};
96101

0 commit comments

Comments
 (0)