Skip to content

Commit e7061d8

Browse files
committed
update response and tests
1 parent 8bbd7ab commit e7061d8

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/main/java/com/amadeus/Response.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,14 @@ protected Response(Request request) {
6060
// Tries to parse the raw response from the request.
6161
protected void parse(HTTPClient client) {
6262
parseStatusCode();
63-
parseData(client);
63+
if (this.statusCode != 204) {
64+
parseData(client);
65+
}
6466
}
6567

6668
// Detects of any exceptions have occured and throws the appropriate exceptions.
6769
protected void detectError(HTTPClient client) throws ResponseException {
6870
ResponseException exception = null;
69-
if (statusCode == 204) {
70-
return;
71-
}
7271
if (statusCode >= 500) {
7372
exception = new ServerException(this);
7473
} else if (statusCode == 404) {
@@ -77,6 +76,8 @@ protected void detectError(HTTPClient client) throws ResponseException {
7776
exception = new AuthenticationException(this);
7877
} else if (statusCode >= 400) {
7978
exception = new ClientException(this);
79+
} else if (statusCode == 204) {
80+
return;
8081
} else if (!parsed) {
8182
exception = new ParserException(this);
8283
}
@@ -99,9 +100,6 @@ private void parseStatusCode() {
99100

100101
// Tries to parse the data
101102
private void parseData(HTTPClient client) {
102-
if (this.statusCode == 204) {
103-
return;
104-
}
105103
this.parsed = false;
106104
this.body = readBody();
107105
this.result = parseJson(client);

src/test/java/com/amadeus/ResponseTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,22 @@ public class ResponseTest {
107107
assertNull(response.getData());
108108
}
109109

110+
@Test public void testNoContent() throws IOException {
111+
when(connection.getResponseCode()).thenReturn(204);
112+
when(connection.getHeaderField("Content-Type")).thenReturn(
113+
"application/json");
114+
when(connection.getInputStream()).thenReturn(
115+
new ByteArrayInputStream("".getBytes()));
116+
117+
response.parse(client);
118+
119+
assertEquals(response.getStatusCode(), 204);
120+
assertEquals(response.getBody(), null);
121+
assertFalse(response.isParsed());
122+
assertNull(response.getResult());
123+
assertNull(response.getData());
124+
}
125+
110126
@Test public void testEmptyConnection() throws IOException {
111127
InputStream stream = mock(ByteArrayInputStream.class);
112128
when(connection.getResponseCode()).thenThrow(new IOException());
@@ -212,6 +228,16 @@ public void detectParserException() throws ResponseException, IOException {
212228
response.detectError(client);
213229
}
214230

231+
@Test public void detectNoExceptionNoContent() throws ResponseException, IOException {
232+
when(connection.getResponseCode()).thenReturn(204);
233+
when(connection.getHeaderField("Content-Type")).thenReturn(
234+
"application/json");
235+
when(connection.getInputStream()).thenReturn(
236+
new ByteArrayInputStream("{}".getBytes()));
237+
response.parse(client);
238+
response.detectError(client);
239+
}
240+
215241
@Test public void testToString() {
216242
assertTrue(response.toString().startsWith("Response("));
217243
}

0 commit comments

Comments
 (0)