Search in sources :

Example 1 with ClientConnectionException

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);
    }
}
Also used : ClientConnectionException(io.neow3j.protocol.exceptions.ClientConnectionException) Headers(okhttp3.Headers) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody)

Example 2 with ClientConnectionException

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");
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) ClientConnectionException(io.neow3j.protocol.exceptions.ClientConnectionException) OkHttpClient(okhttp3.OkHttpClient) Request(io.neow3j.protocol.core.Request) NeoBlockCount(io.neow3j.protocol.core.response.NeoBlockCount) Test(org.junit.Test)

Aggregations

ClientConnectionException (io.neow3j.protocol.exceptions.ClientConnectionException)2 Request (io.neow3j.protocol.core.Request)1 NeoBlockCount (io.neow3j.protocol.core.response.NeoBlockCount)1 Call (okhttp3.Call)1 Headers (okhttp3.Headers)1 OkHttpClient (okhttp3.OkHttpClient)1 RequestBody (okhttp3.RequestBody)1 Response (okhttp3.Response)1 ResponseBody (okhttp3.ResponseBody)1 Test (org.junit.Test)1