-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PT-694] Do not throw exception for successful http codes and remove …
…redundant cases
- Loading branch information
Harman Singh
committed
May 17, 2021
1 parent
6f4f35a
commit 85c1c1b
Showing
2 changed files
with
100 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/test/com/sailthru/client/http/SailthruHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.sailthru.client.http; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.gson.Gson; | ||
import com.sailthru.client.SailthruUtil; | ||
import com.sailthru.client.exceptions.ApiException; | ||
import com.sailthru.client.handler.JsonHandler; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import org.apache.http.HttpStatus; | ||
import org.apache.http.ProtocolVersion; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.entity.BasicHttpEntity; | ||
import org.apache.http.message.BasicStatusLine; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class SailthruHandlerTest { | ||
private final SailthruHandler handler = new SailthruHandler(new JsonHandler()); | ||
private final Gson gson = SailthruUtil.createGson(); | ||
private static final String EXAMPLE_RESPONSE = "{\"sample_response\":true}"; | ||
|
||
@Test | ||
public void testHandlingSuccessfulResponse() throws IOException { | ||
CloseableHttpResponse httpOkResponse = getMockHttpResponseWithStatus(HttpStatus.SC_OK, "OK"); | ||
Object okResponse = handler.handleResponse(httpOkResponse); | ||
Assert.assertEquals(EXAMPLE_RESPONSE, gson.toJson(okResponse)); | ||
|
||
CloseableHttpResponse httpCreatedResponse = getMockHttpResponseWithStatus(HttpStatus.SC_CREATED, "Created"); | ||
Object createdResponse = handler.handleResponse(httpCreatedResponse); | ||
Assert.assertEquals(EXAMPLE_RESPONSE, gson.toJson(createdResponse)); | ||
} | ||
|
||
@Test | ||
public void testHandlingClientErrorResponse() throws IOException { | ||
CloseableHttpResponse httpBadResponse = getMockHttpResponseWithStatus(HttpStatus.SC_BAD_REQUEST, "Bad Request"); | ||
try { | ||
handler.handleResponse(httpBadResponse); | ||
Assert.fail("Expected an APIException to be thrown"); | ||
} catch (ApiException apiException) { | ||
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, apiException.getStatusCode()); | ||
} | ||
|
||
CloseableHttpResponse httpUnauthorizedResponse = getMockHttpResponseWithStatus(HttpStatus.SC_UNAUTHORIZED, "Unauthorized"); | ||
try { | ||
handler.handleResponse(httpUnauthorizedResponse); | ||
Assert.fail("Expected an APIException to be thrown"); | ||
} catch (ApiException apiException) { | ||
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, apiException.getStatusCode()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testHandlingServerErrorResponse() throws IOException { | ||
CloseableHttpResponse httCreatedResponse = getMockHttpResponseWithStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Internal Server Error"); | ||
try { | ||
Object okResponse = handler.handleResponse(httCreatedResponse); | ||
Assert.fail("Expected an APIException to be thrown"); | ||
} catch (ApiException apiException) { | ||
Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, apiException.getStatusCode()); | ||
} | ||
|
||
CloseableHttpResponse httpCreatedResponse = getMockHttpResponseWithStatus(HttpStatus.SC_BAD_GATEWAY, "Bad Gateway"); | ||
try { | ||
Object okResponse = handler.handleResponse(httpCreatedResponse); | ||
Assert.fail("Expected an APIException to be thrown"); | ||
} catch (ApiException apiException) { | ||
Assert.assertEquals(HttpStatus.SC_BAD_GATEWAY, apiException.getStatusCode()); | ||
} | ||
} | ||
|
||
private CloseableHttpResponse getMockHttpResponseWithStatus(int statusCode, String reasonPhrase) { | ||
CloseableHttpResponse mockHttpResponse = mock(CloseableHttpResponse.class); | ||
|
||
BasicHttpEntity fakeEntity = new BasicHttpEntity(); | ||
fakeEntity.setContent(new ByteArrayInputStream(EXAMPLE_RESPONSE.getBytes())); | ||
when(mockHttpResponse.getEntity()).thenReturn(fakeEntity); | ||
|
||
when(mockHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), statusCode, reasonPhrase)); | ||
|
||
when(mockHttpResponse.getFirstHeader("X-Rate-Limit-Limit")).thenReturn(null); | ||
when(mockHttpResponse.getFirstHeader("X-Rate-Limit-Remaining")).thenReturn(null); | ||
when(mockHttpResponse.getFirstHeader("X-Rate-Limit-Reset")).thenReturn(null); | ||
|
||
return mockHttpResponse; | ||
} | ||
} |