use of io.neow3j.protocol.exceptions.ClientConnectionException in project neow3j by neow3j.
the class HttpService method performIO.
@Override
protected InputStream performIO(String request) throws IOException {
RequestBody requestBody = RequestBody.create(JSON_MEDIA_TYPE, request);
Headers headers = buildHeaders();
okhttp3.Request httpRequest = new okhttp3.Request.Builder().url(url).headers(headers).post(requestBody).build();
okhttp3.Response response = httpClient.newCall(httpRequest).execute();
ResponseBody responseBody = response.body();
if (response.isSuccessful()) {
if (responseBody != null) {
return buildInputStream(responseBody);
} else {
return null;
}
} else {
int code = response.code();
String text = responseBody == null ? "N/A" : responseBody.string();
throw new ClientConnectionException("Invalid response received: " + code + "; " + text);
}
}
use of io.neow3j.protocol.exceptions.ClientConnectionException in project neow3j by neow3j.
the class HttpServiceTest method httpException.
@Test
public void httpException() throws IOException {
String content = "400 error";
Response response = new Response.Builder().code(400).message("").body(ResponseBody.create(null, content)).request(new okhttp3.Request.Builder().url(HttpService.DEFAULT_URL).build()).protocol(Protocol.HTTP_1_1).build();
OkHttpClient httpClient = Mockito.mock(OkHttpClient.class);
Mockito.when(httpClient.newCall(Mockito.any())).thenAnswer(invocation -> {
Call call = Mockito.mock(Call.class);
Mockito.when(call.execute()).thenReturn(response);
return call;
});
HttpService mockedHttpService = new HttpService(httpClient);
Request<String, NeoBlockCount> request = new Request<>("getblockcount", Collections.emptyList(), mockedHttpService, NeoBlockCount.class);
try {
mockedHttpService.send(request, NeoBlockCount.class);
} catch (ClientConnectionException e) {
assertEquals(e.getMessage(), "Invalid response received: " + response.code() + "; " + content);
return;
}
fail("No exception");
}
Aggregations