|
| 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