use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method putInvalidatesWithNoContentResponse.
@Test
public void putInvalidatesWithNoContentResponse() throws Exception {
// 1. Seed the cache.
// 2. Invalidate it.
// 3. Expect a cache miss.
server.enqueue(new MockResponse().setBody("A").addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)));
server.enqueue(new MockResponse().clearHeaders().setResponseCode(HttpURLConnection.HTTP_NO_CONTENT));
server.enqueue(new MockResponse().setBody("C"));
HttpUrl url = server.url("/");
assertThat(get(url).body().string()).isEqualTo("A");
Request request = new Request.Builder().url(url).put(RequestBody.create("foo", MediaType.get("text/plain"))).build();
Response invalidate = client.newCall(request).execute();
assertThat(invalidate.body().string()).isEqualTo("");
assertThat(get(url).body().string()).isEqualTo("C");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method requestCacheControlNoCache.
@Test
public void requestCacheControlNoCache() throws Exception {
server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-120, TimeUnit.SECONDS)).addHeader("Date: " + formatDate(0, TimeUnit.SECONDS)).addHeader("Cache-Control: max-age=60").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
HttpUrl url = server.url("/");
assertThat(get(url).body().string()).isEqualTo("A");
Request request = new Request.Builder().url(url).header("Cache-Control", "no-cache").build();
Response response = client.newCall(request).execute();
assertThat(response.body().string()).isEqualTo("B");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method iteratorRemoveFromCache.
@Test
public void iteratorRemoveFromCache() throws Exception {
// Put a response in the cache.
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("a"));
HttpUrl url = server.url("/a");
assertThat(get(url).body().string()).isEqualTo("a");
// Remove it with iteration.
Iterator<String> i = cache.urls();
assertThat(i.next()).isEqualTo(url.toString());
i.remove();
// Confirm that subsequent requests suffer a cache miss.
server.enqueue(new MockResponse().setBody("b"));
assertThat(get(url).body().string()).isEqualTo("b");
}
use of mockwebserver3.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"));
assertThat(get(server.url("/")).body().string()).isEqualTo("A");
Response response = get(server.url("/"));
assertThat(response.body().string()).isEqualTo("A");
assertThat(response.header("Warning")).isEqualTo("113 HttpURLConnection \"Heuristic expiration\"");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method conditionalCacheHitIsNotDoublePooled.
@Test
public void conditionalCacheHitIsNotDoublePooled() throws Exception {
clientTestRule.ensureAllConnectionsReleased();
server.enqueue(new MockResponse().addHeader("ETag: v1").setBody("A"));
server.enqueue(new MockResponse().clearHeaders().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
assertThat(get(server.url("/")).body().string()).isEqualTo("A");
assertThat(get(server.url("/")).body().string()).isEqualTo("A");
assertThat(client.connectionPool().idleConnectionCount()).isEqualTo(1);
}
Aggregations