Search in sources :

Example 6 with SecureConnectStart

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

the class EventListenerTest method secureConnectWithTunnel.

@Test
public void secureConnectWithTunnel() throws IOException {
    enableTlsWithTunnel(true);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END));
    server.enqueue(new MockResponse());
    client = client.newBuilder().proxy(server.toProxyAddress()).build();
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertThat(response.code()).isEqualTo(200);
    response.body().close();
    SecureConnectStart secureStart = listener.removeUpToEvent(SecureConnectStart.class);
    assertThat(secureStart.getCall()).isSameAs(call);
    SecureConnectEnd secureEnd = listener.removeUpToEvent(SecureConnectEnd.class);
    assertThat(secureEnd.getCall()).isSameAs(call);
    assertThat(secureEnd.getHandshake()).isNotNull();
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) SecureConnectStart(okhttp3.CallEvent.SecureConnectStart) SecureConnectEnd(okhttp3.CallEvent.SecureConnectEnd) Test(org.junit.jupiter.api.Test)

Example 7 with SecureConnectStart

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

the class EventListenerTest method successfulSecureConnect.

@Test
public void successfulSecureConnect() throws IOException {
    enableTlsWithTunnel(false);
    server.enqueue(new MockResponse());
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertThat(response.code()).isEqualTo(200);
    response.body().close();
    SecureConnectStart secureStart = listener.removeUpToEvent(SecureConnectStart.class);
    assertThat(secureStart.getCall()).isSameAs(call);
    SecureConnectEnd secureEnd = listener.removeUpToEvent(SecureConnectEnd.class);
    assertThat(secureEnd.getCall()).isSameAs(call);
    assertThat(secureEnd.getHandshake()).isNotNull();
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) SecureConnectStart(okhttp3.CallEvent.SecureConnectStart) SecureConnectEnd(okhttp3.CallEvent.SecureConnectEnd) Test(org.junit.jupiter.api.Test)

Example 8 with SecureConnectStart

use of okhttp3.CallEvent.SecureConnectStart 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

MockResponse (mockwebserver3.MockResponse)8 Test (org.junit.jupiter.api.Test)8 IOException (java.io.IOException)3 SecureConnectStart (okhttp3.CallEvent.SecureConnectStart)3 MockDuplexResponseBody (mockwebserver3.internal.duplex.MockDuplexResponseBody)2 SecureConnectEnd (okhttp3.CallEvent.SecureConnectEnd)2 AsyncRequestBody (okhttp3.internal.duplex.AsyncRequestBody)2 BufferedSink (okio.BufferedSink)2 BufferedSource (okio.BufferedSource)2 InterruptedIOException (java.io.InterruptedIOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 Call (okhttp3.Call)1 CallFailed (okhttp3.CallEvent.CallFailed)1 OkHttpClient (okhttp3.OkHttpClient)1 RecordingEventListener (okhttp3.RecordingEventListener)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 DoubleInetAddressDns (okhttp3.internal.DoubleInetAddressDns)1 HeldCertificate (okhttp3.tls.HeldCertificate)1