use of mockwebserver3.RecordedRequest in project okhttp by square.
the class CacheTest method redirectToCachedResult.
@Test
public void redirectToCachedResult() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("ABC"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_PERM).addHeader("Location: /foo"));
server.enqueue(new MockResponse().setBody("DEF"));
Request request1 = new Request.Builder().url(server.url("/foo")).build();
Response response1 = client.newCall(request1).execute();
assertThat(response1.body().string()).isEqualTo("ABC");
RecordedRequest recordedRequest1 = server.takeRequest();
assertThat(recordedRequest1.getRequestLine()).isEqualTo("GET /foo HTTP/1.1");
assertThat(recordedRequest1.getSequenceNumber()).isEqualTo(0);
Request request2 = new Request.Builder().url(server.url("/bar")).build();
Response response2 = client.newCall(request2).execute();
assertThat(response2.body().string()).isEqualTo("ABC");
RecordedRequest recordedRequest2 = server.takeRequest();
assertThat(recordedRequest2.getRequestLine()).isEqualTo("GET /bar HTTP/1.1");
assertThat(recordedRequest2.getSequenceNumber()).isEqualTo(1);
// an unrelated request should reuse the pooled connection
Request request3 = new Request.Builder().url(server.url("/baz")).build();
Response response3 = client.newCall(request3).execute();
assertThat(response3.body().string()).isEqualTo("DEF");
RecordedRequest recordedRequest3 = server.takeRequest();
assertThat(recordedRequest3.getRequestLine()).isEqualTo("GET /baz HTTP/1.1");
assertThat(recordedRequest3.getSequenceNumber()).isEqualTo(2);
}
use of mockwebserver3.RecordedRequest in project okhttp by square.
the class CacheTest method pragmaNoCacheAndExpirationDateInTheFuture.
@Test
public void pragmaNoCacheAndExpirationDateInTheFuture() throws Exception {
String lastModifiedDate = formatDate(-2, TimeUnit.HOURS);
RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("Last-Modified: " + lastModifiedDate).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).addHeader("Pragma: no-cache"));
assertThat(conditionalRequest.getHeader("If-Modified-Since")).isEqualTo(lastModifiedDate);
}
use of mockwebserver3.RecordedRequest in project okhttp by square.
the class CacheTest method etagAndExpirationDateInThePast.
/**
* If both If-Modified-Since and If-None-Match conditions apply, send only If-None-Match.
*/
@Test
public void etagAndExpirationDateInThePast() throws Exception {
String lastModifiedDate = formatDate(-2, TimeUnit.HOURS);
RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("ETag: v1").addHeader("Last-Modified: " + lastModifiedDate).addHeader("Expires: " + formatDate(-1, TimeUnit.HOURS)));
assertThat(conditionalRequest.getHeader("If-None-Match")).isEqualTo("v1");
assertThat(conditionalRequest.getHeader("If-Modified-Since")).isNull();
}
use of mockwebserver3.RecordedRequest in project okhttp by square.
the class CacheTest method etag.
@Test
public void etag() throws Exception {
RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("ETag: v1"));
assertThat(conditionalRequest.getHeader("If-None-Match")).isEqualTo("v1");
}
use of mockwebserver3.RecordedRequest in project okhttp by square.
the class DnsOverHttpsTest method usesCache.
// TODO GET preferred order - with tests to confirm this
// 1. successful fresh cached GET response
// 2. unsuccessful (404, 500) fresh cached GET response
// 3. successful network response
// 4. successful stale cached GET response
// 5. unsuccessful response
// TODO how closely to follow POST rules on caching?
@Test
public void usesCache() throws Exception {
Cache cache = new Cache(Path.get("cache"), 100 * 1024, cacheFs);
OkHttpClient cachedClient = bootstrapClient.newBuilder().cache(cache).build();
DnsOverHttps cachedDns = buildLocalhost(cachedClient, false);
server.enqueue(dnsResponse("0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c00050001" + "00000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c04200010" + "0010000003b00049df00112").setHeader("cache-control", "private, max-age=298"));
List<InetAddress> result = cachedDns.lookup("google.com");
assertThat(result).containsExactly(address("157.240.1.18"));
RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest.getMethod()).isEqualTo("GET");
assertThat(recordedRequest.getPath()).isEqualTo("/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ");
result = cachedDns.lookup("google.com");
assertThat(result).isEqualTo(singletonList(address("157.240.1.18")));
}
Aggregations