Search in sources :

Example 1 with CallStart

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

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

the class EventListenerTest method successfulCallEventSequenceWithListener.

@Test
public void successfulCallEventSequenceWithListener() throws IOException {
    server.enqueue(new MockResponse().setBody("abc"));
    client = client.newBuilder().addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build();
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertThat(response.code()).isEqualTo(200);
    assertThat(response.body().string()).isEqualTo("abc");
    response.body().close();
    assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "ProxySelectStart", "ProxySelectEnd", "DnsStart", "DnsEnd", "ConnectStart", "ConnectEnd", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "ConnectionReleased", "CallEnd");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Test(org.junit.jupiter.api.Test)

Example 3 with CallStart

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

Example 4 with CallStart

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

the class LoggingEventListenerTest method get.

@Test
public void get() throws Exception {
    TestUtil.assumeNotWindows();
    server.enqueue(new MockResponse().setBody("Hello!").setHeader("Content-Type", PLAIN));
    Response response = client.newCall(request().build()).execute();
    assertThat(response.body()).isNotNull();
    response.body().bytes();
    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("connectEnd: http/1.1").assertLogMatch("connectionAcquired: Connection\\{" + url.host() + ":\\d+, proxy=DIRECT hostAddress=" + url.host() + "/.+ cipherSuite=none protocol=http/1\\.1\\}").assertLogMatch("requestHeadersStart").assertLogMatch("requestHeadersEnd").assertLogMatch("responseHeadersStart").assertLogMatch("responseHeadersEnd: Response\\{protocol=http/1\\.1, code=200, message=OK, url=" + url + "\\}").assertLogMatch("responseBodyStart").assertLogMatch("responseBodyEnd: byteCount=6").assertLogMatch("connectionReleased").assertLogMatch("callEnd").assertNoMoreLogs();
}
Also used : Response(okhttp3.Response) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 5 with CallStart

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

Aggregations

MockResponse (mockwebserver3.MockResponse)8 Test (org.junit.jupiter.api.Test)8 IOException (java.io.IOException)4 Response (okhttp3.Response)3 MockDuplexResponseBody (mockwebserver3.internal.duplex.MockDuplexResponseBody)2 Call (okhttp3.Call)2 OkHttpClient (okhttp3.OkHttpClient)2 Request (okhttp3.Request)2 AsyncRequestBody (okhttp3.internal.duplex.AsyncRequestBody)2 BufferedSink (okio.BufferedSink)2 BufferedSource (okio.BufferedSource)2 InterruptedIOException (java.io.InterruptedIOException)1 UnknownHostException (java.net.UnknownHostException)1 Arrays.asList (java.util.Arrays.asList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 MockWebServer (mockwebserver3.MockWebServer)1 SocketPolicy (mockwebserver3.SocketPolicy)1 MockWebServerExtension (mockwebserver3.junit5.internal.MockWebServerExtension)1 ResponseFailed (okhttp3.CallEvent.ResponseFailed)1