Skip to content

Commit 85c1c1b

Browse files
author
Harman Singh
committed
[PT-694] Do not throw exception for successful http codes and remove redundant cases
1 parent 6f4f35a commit 85c1c1b

File tree

2 files changed

+100
-45
lines changed

2 files changed

+100
-45
lines changed

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

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,19 @@
22

33
import com.sailthru.client.LastRateLimitInfo;
44
import com.sailthru.client.exceptions.ApiException;
5-
import com.sailthru.client.exceptions.ResourceNotFoundException;
6-
import com.sailthru.client.exceptions.UnAuthorizedException;
75
import com.sailthru.client.handler.SailthruResponseHandler;
86
import java.io.IOException;
97
import java.util.Date;
108
import java.util.Map;
11-
129
import org.apache.http.Header;
1310
import org.apache.http.HttpResponse;
1411
import org.apache.http.StatusLine;
15-
import org.apache.http.client.ClientProtocolException;
1612
import org.apache.http.client.ResponseHandler;
1713
import org.apache.http.util.EntityUtils;
1814
import org.slf4j.Logger;
1915
import org.slf4j.LoggerFactory;
2016

2117
/**
22-
*
2318
* @author Prajwal Tuladhar <[email protected]>
2419
*/
2520
public class SailthruHandler implements ResponseHandler<Object> {
@@ -31,58 +26,26 @@ public class SailthruHandler implements ResponseHandler<Object> {
3126
// key used to store rate limit info, for use and removal by the parent SailthruClient
3227
public static final String RATE_LIMIT_INFO_KEY = "x_rate_limit_info";
3328

34-
/* Supported HTTP Status codes */
35-
public static final int STATUS_OK = 200;
36-
public static final int STATUS_BAD_REQUEST = 400;
37-
public static final int STATUS_UNAUTHORIZED = 401;
38-
public static final int STATUS_FORBIDDEN = 403;
39-
public static final int STATUS_NOT_FOUND = 404;
40-
public static final int STATUS_METHOD_NOT_FOUND = 405;
41-
public static final int STATUS_INTERNAL_SERVER_ERROR = 500;
42-
4329
public SailthruHandler(SailthruResponseHandler handler) {
4430
super();
4531
this.handler = handler;
4632
}
4733

48-
public Object handleResponse(HttpResponse httpResponse) throws ClientProtocolException, IOException {
49-
logger.debug("Received Response: {}", httpResponse.toString());
34+
public Object handleResponse(HttpResponse httpResponse) throws IOException {
35+
logger.debug("Received Response: {}", httpResponse);
5036

5137
StatusLine statusLine = httpResponse.getStatusLine();
52-
int statusCode = statusLine.getStatusCode();
5338

54-
String jsonString = null;
55-
jsonString = EntityUtils.toString(httpResponse.getEntity());
56-
Object parseObject = handler.parseResponse(jsonString);
39+
String jsonString = EntityUtils.toString(httpResponse.getEntity());
40+
Object parseObject = handler.parseResponse(jsonString);
5741

5842
addRateLimitInfoToResponseObject(httpResponse, parseObject);
5943

60-
switch (statusCode) {
61-
case STATUS_OK:
62-
break;
63-
64-
case STATUS_BAD_REQUEST:
65-
throw ApiException.create(statusLine, parseObject);
66-
67-
case STATUS_UNAUTHORIZED:
68-
throw UnAuthorizedException.create(statusLine, parseObject);
69-
70-
case STATUS_FORBIDDEN:
71-
throw ApiException.create(statusLine, parseObject);
72-
73-
case STATUS_NOT_FOUND:
74-
throw ResourceNotFoundException.create(statusLine, parseObject);
75-
76-
case STATUS_METHOD_NOT_FOUND:
77-
throw ApiException.create(statusLine, parseObject);
78-
79-
case STATUS_INTERNAL_SERVER_ERROR:
80-
throw ApiException.create(statusLine, parseObject);
81-
82-
default:
83-
throw ApiException.create(statusLine, parseObject);
44+
// if status is not successful (i.e. 2xx) throw exception
45+
int statusCode = statusLine.getStatusCode();
46+
if (statusCode / 100 != 2) {
47+
throw ApiException.create(statusLine, parseObject);
8448
}
85-
8649
return parseObject;
8750
}
8851

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
39+
@Test
40+
public void testHandlingClientErrorResponse() throws IOException {
41+
CloseableHttpResponse httpBadResponse = getMockHttpResponseWithStatus(HttpStatus.SC_BAD_REQUEST, "Bad Request");
42+
try {
43+
handler.handleResponse(httpBadResponse);
44+
Assert.fail("Expected an APIException to be thrown");
45+
} catch (ApiException apiException) {
46+
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, apiException.getStatusCode());
47+
}
48+
49+
CloseableHttpResponse httpUnauthorizedResponse = getMockHttpResponseWithStatus(HttpStatus.SC_UNAUTHORIZED, "Unauthorized");
50+
try {
51+
handler.handleResponse(httpUnauthorizedResponse);
52+
Assert.fail("Expected an APIException to be thrown");
53+
} catch (ApiException apiException) {
54+
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, apiException.getStatusCode());
55+
}
56+
}
57+
58+
@Test
59+
public void testHandlingServerErrorResponse() throws IOException {
60+
CloseableHttpResponse httCreatedResponse = getMockHttpResponseWithStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Internal Server Error");
61+
try {
62+
Object okResponse = handler.handleResponse(httCreatedResponse);
63+
Assert.fail("Expected an APIException to be thrown");
64+
} catch (ApiException apiException) {
65+
Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, apiException.getStatusCode());
66+
}
67+
68+
CloseableHttpResponse httpCreatedResponse = getMockHttpResponseWithStatus(HttpStatus.SC_BAD_GATEWAY, "Bad Gateway");
69+
try {
70+
Object okResponse = handler.handleResponse(httpCreatedResponse);
71+
Assert.fail("Expected an APIException to be thrown");
72+
} catch (ApiException apiException) {
73+
Assert.assertEquals(HttpStatus.SC_BAD_GATEWAY, apiException.getStatusCode());
74+
}
75+
}
76+
77+
private CloseableHttpResponse getMockHttpResponseWithStatus(int statusCode, String reasonPhrase) {
78+
CloseableHttpResponse mockHttpResponse = mock(CloseableHttpResponse.class);
79+
80+
BasicHttpEntity fakeEntity = new BasicHttpEntity();
81+
fakeEntity.setContent(new ByteArrayInputStream(EXAMPLE_RESPONSE.getBytes()));
82+
when(mockHttpResponse.getEntity()).thenReturn(fakeEntity);
83+
84+
when(mockHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), statusCode, reasonPhrase));
85+
86+
when(mockHttpResponse.getFirstHeader("X-Rate-Limit-Limit")).thenReturn(null);
87+
when(mockHttpResponse.getFirstHeader("X-Rate-Limit-Remaining")).thenReturn(null);
88+
when(mockHttpResponse.getFirstHeader("X-Rate-Limit-Reset")).thenReturn(null);
89+
90+
return mockHttpResponse;
91+
}
92+
}

0 commit comments

Comments
 (0)