Search in sources :

Example 1 with ConnectionAcquired

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

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

the class EventListenerTest method pooledConnectionFound.

@Test
public void pooledConnectionFound() throws IOException {
    server.enqueue(new MockResponse());
    server.enqueue(new MockResponse());
    // 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();
    ConnectionAcquired connectionAcquired1 = listener.removeUpToEvent(ConnectionAcquired.class);
    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();
    ConnectionAcquired connectionAcquired2 = listener.removeUpToEvent(ConnectionAcquired.class);
    assertThat(connectionAcquired2.getConnection()).isSameAs(connectionAcquired1.getConnection());
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) ConnectionAcquired(okhttp3.CallEvent.ConnectionAcquired) Test(org.junit.jupiter.api.Test)

Example 3 with ConnectionAcquired

use of okhttp3.CallEvent.ConnectionAcquired 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 4 with ConnectionAcquired

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

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

Aggregations

MockResponse (mockwebserver3.MockResponse)10 Test (org.junit.jupiter.api.Test)9 IOException (java.io.IOException)4 ConnectionAcquired (okhttp3.CallEvent.ConnectionAcquired)3 InterruptedIOException (java.io.InterruptedIOException)2 MockDuplexResponseBody (mockwebserver3.internal.duplex.MockDuplexResponseBody)2 Response (okhttp3.Response)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 CallFailed (okhttp3.CallEvent.CallFailed)1 ResponseFailed (okhttp3.CallEvent.ResponseFailed)1 OkHttpClient (okhttp3.OkHttpClient)1 RecordingEventListener (okhttp3.RecordingEventListener)1 Request (okhttp3.Request)1 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)1 HeldCertificate (okhttp3.tls.HeldCertificate)1