use of mockwebserver3.MockResponse in project okhttp by square.
the class EventListenerTest method conditionalCache.
@Test
public void conditionalCache() throws IOException {
enableCache();
server.enqueue(new MockResponse().addHeader("ETag: v1").setBody("abc"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
assertThat(response.code()).isEqualTo(200);
response.close();
listener.clearAllEvents();
call = call.clone();
response = call.execute();
assertThat(response.code()).isEqualTo(200);
assertThat(response.body().string()).isEqualTo("abc");
response.close();
assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "CacheConditionalHit", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "CacheHit", "ConnectionReleased", "CallEnd");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class EventListenerTest method successfulDnsLookup.
@Test
public void successfulDnsLookup() 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();
DnsStart dnsStart = listener.removeUpToEvent(DnsStart.class);
assertThat(dnsStart.getCall()).isSameAs(call);
assertThat(dnsStart.getDomainName()).isEqualTo(server.getHostName());
DnsEnd dnsEnd = listener.removeUpToEvent(DnsEnd.class);
assertThat(dnsEnd.getCall()).isSameAs(call);
assertThat(dnsEnd.getDomainName()).isEqualTo(server.getHostName());
assertThat(dnsEnd.getInetAddressList().size()).isEqualTo(1);
}
use of mockwebserver3.MockResponse 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());
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class EventListenerTest method successfulCallEventSequence.
@Test
public void successfulCallEventSequence() throws IOException {
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.body().close();
assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "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 timeToFirstByteGapBetweenResponseHeaderStartAndEnd.
@Test
public void timeToFirstByteGapBetweenResponseHeaderStartAndEnd() throws IOException {
long responseHeadersStartDelay = 250L;
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.EXPECT_CONTINUE).setHeadersDelay(responseHeadersStartDelay, TimeUnit.MILLISECONDS));
Request request = new Request.Builder().url(server.url("/")).header("Expect", "100-continue").post(RequestBody.create("abc", MediaType.get("text/plain"))).build();
Call call = client.newCall(request);
try (Response response = call.execute()) {
assertThat(response.body().string()).isEqualTo("");
}
listener.removeUpToEvent(ResponseHeadersStart.class);
listener.takeEvent(RequestBodyStart.class, 0L);
listener.takeEvent(RequestBodyEnd.class, 0L);
listener.takeEvent(ResponseHeadersEnd.class, responseHeadersStartDelay);
}
Aggregations