use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method etagConditionCanBeNonAscii.
@Test
public void etagConditionCanBeNonAscii() throws Exception {
server.enqueue(new MockResponse().addHeaderLenient("Etag", "α").addHeader("Cache-Control: max-age=0").setBody("abcd"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
Response response1 = get(server.url("/"));
assertThat(response1.body().string()).isEqualTo("abcd");
Response response2 = get(server.url("/"));
assertThat(response2.body().string()).isEqualTo("abcd");
assertThat(server.takeRequest().getHeader("If-None-Match")).isNull();
assertThat(server.takeRequest().getHeader("If-None-Match")).isEqualTo("α");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method varyAndHttps.
@Test
public void varyAndHttps() throws Exception {
server.useHttps(handshakeCertificates.sslSocketFactory(), false);
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
client = client.newBuilder().sslSocketFactory(handshakeCertificates.sslSocketFactory(), handshakeCertificates.trustManager()).hostnameVerifier(NULL_HOSTNAME_VERIFIER).build();
HttpUrl url = server.url("/");
Request request1 = new Request.Builder().url(url).header("Accept-Language", "en-US").build();
Response response1 = client.newCall(request1).execute();
assertThat(response1.body().string()).isEqualTo("A");
Request request2 = new Request.Builder().url(url).header("Accept-Language", "en-US").build();
Response response2 = client.newCall(request2).execute();
assertThat(response2.body().string()).isEqualTo("A");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method defaultExpirationDateFullyCachedForLessThan24Hours.
@Test
public void defaultExpirationDateFullyCachedForLessThan24Hours() throws Exception {
// last modified: 105 seconds ago
// served: 5 seconds ago
// default lifetime: (105 - 5) / 10 = 10 seconds
// expires: 10 seconds from served date = 5 seconds from now
server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-105, TimeUnit.SECONDS)).addHeader("Date: " + formatDate(-5, TimeUnit.SECONDS)).setBody("A"));
HttpUrl url = server.url("/");
Response response1 = get(url);
assertThat(response1.body().string()).isEqualTo("A");
Response response2 = get(url);
assertThat(response2.body().string()).isEqualTo("A");
assertThat(response2.header("Warning")).isNull();
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method authorizationRequestFullyCached.
@Test
public void authorizationRequestFullyCached() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
HttpUrl url = server.url("/");
Request request = new Request.Builder().url(url).header("Authorization", "password").build();
Response response = client.newCall(request).execute();
assertThat(response.body().string()).isEqualTo("A");
assertThat(get(url).body().string()).isEqualTo("A");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method requestMaxStaleNotHonoredWithMustRevalidate.
@Test
public void requestMaxStaleNotHonoredWithMustRevalidate() throws IOException {
server.enqueue(new MockResponse().setBody("A").addHeader("Cache-Control: max-age=120, must-revalidate").addHeader("Date: " + formatDate(-4, TimeUnit.MINUTES)));
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-stale=180").build();
Response response = client.newCall(request).execute();
assertThat(response.body().string()).isEqualTo("B");
}
Aggregations