Skip to content

Commit 231a33d

Browse files
authored
Merge pull request #1111 from ably/ECO-5430/fix-npe-on-check-conectivity
[ECO-5430] fix: npe in the connectivity check
2 parents 4e25624 + c3591e3 commit 231a33d

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

lib/src/main/java/io/ably/lib/http/HttpHelpers.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.ably.lib.http;
22

3-
import java.io.IOException;
43
import java.net.URL;
54

65
import io.ably.lib.types.AblyException;
@@ -42,7 +41,11 @@ public void execute(HttpScheduler http, Callback<T> callback) throws AblyExcepti
4241
* @throws AblyException
4342
*/
4443
public static String getUrlString(HttpCore httpCore, String url) throws AblyException {
45-
return new String(getUrl(httpCore, url));
44+
byte[] bytes = getUrl(httpCore, url);
45+
if (bytes == null) {
46+
throw AblyException.fromErrorInfo(new ErrorInfo("Empty response body", 500, 50000));
47+
}
48+
return new String(bytes);
4649
}
4750

4851
/**
@@ -62,8 +65,8 @@ public byte[] handleResponse(HttpCore.Response response, ErrorInfo error) throws
6265
}
6366
return response.body;
6467
}});
65-
} catch(IOException ioe) {
66-
throw AblyException.fromThrowable(ioe);
68+
} catch (Exception e) {
69+
throw AblyException.fromThrowable(e);
6770
}
6871
}
6972

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)