diff --git a/lib/src/main/java/io/ably/lib/http/HttpHelpers.java b/lib/src/main/java/io/ably/lib/http/HttpHelpers.java index 264d5fb79..56862ffd4 100644 --- a/lib/src/main/java/io/ably/lib/http/HttpHelpers.java +++ b/lib/src/main/java/io/ably/lib/http/HttpHelpers.java @@ -1,6 +1,5 @@ package io.ably.lib.http; -import java.io.IOException; import java.net.URL; import io.ably.lib.types.AblyException; @@ -42,7 +41,11 @@ public void execute(HttpScheduler http, Callback callback) throws AblyExcepti * @throws AblyException */ public static String getUrlString(HttpCore httpCore, String url) throws AblyException { - return new String(getUrl(httpCore, url)); + byte[] bytes = getUrl(httpCore, url); + if (bytes == null) { + throw AblyException.fromErrorInfo(new ErrorInfo("Empty response body", 500, 50000)); + } + return new String(bytes); } /** @@ -62,8 +65,8 @@ public byte[] handleResponse(HttpCore.Response response, ErrorInfo error) throws } return response.body; }}); - } catch(IOException ioe) { - throw AblyException.fromThrowable(ioe); + } catch (Exception e) { + throw AblyException.fromThrowable(e); } } diff --git a/lib/src/test/java/io/ably/lib/http/HttpHelpersTest.java b/lib/src/test/java/io/ably/lib/http/HttpHelpersTest.java new file mode 100644 index 000000000..039eb342a --- /dev/null +++ b/lib/src/test/java/io/ably/lib/http/HttpHelpersTest.java @@ -0,0 +1,61 @@ +package io.ably.lib.http; + +import io.ably.lib.types.AblyException; +import org.junit.Test; + +import java.net.URL; + +import static org.junit.Assert.assertThrows; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.junit.Assert.assertEquals; + +public class HttpHelpersTest { + + @Test + public void getUrlString_validResponse_returnsString() throws Exception { + HttpCore mockHttpCore = mock(HttpCore.class); + HttpCore.Response mockResponse = new HttpCore.Response(); + mockResponse.body = "Test Response".getBytes(); + + when(mockHttpCore.httpExecuteWithRetry( + eq(new URL("http://example.com")), + eq("GET"), + eq(null), + eq(null), + any(HttpCore.ResponseHandler.class), + eq(false) + )).thenAnswer(invocation -> { + HttpCore.ResponseHandler responseHandler = invocation.getArgumentAt(4, HttpCore.ResponseHandler.class); + return responseHandler.handleResponse(mockResponse, null); + }); + + String result = HttpHelpers.getUrlString(mockHttpCore, "http://example.com"); + assertEquals("Test Response", result); + } + + @Test + public void getUrlString_emptyResponse_throwsAblyException() throws Exception { + HttpCore mockHttpCore = mock(HttpCore.class); + HttpCore.Response mockResponse = new HttpCore.Response(); + + when(mockHttpCore.httpExecuteWithRetry( + eq(new URL("http://example.com")), + eq("GET"), + eq(null), + eq(null), + any(HttpCore.ResponseHandler.class), + eq(false) + )).thenAnswer(invocation -> { + HttpCore.ResponseHandler responseHandler = invocation.getArgumentAt(4, HttpCore.ResponseHandler.class); + return responseHandler.handleResponse(mockResponse, null); + }); + + AblyException e = assertThrows(AblyException.class, () -> HttpHelpers.getUrlString(mockHttpCore, "http://example.com")); + assertEquals(500, e.errorInfo.statusCode); + assertEquals(50000, e.errorInfo.code); + assertEquals("Empty response body", e.errorInfo.message); + } +}