use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class CacheTest method etagAndExpirationDateInThePast.
/** If both If-Modified-Since and If-None-Match conditions apply, send only If-None-Match. */
@Test
public void etagAndExpirationDateInThePast() throws Exception {
String lastModifiedDate = formatDate(-2, TimeUnit.HOURS);
RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("ETag: v1").addHeader("Last-Modified: " + lastModifiedDate).addHeader("Expires: " + formatDate(-1, TimeUnit.HOURS)));
assertEquals("v1", conditionalRequest.getHeader("If-None-Match"));
assertNull(conditionalRequest.getHeader("If-Modified-Since"));
}
use of okhttp3.mockwebserver.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"));
assertEquals("A", get(server.url("/")).body().string());
Request request = new Request.Builder().url(server.url("/")).header("Cache-Control", "max-stale=180").build();
Response response = client.newCall(request).execute();
assertEquals("B", response.body().string());
}
use of okhttp3.mockwebserver.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);
assertEquals("A", response1.body().string());
Response response2 = get(url);
assertEquals("A", response2.body().string());
assertNull(response2.header("Warning"));
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class CacheTest method varyAndHttps.
@Test
public void varyAndHttps() throws Exception {
server.useHttps(sslClient.socketFactory, 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(sslClient.socketFactory, sslClient.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();
assertEquals("A", response1.body().string());
Request request2 = new Request.Builder().url(url).header("Accept-Language", "en-US").build();
Response response2 = client.newCall(request2).execute();
assertEquals("A", response2.body().string());
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class ResponseCacheTest method otherStacks_cacheMissWithVary.
// Other stacks (e.g. older versions of OkHttp bundled inside Android apps) can interact with the
// default ResponseCache. We can't keep the Vary case working because we can't get to the Vary
// request headers after connect().
@Test
public void otherStacks_cacheMissWithVary() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
// Set the cache as the shared cache.
ResponseCache.setDefault(cache);
// Use the platform's HTTP stack.
URLConnection connection = server.url("/").url().openConnection();
assertFalse(connection instanceof OkHttpURLConnection);
connection.setRequestProperty("Accept-Language", "en-US");
assertEquals("A", readAscii(connection));
URLConnection connection2 = server.url("/").url().openConnection();
assertFalse(connection2 instanceof OkHttpURLConnection);
assertEquals("B", readAscii(connection2));
}
Aggregations