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");
}
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");
}
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);
}
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);
}
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);
}
Aggregations