use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class CacheTest method cacheControlNoCacheAndExpirationDateInTheFuture.
@Test
public void cacheControlNoCacheAndExpirationDateInTheFuture() throws Exception {
String lastModifiedDate = formatDate(-2, TimeUnit.HOURS);
RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("Last-Modified: " + lastModifiedDate).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).addHeader("Cache-Control: no-cache"));
assertEquals(lastModifiedDate, conditionalRequest.getHeader("If-Modified-Since"));
}
use of okhttp3.mockwebserver.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("/"));
assertEquals("A", response1.body().string());
assertEquals("identity", response1.header("Transfer-Encoding"));
Response response2 = get(server.url("/"));
assertEquals("A", response2.body().string());
assertEquals("identity", response2.header("Transfer-Encoding"));
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class ResponseCacheTest method temporaryRedirectNotCachedWithoutCachingHeader.
private void temporaryRedirectNotCachedWithoutCachingHeader(int responseCode) throws Exception {
server.enqueue(new MockResponse().setResponseCode(responseCode).addHeader("Location", "/a"));
server.enqueue(new MockResponse().setBody("a"));
server.enqueue(new MockResponse().setBody("b"));
URL url = server.url("/").url();
assertEquals("a", readAscii(openConnection(url)));
assertEquals("b", readAscii(openConnection(url)));
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class ResponseCacheTest method otherStacks_cacheMissWithVaryAcceptEncoding.
// 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(). Accept-Encoding has special behavior so we test it explicitly.
@Test
public void otherStacks_cacheMissWithVaryAcceptEncoding() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Encoding").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);
assertEquals("A", readAscii(connection));
URLConnection connection2 = server.url("/").url().openConnection();
assertFalse(connection2 instanceof OkHttpURLConnection);
assertEquals("B", readAscii(connection2));
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class ResponseCacheTest method varyMatchesUnchangedRequestHeaderField.
@Test
public void varyMatchesUnchangedRequestHeaderField() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
URL url = server.url("/").url();
HttpURLConnection frenchConnection1 = openConnection(url);
frenchConnection1.setRequestProperty("Accept-Language", "fr-CA");
assertEquals("A", readAscii(frenchConnection1));
HttpURLConnection frenchConnection2 = openConnection(url);
frenchConnection2.setRequestProperty("Accept-Language", "fr-CA");
assertEquals("A", readAscii(frenchConnection2));
}
Aggregations