Search in sources :

Example 1 with CallFailed

use of okhttp3.CallEvent.CallFailed in project okhttp by square.

the class EventListenerTest method failedDribbledCallEventSequence.

@Test
public void failedDribbledCallEventSequence() throws IOException {
    server.enqueue(new MockResponse().setBody("0123456789").throttleBody(2, 100, TimeUnit.MILLISECONDS).setSocketPolicy(SocketPolicy.DISCONNECT_DURING_RESPONSE_BODY));
    client = client.newBuilder().protocols(Collections.singletonList(Protocol.HTTP_1_1)).readTimeout(Duration.ofMillis(250)).build();
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    try {
        response.body().string();
        fail();
    } catch (IOException expected) {
        assertThat(expected.getMessage()).isEqualTo("unexpected end of stream");
    }
    assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "ProxySelectStart", "ProxySelectEnd", "DnsStart", "DnsEnd", "ConnectStart", "ConnectEnd", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseFailed", "ConnectionReleased", "CallFailed");
    ResponseFailed responseFailed = listener.removeUpToEvent(ResponseFailed.class);
    assertThat(responseFailed.getIoe().getMessage()).isEqualTo("unexpected end of stream");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) ResponseFailed(okhttp3.CallEvent.ResponseFailed) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 2 with CallFailed

use of okhttp3.CallEvent.CallFailed in project okhttp by square.

the class EventListenerTest method failedSecureConnect.

@Test
public void failedSecureConnect() {
    enableTlsWithTunnel(false);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE));
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    try {
        call.execute();
        fail();
    } catch (IOException expected) {
    }
    SecureConnectStart secureStart = listener.removeUpToEvent(SecureConnectStart.class);
    assertThat(secureStart.getCall()).isSameAs(call);
    CallFailed callFailed = listener.removeUpToEvent(CallFailed.class);
    assertThat(callFailed.getCall()).isSameAs(call);
    assertThat(callFailed.getIoe()).isNotNull();
}
Also used : MockResponse(mockwebserver3.MockResponse) SecureConnectStart(okhttp3.CallEvent.SecureConnectStart) CallFailed(okhttp3.CallEvent.CallFailed) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 3 with CallFailed

use of okhttp3.CallEvent.CallFailed in project okhttp by square.

the class EventListenerTest method failedDnsLookup.

@Test
public void failedDnsLookup() {
    client = client.newBuilder().dns(new FakeDns()).build();
    Call call = client.newCall(new Request.Builder().url("http://fakeurl/").build());
    try {
        call.execute();
        fail();
    } catch (IOException expected) {
    }
    listener.removeUpToEvent(DnsStart.class);
    CallFailed callFailed = listener.removeUpToEvent(CallFailed.class);
    assertThat(callFailed.getCall()).isSameAs(call);
    assertThat(callFailed.getIoe()).isInstanceOf(UnknownHostException.class);
}
Also used : CallFailed(okhttp3.CallEvent.CallFailed) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 4 with CallFailed

use of okhttp3.CallEvent.CallFailed in project okhttp by square.

the class EventListenerTest method requestBodyFail.

private void requestBodyFail(@Nullable Protocol expectedProtocol) {
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_DURING_REQUEST_BODY));
    NonCompletingRequestBody request = new NonCompletingRequestBody();
    Call call = client.newCall(new Request.Builder().url(server.url("/")).post(request).build());
    try {
        call.execute();
        fail();
    } catch (IOException expected) {
    }
    if (expectedProtocol != null) {
        ConnectionAcquired connectionAcquired = listener.removeUpToEvent(ConnectionAcquired.class);
        assertThat(connectionAcquired.getConnection().protocol()).isEqualTo(expectedProtocol);
    }
    CallFailed callFailed = listener.removeUpToEvent(CallFailed.class);
    assertThat(callFailed.getIoe()).isNotNull();
    assertThat(request.ioe).isNotNull();
}
Also used : MockResponse(mockwebserver3.MockResponse) ConnectionAcquired(okhttp3.CallEvent.ConnectionAcquired) CallFailed(okhttp3.CallEvent.CallFailed) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 5 with CallFailed

use of okhttp3.CallEvent.CallFailed in project okhttp by square.

the class LoggingEventListenerTest method dnsFail.

@Test
public void dnsFail() throws IOException {
    client = new OkHttpClient.Builder().dns(hostname -> {
        throw new UnknownHostException("reason");
    }).eventListenerFactory(loggingEventListenerFactory).build();
    try {
        client.newCall(request().build()).execute();
        fail();
    } catch (UnknownHostException expected) {
    }
    logRecorder.assertLogMatch("callStart: Request\\{method=GET, url=" + url + "\\}").assertLogMatch("proxySelectStart: " + url).assertLogMatch("proxySelectEnd: \\[DIRECT\\]").assertLogMatch("dnsStart: " + url.host()).assertLogMatch("callFailed: java.net.UnknownHostException: reason").assertNoMoreLogs();
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) BeforeEach(org.junit.jupiter.api.BeforeEach) HTTP_1_1(okhttp3.Protocol.HTTP_1_1) HTTP_2(okhttp3.Protocol.HTTP_2) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockWebServerExtension(mockwebserver3.junit5.internal.MockWebServerExtension) HandshakeCertificates(okhttp3.tls.HandshakeCertificates) MockWebServer(mockwebserver3.MockWebServer) RequestBody(okhttp3.RequestBody) SocketPolicy(mockwebserver3.SocketPolicy) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Arrays.asList(java.util.Arrays.asList) EventListener(okhttp3.EventListener) Response(okhttp3.Response) Call(okhttp3.Call) MediaType(okhttp3.MediaType) Request(okhttp3.Request) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) OkHttpClientTestRule(okhttp3.OkHttpClientTestRule) Test(org.junit.jupiter.api.Test) TlsUtil.localhost(okhttp3.tls.internal.TlsUtil.localhost) OkHttpClient(okhttp3.OkHttpClient) PlatformRule(okhttp3.testing.PlatformRule) MockResponse(mockwebserver3.MockResponse) TestUtil(okhttp3.TestUtil) HttpUrl(okhttp3.HttpUrl) UnknownHostException(java.net.UnknownHostException) Test(org.junit.jupiter.api.Test)

Aggregations

IOException (java.io.IOException)8 MockResponse (mockwebserver3.MockResponse)7 InterruptedIOException (java.io.InterruptedIOException)6 Test (org.junit.jupiter.api.Test)6 CallFailed (okhttp3.CallEvent.CallFailed)5 UnknownHostException (java.net.UnknownHostException)2 Arrays.asList (java.util.Arrays.asList)2 MockWebServer (mockwebserver3.MockWebServer)2 SocketPolicy (mockwebserver3.SocketPolicy)2 Call (okhttp3.Call)2 ConnectionAcquired (okhttp3.CallEvent.ConnectionAcquired)2 ResponseFailed (okhttp3.CallEvent.ResponseFailed)2 Buffer (okio.Buffer)2 File (java.io.File)1 HttpURLConnection (java.net.HttpURLConnection)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 Duration (java.time.Duration)1 Collections (java.util.Collections)1