Search in sources :

Example 6 with CallFailed

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

the class EventListenerTest method responseBodyFail.

private void responseBodyFail(Protocol expectedProtocol) throws IOException {
    // Use a 2 MiB body so the disconnect won't happen until the client has read some data.
    // 2 MiB
    int responseBodySize = 2 * 1024 * 1024;
    server.enqueue(new MockResponse().setBody(new Buffer().write(new byte[responseBodySize])).setSocketPolicy(SocketPolicy.DISCONNECT_DURING_RESPONSE_BODY));
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    if (expectedProtocol == Protocol.HTTP_2) {
        // soft failure since client may not support depending on Platform
        assumeThat(response, matchesProtocol(Protocol.HTTP_2));
    }
    assertThat(response.protocol()).isEqualTo(expectedProtocol);
    try {
        response.body().string();
        fail();
    } catch (IOException expected) {
    }
    CallFailed callFailed = listener.removeUpToEvent(CallFailed.class);
    assertThat(callFailed.getIoe()).isNotNull();
}
Also used : Buffer(okio.Buffer) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) CallFailed(okhttp3.CallEvent.CallFailed) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 7 with CallFailed

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

the class EventListenerTest method emptyDnsLookup.

@Test
public void emptyDnsLookup() {
    Dns emptyDns = hostname -> Collections.emptyList();
    client = client.newBuilder().dns(emptyDns).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 : HttpURLConnection(java.net.HttpURLConnection) SecureConnectStart(okhttp3.CallEvent.SecureConnectStart) CoreMatchers(org.hamcrest.CoreMatchers) BeforeEach(org.junit.jupiter.api.BeforeEach) RealConnectionPool(okhttp3.internal.connection.RealConnectionPool) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HandshakeCertificates(okhttp3.tls.HandshakeCertificates) MockWebServer(mockwebserver3.MockWebServer) InetAddress(java.net.InetAddress) Proxy(java.net.Proxy) BufferedSink(okio.BufferedSink) Arrays.asList(java.util.Arrays.asList) Duration(java.time.Duration) RequestBodyEnd(okhttp3.CallEvent.RequestBodyEnd) Tag(org.junit.jupiter.api.Tag) ConnectionAcquired(okhttp3.CallEvent.ConnectionAcquired) CallStart(okhttp3.CallEvent.CallStart) DnsStart(okhttp3.CallEvent.DnsStart) SecureConnectEnd(okhttp3.CallEvent.SecureConnectEnd) ResponseBodyStart(okhttp3.CallEvent.ResponseBodyStart) ConnectEnd(okhttp3.CallEvent.ConnectEnd) ResponseBodyEnd(okhttp3.CallEvent.ResponseBodyEnd) ConnectStart(okhttp3.CallEvent.ConnectStart) InetSocketAddress(java.net.InetSocketAddress) Flaky(okhttp3.testing.Flaky) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) RequestBodyStart(okhttp3.CallEvent.RequestBodyStart) TlsUtil.localhost(okhttp3.tls.internal.TlsUtil.localhost) ResponseHeadersEnd(okhttp3.CallEvent.ResponseHeadersEnd) MatcherAssert(org.hamcrest.MatcherAssert) CallEnd(okhttp3.CallEvent.CallEnd) PlatformRule(okhttp3.testing.PlatformRule) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) MockResponse(mockwebserver3.MockResponse) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assume.assumeThat(org.junit.Assume.assumeThat) RequestHeadersEnd(okhttp3.CallEvent.RequestHeadersEnd) ResponseHeadersStart(okhttp3.CallEvent.ResponseHeadersStart) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) DoubleInetAddressDns(okhttp3.internal.DoubleInetAddressDns) DnsEnd(okhttp3.CallEvent.DnsEnd) ResponseFailed(okhttp3.CallEvent.ResponseFailed) ConnectFailed(okhttp3.CallEvent.ConnectFailed) InterruptedIOException(java.io.InterruptedIOException) SocketPolicy(mockwebserver3.SocketPolicy) BaseMatcher(org.hamcrest.BaseMatcher) CoreMatchers.any(org.hamcrest.CoreMatchers.any) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Nullable(javax.annotation.Nullable) ConnectionReleased(okhttp3.CallEvent.ConnectionReleased) Description(org.hamcrest.Description) Buffer(okio.Buffer) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) AfterEach(org.junit.jupiter.api.AfterEach) RequestHeadersStart(okhttp3.CallEvent.RequestHeadersStart) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Matcher(org.hamcrest.Matcher) Collections(java.util.Collections) Timeout(org.junit.jupiter.api.Timeout) CallFailed(okhttp3.CallEvent.CallFailed) DoubleInetAddressDns(okhttp3.internal.DoubleInetAddressDns) CallFailed(okhttp3.CallEvent.CallFailed) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 8 with CallFailed

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

the class ClientAuthTest method invalidClientAuthEvents.

@Test
public void invalidClientAuthEvents() throws Throwable {
    server.enqueue(new MockResponse().setBody("abc"));
    clientCert = new HeldCertificate.Builder().signedBy(clientIntermediateCa).serialNumber(4L).commonName("Jethro Willis").addSubjectAlternativeName("jethrowillis.com").validityInterval(1, 2).build();
    OkHttpClient client = buildClient(clientCert, clientIntermediateCa.certificate());
    RecordingEventListener listener = new RecordingEventListener();
    client = client.newBuilder().eventListener(listener).build();
    SSLSocketFactory socketFactory = buildServerSslSocketFactory();
    server.useHttps(socketFactory, false);
    server.requireClientAuth();
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    try {
        call.execute();
        fail();
    } catch (IOException expected) {
    }
    // Observed Events are variable
    // JDK 14
    // CallStart, ProxySelectStart, ProxySelectEnd, DnsStart, DnsEnd, ConnectStart, SecureConnectStart,
    // SecureConnectEnd, ConnectEnd, ConnectionAcquired, RequestHeadersStart, RequestHeadersEnd,
    // ResponseFailed, ConnectionReleased, CallFailed
    // JDK 8
    // CallStart, ProxySelectStart, ProxySelectEnd, DnsStart, DnsEnd, ConnectStart, SecureConnectStart,
    // ConnectFailed, CallFailed
    // Gradle - JDK 11
    // CallStart, ProxySelectStart, ProxySelectEnd, DnsStart, DnsEnd, ConnectStart, SecureConnectStart,
    // SecureConnectEnd, ConnectFailed, CallFailed
    List<String> recordedEventTypes = listener.recordedEventTypes();
    assertThat(recordedEventTypes).startsWith("CallStart", "ProxySelectStart", "ProxySelectEnd", "DnsStart", "DnsEnd", "ConnectStart", "SecureConnectStart");
    assertThat(recordedEventTypes).endsWith("CallFailed");
}
Also used : MockResponse(mockwebserver3.MockResponse) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) HeldCertificate(okhttp3.tls.HeldCertificate) Request(okhttp3.Request) IOException(java.io.IOException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) RecordingEventListener(okhttp3.RecordingEventListener) 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