Skip to content

Commit 3e18069

Browse files
authored
Merge pull request #99 from amadeus4dev/fix-parser-error-flight-order-mngmnt
Fix 204 parser exception and add test
2 parents 6e2736a + ff178ec commit 3e18069

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

spec/amadeus/client/response.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ describe('Response', () => {
5555
expect(response.data).toBeNull();
5656
});
5757

58+
it('should not parse if status code is 204', () => {
59+
response.contentType = 'plain/text';
60+
response.statusCode = 204;
61+
response.parse();
62+
expect(response.parsed).toBeFalsy();
63+
expect(response.result).toBeNull();
64+
expect(response.data).toBeNull();
65+
});
66+
5867
it('should handle badly formed json', () => {
5968
response.addChunk('{ "a" : ');
6069
response.parse();
@@ -65,6 +74,12 @@ describe('Response', () => {
6574
});
6675

6776
describe('.success', () => {
77+
it('be true if the document was not parsed with a 204 status code', () => {
78+
response.statusCode = 204;
79+
response.parsed = false;
80+
expect(response.success()).toBeTruthy();
81+
});
82+
6883
it('be true if the document was parsed and with a 2XX status code', () => {
6984
response.statusCode = 201;
7085
response.parsed = true;
@@ -77,7 +92,7 @@ describe('Response', () => {
7792
expect(response.success()).toBeFalsy();
7893
});
7994

80-
it('be false if the document was not parsed and with a 2XX status code', () => {
95+
it('be false if the document was not parsed and with a 2XX status code excluding 204', () => {
8196
response.statusCode = 201;
8297
response.parsed = false;
8398
expect(response.success()).toBeFalsy();

src/amadeus/client/response.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class Response {
4545
*/
4646
parse() {
4747
try {
48+
if (this.statusCode == 204) {
49+
return;
50+
}
4851
if (this.isJson()) {
4952
this.result = JSON.parse(this.body);
5053
this.data = this.result.data;
@@ -65,7 +68,12 @@ class Response {
6568
* @protected
6669
*/
6770
success() {
68-
return (this.parsed && this.statusCode < 300);
71+
if (this.statusCode == 204) {
72+
return true;
73+
}
74+
if (this.parsed && this.statusCode < 300) {
75+
return true;
76+
}
6977
}
7078

7179
// PRIVATE

0 commit comments

Comments
 (0)