|
| 1 | +package io.ably.lib.http; |
| 2 | + |
| 3 | +import io.ably.lib.types.AblyException; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import java.net.URL; |
| 7 | + |
| 8 | +import static org.junit.Assert.assertThrows; |
| 9 | +import static org.mockito.Mockito.any; |
| 10 | +import static org.mockito.Mockito.eq; |
| 11 | +import static org.mockito.Mockito.mock; |
| 12 | +import static org.mockito.Mockito.when; |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | + |
| 15 | +public class HttpHelpersTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + public void getUrlString_validResponse_returnsString() throws Exception { |
| 19 | + HttpCore mockHttpCore = mock(HttpCore.class); |
| 20 | + HttpCore.Response mockResponse = new HttpCore.Response(); |
| 21 | + mockResponse.body = "Test Response".getBytes(); |
| 22 | + |
| 23 | + when(mockHttpCore.httpExecuteWithRetry( |
| 24 | + eq(new URL("http://example.com")), |
| 25 | + eq("GET"), |
| 26 | + eq(null), |
| 27 | + eq(null), |
| 28 | + any(HttpCore.ResponseHandler.class), |
| 29 | + eq(false) |
| 30 | + )).thenAnswer(invocation -> { |
| 31 | + HttpCore.ResponseHandler<byte[]> responseHandler = invocation.getArgumentAt(4, HttpCore.ResponseHandler.class); |
| 32 | + return responseHandler.handleResponse(mockResponse, null); |
| 33 | + }); |
| 34 | + |
| 35 | + String result = HttpHelpers.getUrlString(mockHttpCore, "http://example.com"); |
| 36 | + assertEquals("Test Response", result); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void getUrlString_emptyResponse_throwsAblyException() throws Exception { |
| 41 | + HttpCore mockHttpCore = mock(HttpCore.class); |
| 42 | + HttpCore.Response mockResponse = new HttpCore.Response(); |
| 43 | + |
| 44 | + when(mockHttpCore.httpExecuteWithRetry( |
| 45 | + eq(new URL("http://example.com")), |
| 46 | + eq("GET"), |
| 47 | + eq(null), |
| 48 | + eq(null), |
| 49 | + any(HttpCore.ResponseHandler.class), |
| 50 | + eq(false) |
| 51 | + )).thenAnswer(invocation -> { |
| 52 | + HttpCore.ResponseHandler<byte[]> responseHandler = invocation.getArgumentAt(4, HttpCore.ResponseHandler.class); |
| 53 | + return responseHandler.handleResponse(mockResponse, null); |
| 54 | + }); |
| 55 | + |
| 56 | + AblyException e = assertThrows(AblyException.class, () -> HttpHelpers.getUrlString(mockHttpCore, "http://example.com")); |
| 57 | + assertEquals(500, e.errorInfo.statusCode); |
| 58 | + assertEquals(50000, e.errorInfo.code); |
| 59 | + assertEquals("Empty response body", e.errorInfo.message); |
| 60 | + } |
| 61 | +} |
0 commit comments