Search in sources :

Example 1 with SecureConnectEnd

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

the class EventListenerTest method noSecureConnectsOnPooledConnection.

@Test
public void noSecureConnectsOnPooledConnection() throws IOException {
    enableTlsWithTunnel(false);
    server.enqueue(new MockResponse());
    server.enqueue(new MockResponse());
    client = client.newBuilder().dns(new DoubleInetAddressDns()).build();
    // Seed the pool.
    Call call1 = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response1 = call1.execute();
    assertThat(response1.code()).isEqualTo(200);
    response1.body().close();
    listener.clearAllEvents();
    Call call2 = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response2 = call2.execute();
    assertThat(response2.code()).isEqualTo(200);
    response2.body().close();
    List<String> recordedEvents = listener.recordedEventTypes();
    assertThat(recordedEvents).doesNotContain("SecureConnectStart");
    assertThat(recordedEvents).doesNotContain("SecureConnectEnd");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) DoubleInetAddressDns(okhttp3.internal.DoubleInetAddressDns) Test(org.junit.jupiter.api.Test)

Example 2 with SecureConnectEnd

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

the class LoggingEventListenerTest method secureGet.

@Test
public void secureGet() throws Exception {
    TestUtil.assumeNotWindows();
    platform.assumeNotBouncyCastle();
    server.useHttps(handshakeCertificates.sslSocketFactory(), false);
    url = server.url("/");
    server.enqueue(new MockResponse());
    Response response = client.newCall(request().build()).execute();
    assertThat(response.body()).isNotNull();
    response.body().bytes();
    platform.assumeHttp2Support();
    logRecorder.assertLogMatch("callStart: Request\\{method=GET, url=" + url + "\\}").assertLogMatch("proxySelectStart: " + url).assertLogMatch("proxySelectEnd: \\[DIRECT\\]").assertLogMatch("dnsStart: " + url.host()).assertLogMatch("dnsEnd: \\[.+\\]").assertLogMatch("connectStart: " + url.host() + "/.+ DIRECT").assertLogMatch("secureConnectStart").assertLogMatch("secureConnectEnd: Handshake\\{" + "tlsVersion=TLS_1_[23] " + "cipherSuite=TLS_.* " + "peerCertificates=\\[CN=localhost\\] " + "localCertificates=\\[\\]}").assertLogMatch("connectEnd: h2").assertLogMatch("connectionAcquired: Connection\\{" + url.host() + ":\\d+, proxy=DIRECT hostAddress=" + url.host() + "/.+ cipherSuite=.+ protocol=h2\\}").assertLogMatch("requestHeadersStart").assertLogMatch("requestHeadersEnd").assertLogMatch("responseHeadersStart").assertLogMatch("responseHeadersEnd: Response\\{protocol=h2, code=200, message=, url=" + url + "\\}").assertLogMatch("responseBodyStart").assertLogMatch("responseBodyEnd: byteCount=0").assertLogMatch("connectionReleased").assertLogMatch("callEnd").assertNoMoreLogs();
}
Also used : Response(okhttp3.Response) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 3 with SecureConnectEnd

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

the class DuplexTest method duplexWithRedirect.

/**
 * Duplex calls that have follow-ups are weird. By the time we know there's a follow-up we've
 * already split off another thread to stream the request body. Because we permit at most one
 * exchange at a time we break the request stream out from under that writer.
 */
@Test
public void duplexWithRedirect() throws Exception {
    enableProtocol(Protocol.HTTP_2);
    CountDownLatch duplexResponseSent = new CountDownLatch(1);
    listener = new RecordingEventListener() {

        @Override
        public void responseHeadersEnd(Call call, Response response) {
            try {
                // Wait for the server to send the duplex response before acting on the 301 response
                // and resetting the stream.
                duplexResponseSent.await();
            } catch (InterruptedException e) {
                throw new AssertionError();
            }
            super.responseHeadersEnd(call, response);
        }
    };
    client = client.newBuilder().eventListener(listener).build();
    MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders().setResponseCode(HttpURLConnection.HTTP_MOVED_PERM).addHeader("Location: /b"), new MockDuplexResponseBody().sendResponse("/a has moved!\n", duplexResponseSent).requestIOException().exhaustResponse());
    server.enqueue(new MockResponse().setBody("this is /b"));
    Call call = client.newCall(new Request.Builder().url(server.url("/")).post(new AsyncRequestBody()).build());
    try (Response response = call.execute()) {
        BufferedSource responseBody = response.body().source();
        assertThat(responseBody.readUtf8Line()).isEqualTo("this is /b");
    }
    BufferedSink requestBody = ((AsyncRequestBody) call.request().body()).takeSink();
    try {
        requestBody.writeUtf8("request body\n");
        requestBody.flush();
        fail();
    } catch (IOException expected) {
        assertThat(expected.getMessage()).isEqualTo("stream was reset: CANCEL");
    }
    mockDuplexResponseBody.awaitSuccess();
    assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "ProxySelectStart", "ProxySelectEnd", "DnsStart", "DnsEnd", "ConnectStart", "SecureConnectStart", "SecureConnectEnd", "ConnectEnd", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "RequestBodyStart", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "RequestHeadersStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "ConnectionReleased", "CallEnd", "RequestFailed");
}
Also used : MockResponse(mockwebserver3.MockResponse) AsyncRequestBody(okhttp3.internal.duplex.AsyncRequestBody) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) MockResponse(mockwebserver3.MockResponse) MockDuplexResponseBody(mockwebserver3.internal.duplex.MockDuplexResponseBody) BufferedSource(okio.BufferedSource) Test(org.junit.jupiter.api.Test)

Example 4 with SecureConnectEnd

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

the class DuplexTest method requestBodyEndsAfterResponseBody.

@Test
public void requestBodyEndsAfterResponseBody() throws Exception {
    enableProtocol(Protocol.HTTP_2);
    MockDuplexResponseBody mockDuplexResponseBody = enqueueResponseWithBody(new MockResponse().clearHeaders(), new MockDuplexResponseBody().exhaustResponse().receiveRequest("request A\n").exhaustRequest());
    Call call = client.newCall(new Request.Builder().url(server.url("/")).post(new AsyncRequestBody()).build());
    try (Response response = call.execute()) {
        BufferedSource responseBody = response.body().source();
        assertTrue(responseBody.exhausted());
        BufferedSink requestBody = ((AsyncRequestBody) call.request().body()).takeSink();
        requestBody.writeUtf8("request A\n");
        requestBody.close();
    }
    mockDuplexResponseBody.awaitSuccess();
    assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "ProxySelectStart", "ProxySelectEnd", "DnsStart", "DnsEnd", "ConnectStart", "SecureConnectStart", "SecureConnectEnd", "ConnectEnd", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "RequestBodyStart", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "RequestBodyEnd", "ConnectionReleased", "CallEnd");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) MockDuplexResponseBody(mockwebserver3.internal.duplex.MockDuplexResponseBody) AsyncRequestBody(okhttp3.internal.duplex.AsyncRequestBody) BufferedSink(okio.BufferedSink) BufferedSource(okio.BufferedSource) Test(org.junit.jupiter.api.Test)

Example 5 with SecureConnectEnd

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

Aggregations

MockResponse (mockwebserver3.MockResponse)7 Test (org.junit.jupiter.api.Test)7 IOException (java.io.IOException)2 MockDuplexResponseBody (mockwebserver3.internal.duplex.MockDuplexResponseBody)2 SecureConnectEnd (okhttp3.CallEvent.SecureConnectEnd)2 SecureConnectStart (okhttp3.CallEvent.SecureConnectStart)2 AsyncRequestBody (okhttp3.internal.duplex.AsyncRequestBody)2 BufferedSink (okio.BufferedSink)2 BufferedSource (okio.BufferedSource)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 Call (okhttp3.Call)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