use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class CacheTest method serverReturnsDocumentOlderThanCache.
/**
* When the server returns a full response body we will store it and return it regardless of what
* its Last-Modified date is. This behavior was different prior to OkHttp 3.5 when we would prefer
* the response with the later Last-Modified date.
*
* https://github.com/square/okhttp/issues/2886
*/
@Test
public void serverReturnsDocumentOlderThanCache() throws Exception {
server.enqueue(new MockResponse().setBody("A").addHeader("Last-Modified: " + formatDate(-2, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(-1, TimeUnit.HOURS)));
server.enqueue(new MockResponse().setBody("B").addHeader("Last-Modified: " + formatDate(-4, TimeUnit.HOURS)));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
HttpUrl url = server.url("/");
assertEquals("A", get(url).body().string());
assertEquals("B", get(url).body().string());
assertEquals("B", get(url).body().string());
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class CacheTest method clientSuppliedIfNoneMatchSinceWithCachedResult.
@Test
public void clientSuppliedIfNoneMatchSinceWithCachedResult() throws Exception {
String lastModifiedDate = formatDate(-3, TimeUnit.MINUTES);
MockResponse response = new MockResponse().addHeader("Last-Modified: " + lastModifiedDate).addHeader("Date: " + formatDate(-2, TimeUnit.MINUTES)).addHeader("Cache-Control: max-age=0");
RecordedRequest request = assertClientSuppliedCondition(response, "If-None-Match", "v1");
assertEquals("v1", request.getHeader("If-None-Match"));
assertNull(request.getHeader("If-Modified-Since"));
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class CacheTest method conditionalMissUpdatesCache.
/** Test https://github.com/square/okhttp/issues/1712. */
@Test
public void conditionalMissUpdatesCache() throws Exception {
server.enqueue(new MockResponse().addHeader("ETag: v1").setBody("A"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
server.enqueue(new MockResponse().addHeader("ETag: v2").setBody("B"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
HttpUrl url = server.url("/");
assertEquals("A", get(url).body().string());
assertEquals("A", get(url).body().string());
assertEquals("B", get(url).body().string());
assertEquals("B", get(url).body().string());
assertEquals(null, server.takeRequest().getHeader("If-None-Match"));
assertEquals("v1", server.takeRequest().getHeader("If-None-Match"));
assertEquals("v1", server.takeRequest().getHeader("If-None-Match"));
assertEquals("v2", server.takeRequest().getHeader("If-None-Match"));
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class CacheTest method defaultExpirationDateFullyCachedForMoreThan24Hours.
@Test
public void defaultExpirationDateFullyCachedForMoreThan24Hours() throws Exception {
// last modified: 105 days ago
// served: 5 days ago
// default lifetime: (105 - 5) / 10 = 10 days
// expires: 10 days from served date = 5 days from now
server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-105, TimeUnit.DAYS)).addHeader("Date: " + formatDate(-5, TimeUnit.DAYS)).setBody("A"));
assertEquals("A", get(server.url("/")).body().string());
Response response = get(server.url("/"));
assertEquals("A", response.body().string());
assertEquals("113 HttpURLConnection \"Heuristic expiration\"", response.header("Warning"));
}
use of okhttp3.mockwebserver.MockResponse in project okhttp by square.
the class CacheTest method cachedRedirect.
/** https://github.com/square/okhttp/issues/2198 */
@Test
public void cachedRedirect() throws IOException {
server.enqueue(new MockResponse().setResponseCode(301).addHeader("Cache-Control: max-age=60").addHeader("Location: /bar"));
server.enqueue(new MockResponse().setBody("ABC"));
server.enqueue(new MockResponse().setBody("ABC"));
Request request1 = new Request.Builder().url(server.url("/")).build();
Response response1 = client.newCall(request1).execute();
assertEquals("ABC", response1.body().string());
Request request2 = new Request.Builder().url(server.url("/")).build();
Response response2 = client.newCall(request2).execute();
assertEquals("ABC", response2.body().string());
}
Aggregations