Skip to content

Commit c62307f

Browse files
author
Harman Singh
committed
[PT-694] Do not throw exception for successful created http code
1 parent 6f4f35a commit c62307f

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

src/main/com/sailthru/client/http/SailthruHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public class SailthruHandler implements ResponseHandler<Object> {
3333

3434
/* Supported HTTP Status codes */
3535
public static final int STATUS_OK = 200;
36+
public static final int STATUS_CREATED = 201;
37+
public static final int STATUS_NO_CONTENT = 204;
3638
public static final int STATUS_BAD_REQUEST = 400;
3739
public static final int STATUS_UNAUTHORIZED = 401;
3840
public static final int STATUS_FORBIDDEN = 403;
@@ -59,6 +61,8 @@ public Object handleResponse(HttpResponse httpResponse) throws ClientProtocolExc
5961

6062
switch (statusCode) {
6163
case STATUS_OK:
64+
case STATUS_CREATED:
65+
case STATUS_NO_CONTENT:
6266
break;
6367

6468
case STATUS_BAD_REQUEST:
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.sailthru.client.http;
2+
3+
import static org.mockito.Mockito.mock;
4+
import static org.mockito.Mockito.when;
5+
6+
import com.google.gson.Gson;
7+
import com.sailthru.client.SailthruUtil;
8+
import com.sailthru.client.exceptions.ApiException;
9+
import com.sailthru.client.handler.JsonHandler;
10+
import java.io.ByteArrayInputStream;
11+
import java.io.IOException;
12+
import org.apache.http.HttpStatus;
13+
import org.apache.http.ProtocolVersion;
14+
import org.apache.http.client.methods.CloseableHttpResponse;
15+
import org.apache.http.entity.BasicHttpEntity;
16+
import org.apache.http.message.BasicStatusLine;
17+
import org.junit.Assert;
18+
import org.junit.Test;
19+
import org.junit.runner.RunWith;
20+
import org.mockito.junit.MockitoJUnitRunner;
21+
22+
@RunWith(MockitoJUnitRunner.class)
23+
public class SailthruHandlerTest {
24+
private final SailthruHandler handler = new SailthruHandler(new JsonHandler());
25+
private final Gson gson = SailthruUtil.createGson();
26+
private static final String EXAMPLE_RESPONSE = "{\"sample_response\":true}";
27+
28+
@Test
29+
public void testHandlingSuccessfulResponse() throws IOException {
30+
CloseableHttpResponse httpOkResponse = getMockHttpResponseWithStatus(HttpStatus.SC_OK, "OK");
31+
Object okResponse = handler.handleResponse(httpOkResponse);
32+
Assert.assertEquals(EXAMPLE_RESPONSE, gson.toJson(okResponse));
33+
34+
CloseableHttpResponse httpCreatedResponse = getMockHttpResponseWithStatus(HttpStatus.SC_CREATED, "Created");
35+
Object createdResponse = handler.handleResponse(httpCreatedResponse);
36+
Assert.assertEquals(EXAMPLE_RESPONSE, gson.toJson(createdResponse));
37+
38+
CloseableHttpResponse httpNoContentResponse = getMockHttpResponseWithStatus(HttpStatus.SC_NO_CONTENT, "No Content");
39+
Object noContentResponse = handler.handleResponse(httpNoContentResponse);
40+
Assert.assertEquals(EXAMPLE_RESPONSE, gson.toJson(noContentResponse));
41+
}
42+
43+
@Test
44+
public void testHandlingClientErrorResponse() throws IOException {
45+
CloseableHttpResponse httpBadResponse = getMockHttpResponseWithStatus(HttpStatus.SC_BAD_REQUEST, "Bad Request");
46+
try {
47+
handler.handleResponse(httpBadResponse);
48+
Assert.fail("Expected an APIException to be thrown");
49+
} catch (ApiException apiException) {
50+
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, apiException.getStatusCode());
51+
}
52+
53+
CloseableHttpResponse httpUnauthorizedResponse = getMockHttpResponseWithStatus(HttpStatus.SC_UNAUTHORIZED, "Unauthorized");
54+
try {
55+
handler.handleResponse(httpUnauthorizedResponse);
56+
Assert.fail("Expected an APIException to be thrown");
57+
} catch (ApiException apiException) {
58+
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, apiException.getStatusCode());
59+
}
60+
}
61+
62+
@Test
63+
public void testHandlingServerErrorResponse() throws IOException {
64+
CloseableHttpResponse httpServerErrorResponse = getMockHttpResponseWithStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Internal Server Error");
65+
try {
66+
handler.handleResponse(httpServerErrorResponse);
67+
Assert.fail("Expected an APIException to be thrown");
68+
} catch (ApiException apiException) {
69+
Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, apiException.getStatusCode());
70+
}
71+
72+
CloseableHttpResponse httpBadGatewayResponse = getMockHttpResponseWithStatus(HttpStatus.SC_BAD_GATEWAY, "Bad Gateway");
73+
try {
74+
handler.handleResponse(httpBadGatewayResponse);
75+
Assert.fail("Expected an APIException to be thrown");
76+
} catch (ApiException apiException) {
77+
Assert.assertEquals(HttpStatus.SC_BAD_GATEWAY, apiException.getStatusCode());
78+
}
79+
}
80+
81+
private CloseableHttpResponse getMockHttpResponseWithStatus(int statusCode, String reasonPhrase) {
82+
CloseableHttpResponse mockHttpResponse = mock(CloseableHttpResponse.class);
83+
84+
BasicHttpEntity fakeEntity = new BasicHttpEntity();
85+
fakeEntity.setContent(new ByteArrayInputStream(EXAMPLE_RESPONSE.getBytes()));
86+
when(mockHttpResponse.getEntity()).thenReturn(fakeEntity);
87+
88+
when(mockHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), statusCode, reasonPhrase));
89+
90+
when(mockHttpResponse.getFirstHeader("X-Rate-Limit-Limit")).thenReturn(null);
91+
when(mockHttpResponse.getFirstHeader("X-Rate-Limit-Remaining")).thenReturn(null);
92+
when(mockHttpResponse.getFirstHeader("X-Rate-Limit-Reset")).thenReturn(null);
93+
94+
return mockHttpResponse;
95+
}
96+
}

0 commit comments

Comments
 (0)