use of mockwebserver3.MockResponse in project okhttp by square.
the class EventListenerTest method multipleDnsLookupsForSingleCall.
@Test
public void multipleDnsLookupsForSingleCall() throws IOException {
server.enqueue(new MockResponse().setResponseCode(301).setHeader("Location", "http://www.fakeurl:" + server.getPort()));
server.enqueue(new MockResponse());
FakeDns dns = new FakeDns();
dns.set("fakeurl", client.dns().lookup(server.getHostName()));
dns.set("www.fakeurl", client.dns().lookup(server.getHostName()));
client = client.newBuilder().dns(dns).build();
Call call = client.newCall(new Request.Builder().url("http://fakeurl:" + server.getPort()).build());
Response response = call.execute();
assertThat(response.code()).isEqualTo(200);
response.body().close();
listener.removeUpToEvent(DnsStart.class);
listener.removeUpToEvent(DnsEnd.class);
listener.removeUpToEvent(DnsStart.class);
listener.removeUpToEvent(DnsEnd.class);
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class EventListenerTest method emptyResponseBodyConnectionClose.
@Test
public void emptyResponseBodyConnectionClose() throws IOException {
server.enqueue(new MockResponse().addHeader("Connection", "close").setBody(""));
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
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 conditionalCacheMiss.
@Test
public void conditionalCacheMiss() throws IOException {
enableCache();
server.enqueue(new MockResponse().addHeader("ETag: v1").setBody("abc"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_OK).addHeader("ETag: v2").setBody("abd"));
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("abd");
response.close();
assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "CacheConditionalHit", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "CacheMiss", "ResponseBodyStart", "ResponseBodyEnd", "ConnectionReleased", "CallEnd");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class EventListenerTest method requestBodySuccess.
private void requestBodySuccess(RequestBody body, Matcher<Long> requestBodyBytes, Matcher<Long> responseHeaderLength) throws IOException {
server.enqueue(new MockResponse().setResponseCode(200).setBody("World!"));
Call call = client.newCall(new Request.Builder().url(server.url("/")).post(body).build());
Response response = call.execute();
assertThat(response.body().string()).isEqualTo("World!");
assertBytesReadWritten(listener, any(Long.class), requestBodyBytes, responseHeaderLength, equalTo(6L));
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method requestMaxAge.
@Test
public void requestMaxAge() throws IOException {
server.enqueue(new MockResponse().setBody("A").addHeader("Last-Modified: " + formatDate(-2, TimeUnit.HOURS)).addHeader("Date: " + formatDate(-1, TimeUnit.MINUTES)).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)));
server.enqueue(new MockResponse().setBody("B"));
assertThat(get(server.url("/")).body().string()).isEqualTo("A");
Request request = new Request.Builder().url(server.url("/")).header("Cache-Control", "max-age=30").build();
Response response = client.newCall(request).execute();
assertThat(response.body().string()).isEqualTo("B");
}
Aggregations