use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method postInvalidatesCacheWithUncacheableResponse.
@Test
public void postInvalidatesCacheWithUncacheableResponse() throws Exception {
// 1. Seed the cache.
// 2. Invalidate it with an uncacheable response.
// 3. Expect a cache miss.
server.enqueue(new MockResponse().setBody("A").addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)));
server.enqueue(new MockResponse().setBody("B").setResponseCode(500));
server.enqueue(new MockResponse().setBody("C"));
HttpUrl url = server.url("/");
assertThat(get(url).body().string()).isEqualTo("A");
Request request = new Request.Builder().url(url).method("POST", requestBodyOrNull("POST")).build();
Response invalidate = client.newCall(request).execute();
assertThat(invalidate.body().string()).isEqualTo("B");
assertThat(get(url).body().string()).isEqualTo("C");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method assertConditionallyCached.
/**
* @return the request with the conditional get headers.
*/
private RecordedRequest assertConditionallyCached(MockResponse response) throws Exception {
// scenario 1: condition succeeds
server.enqueue(response.setBody("A").setStatus("HTTP/1.1 200 A-OK"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
// scenario 2: condition fails
server.enqueue(response.setBody("B").setStatus("HTTP/1.1 200 B-OK"));
server.enqueue(new MockResponse().setStatus("HTTP/1.1 200 C-OK").setBody("C"));
HttpUrl valid = server.url("/valid");
Response response1 = get(valid);
assertThat(response1.body().string()).isEqualTo("A");
assertThat(response1.code()).isEqualTo(HttpURLConnection.HTTP_OK);
assertThat(response1.message()).isEqualTo("A-OK");
Response response2 = get(valid);
assertThat(response2.body().string()).isEqualTo("A");
assertThat(response2.code()).isEqualTo(HttpURLConnection.HTTP_OK);
assertThat(response2.message()).isEqualTo("A-OK");
HttpUrl invalid = server.url("/invalid");
Response response3 = get(invalid);
assertThat(response3.body().string()).isEqualTo("B");
assertThat(response3.code()).isEqualTo(HttpURLConnection.HTTP_OK);
assertThat(response3.message()).isEqualTo("B-OK");
Response response4 = get(invalid);
assertThat(response4.body().string()).isEqualTo("C");
assertThat(response4.code()).isEqualTo(HttpURLConnection.HTTP_OK);
assertThat(response4.message()).isEqualTo("C-OK");
// regular get
server.takeRequest();
// conditional get
return server.takeRequest();
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method getHeadersReturnsCachedHopByHopHeaders.
@Test
public void getHeadersReturnsCachedHopByHopHeaders() throws Exception {
server.enqueue(new MockResponse().addHeader("Transfer-Encoding: identity").addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").setBody("A"));
server.enqueue(new MockResponse().addHeader("Transfer-Encoding: none").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
Response response1 = get(server.url("/"));
assertThat(response1.body().string()).isEqualTo("A");
assertThat(response1.header("Transfer-Encoding")).isEqualTo("identity");
Response response2 = get(server.url("/"));
assertThat(response2.body().string()).isEqualTo("A");
assertThat(response2.header("Transfer-Encoding")).isEqualTo("identity");
}
use of mockwebserver3.MockResponse 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 mockwebserver3.MockResponse 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);
}
Aggregations