use of mockwebserver3.MockResponse 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");
}
use of mockwebserver3.MockResponse 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 mockwebserver3.MockResponse in project okhttp by square.
the class EventListenerTest method secondCallEventSequence.
@Test
public void secondCallEventSequence() throws IOException {
enableTlsWithTunnel(false);
server.setProtocols(asList(Protocol.HTTP_2, Protocol.HTTP_1_1));
server.enqueue(new MockResponse());
server.enqueue(new MockResponse());
client.newCall(new Request.Builder().url(server.url("/")).build()).execute().close();
listener.removeUpToEvent(CallEnd.class);
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
response.close();
assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "ConnectionReleased", "CallEnd");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class EventListenerTest method cacheMiss.
@Test
public void cacheMiss() throws IOException {
enableCache();
server.enqueue(new MockResponse().setBody("abc"));
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.close();
assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "CacheMiss", "ProxySelectStart", "ProxySelectEnd", "DnsStart", "DnsEnd", "ConnectStart", "ConnectEnd", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "ConnectionReleased", "CallEnd");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class EventListenerTest method failedSecureConnect.
@Test
public void failedSecureConnect() {
enableTlsWithTunnel(false);
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE));
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
try {
call.execute();
fail();
} catch (IOException expected) {
}
SecureConnectStart secureStart = listener.removeUpToEvent(SecureConnectStart.class);
assertThat(secureStart.getCall()).isSameAs(call);
CallFailed callFailed = listener.removeUpToEvent(CallFailed.class);
assertThat(callFailed.getCall()).isSameAs(call);
assertThat(callFailed.getIoe()).isNotNull();
}
Aggregations