use of okhttp3.Connection in project okhttp by square.
the class ResponseCacheTest method requestMaxStale.
@Test
public void requestMaxStale() throws IOException {
server.enqueue(new MockResponse().setBody("A").addHeader("Cache-Control: max-age=120").addHeader("Date: " + formatDate(-4, TimeUnit.MINUTES)));
server.enqueue(new MockResponse().setBody("B"));
assertEquals("A", readAscii(openConnection(server.url("/").url())));
URLConnection connection = openConnection(server.url("/").url());
connection.addRequestProperty("Cache-Control", "max-stale=180");
assertEquals("A", readAscii(connection));
assertEquals("110 HttpURLConnection \"Response is stale\"", connection.getHeaderField("Warning"));
}
use of okhttp3.Connection in project okhttp by square.
the class ResponseCacheTest method emptyResponseHeaderNameFromCacheIsLenient.
@Test
public void emptyResponseHeaderNameFromCacheIsLenient() throws Exception {
Headers.Builder headers = new Headers.Builder().add("Cache-Control: max-age=120");
Internal.instance.addLenient(headers, ": A");
server.enqueue(new MockResponse().setHeaders(headers.build()).setBody("body"));
HttpURLConnection connection = openConnection(server.url("/").url());
assertEquals("A", connection.getHeaderField(""));
}
use of okhttp3.Connection in project okhttp by square.
the class ResponseCacheTest method clientSuppliedConditionWithoutCachedResult.
@Test
public void clientSuppliedConditionWithoutCachedResult() throws Exception {
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
HttpURLConnection connection = openConnection(server.url("/").url());
String clientIfModifiedSince = formatDate(-24, TimeUnit.HOURS);
connection.addRequestProperty("If-Modified-Since", clientIfModifiedSince);
assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, connection.getResponseCode());
assertEquals("", readAscii(connection));
}
use of okhttp3.Connection 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));
}
use of okhttp3.Connection in project okhttp by square.
the class ResponseCacheTest method useCachesFalseDoesNotReadFromCache.
@Test
public void useCachesFalseDoesNotReadFromCache() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
assertEquals("A", readAscii(openConnection(server.url("/").url())));
URLConnection connection = openConnection(server.url("/").url());
connection.setUseCaches(false);
assertEquals("B", readAscii(connection));
}
Aggregations