Search in sources :

Example 1 with ConnectEnd

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

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

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

the class EventListenerTest method authenticatingTunnelProxyConnect.

@Test
public void authenticatingTunnelProxyConnect() throws IOException {
    enableTlsWithTunnel(true);
    server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\"").addHeader("Connection: close"));
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END));
    server.enqueue(new MockResponse());
    client = client.newBuilder().proxy(server.toProxyAddress()).proxyAuthenticator(new RecordingOkAuthenticator("password", "Basic")).build();
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertThat(response.code()).isEqualTo(200);
    response.body().close();
    listener.removeUpToEvent(ConnectStart.class);
    ConnectEnd connectEnd = listener.removeUpToEvent(ConnectEnd.class);
    assertThat(connectEnd.getProtocol()).isNull();
    listener.removeUpToEvent(ConnectStart.class);
    listener.removeUpToEvent(ConnectEnd.class);
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) SecureConnectEnd(okhttp3.CallEvent.SecureConnectEnd) ConnectEnd(okhttp3.CallEvent.ConnectEnd) Test(org.junit.jupiter.api.Test)

Example 4 with ConnectEnd

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

the class EventListenerTest method successfulConnect.

@Test
public void successfulConnect() throws IOException {
    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();
    InetAddress address = client.dns().lookup(server.getHostName()).get(0);
    InetSocketAddress expectedAddress = new InetSocketAddress(address, server.getPort());
    ConnectStart connectStart = listener.removeUpToEvent(ConnectStart.class);
    assertThat(connectStart.getCall()).isSameAs(call);
    assertThat(connectStart.getInetSocketAddress()).isEqualTo(expectedAddress);
    assertThat(connectStart.getProxy()).isEqualTo(Proxy.NO_PROXY);
    ConnectEnd connectEnd = listener.removeUpToEvent(ConnectEnd.class);
    assertThat(connectEnd.getCall()).isSameAs(call);
    assertThat(connectEnd.getInetSocketAddress()).isEqualTo(expectedAddress);
    assertThat(connectEnd.getProtocol()).isEqualTo(Protocol.HTTP_1_1);
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) SecureConnectEnd(okhttp3.CallEvent.SecureConnectEnd) ConnectEnd(okhttp3.CallEvent.ConnectEnd) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) SecureConnectStart(okhttp3.CallEvent.SecureConnectStart) ConnectStart(okhttp3.CallEvent.ConnectStart) Test(org.junit.jupiter.api.Test)

Example 5 with ConnectEnd

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

the class EventListenerTest method successfulSocksProxyConnect.

@Test
public void successfulSocksProxyConnect() throws Exception {
    server.enqueue(new MockResponse());
    socksProxy = new SocksProxy();
    socksProxy.play();
    Proxy proxy = socksProxy.proxy();
    client = client.newBuilder().proxy(proxy).build();
    Call call = client.newCall(new Request.Builder().url("http://" + SocksProxy.HOSTNAME_THAT_ONLY_THE_PROXY_KNOWS + ":" + server.getPort()).build());
    Response response = call.execute();
    assertThat(response.code()).isEqualTo(200);
    response.body().close();
    InetSocketAddress expectedAddress = InetSocketAddress.createUnresolved(SocksProxy.HOSTNAME_THAT_ONLY_THE_PROXY_KNOWS, server.getPort());
    ConnectStart connectStart = listener.removeUpToEvent(ConnectStart.class);
    assertThat(connectStart.getCall()).isSameAs(call);
    assertThat(connectStart.getInetSocketAddress()).isEqualTo(expectedAddress);
    assertThat(connectStart.getProxy()).isEqualTo(proxy);
    ConnectEnd connectEnd = listener.removeUpToEvent(ConnectEnd.class);
    assertThat(connectEnd.getCall()).isSameAs(call);
    assertThat(connectEnd.getInetSocketAddress()).isEqualTo(expectedAddress);
    assertThat(connectEnd.getProtocol()).isEqualTo(Protocol.HTTP_1_1);
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Proxy(java.net.Proxy) SecureConnectEnd(okhttp3.CallEvent.SecureConnectEnd) ConnectEnd(okhttp3.CallEvent.ConnectEnd) InetSocketAddress(java.net.InetSocketAddress) SecureConnectStart(okhttp3.CallEvent.SecureConnectStart) ConnectStart(okhttp3.CallEvent.ConnectStart) Test(org.junit.jupiter.api.Test)

Aggregations

MockResponse (mockwebserver3.MockResponse)11 Test (org.junit.jupiter.api.Test)11 ConnectEnd (okhttp3.CallEvent.ConnectEnd)4 SecureConnectEnd (okhttp3.CallEvent.SecureConnectEnd)4 IOException (java.io.IOException)3 InetSocketAddress (java.net.InetSocketAddress)3 ConnectStart (okhttp3.CallEvent.ConnectStart)3 SecureConnectStart (okhttp3.CallEvent.SecureConnectStart)3 InetAddress (java.net.InetAddress)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 InterruptedIOException (java.io.InterruptedIOException)1 Proxy (java.net.Proxy)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 Call (okhttp3.Call)1 ResponseFailed (okhttp3.CallEvent.ResponseFailed)1