use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method varyMultipleFieldValuesWithMatch.
@Test
public void varyMultipleFieldValuesWithMatch() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language").setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
HttpUrl url = server.url("/");
Request request1 = new Request.Builder().url(url).addHeader("Accept-Language", "fr-CA, fr-FR").addHeader("Accept-Language", "en-US").build();
Response response1 = client.newCall(request1).execute();
assertThat(response1.body().string()).isEqualTo("A");
Request request2 = new Request.Builder().url(url).addHeader("Accept-Language", "fr-CA, fr-FR").addHeader("Accept-Language", "en-US").build();
Response response2 = client.newCall(request2).execute();
assertThat(response2.body().string()).isEqualTo("A");
}
use of mockwebserver3.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("/");
assertThat(get(url).body().string()).isEqualTo("A");
assertThat(get(url).body().string()).isEqualTo("B");
assertThat(get(url).body().string()).isEqualTo("B");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method getHeadersReturnsNetworkEndToEndHeaders.
@Test
public void getHeadersReturnsNetworkEndToEndHeaders() throws Exception {
server.enqueue(new MockResponse().addHeader("Allow: GET, HEAD").addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").setBody("A"));
server.enqueue(new MockResponse().addHeader("Allow: GET, HEAD, PUT").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
Response response1 = get(server.url("/"));
assertThat(response1.body().string()).isEqualTo("A");
assertThat(response1.header("Allow")).isEqualTo("GET, HEAD");
Response response2 = get(server.url("/"));
assertThat(response2.body().string()).isEqualTo("A");
assertThat(response2.header("Allow")).isEqualTo("GET, HEAD, PUT");
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest method statisticsFullCacheHit.
@Test
public void statisticsFullCacheHit() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
assertThat(get(server.url("/")).body().string()).isEqualTo("A");
assertThat(cache.requestCount()).isEqualTo(1);
assertThat(cache.networkCount()).isEqualTo(1);
assertThat(cache.hitCount()).isEqualTo(0);
assertThat(get(server.url("/")).body().string()).isEqualTo("A");
assertThat(get(server.url("/")).body().string()).isEqualTo("A");
assertThat(cache.requestCount()).isEqualTo(3);
assertThat(cache.networkCount()).isEqualTo(1);
assertThat(cache.hitCount()).isEqualTo(2);
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CacheTest 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"));
assertThat(get(server.url("/")).body().string()).isEqualTo("A");
Request request = new Request.Builder().url(server.url("/")).header("Cache-Control", "max-stale=180").build();
Response response = client.newCall(request).execute();
assertThat(response.body().string()).isEqualTo("A");
assertThat(response.header("Warning")).isEqualTo("110 HttpURLConnection \"Response is stale\"");
}
Aggregations