use of okhttp3.HttpUrl 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.HttpUrl 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.HttpUrl in project okhttp by square.
the class CacheTest method testGoldenCacheHttpsResponseOkHttp30.
/** The TLS version is present in OkHttp 3.0 and beyond. */
@Test
public void testGoldenCacheHttpsResponseOkHttp30() throws Exception {
HttpUrl url = server.url("/");
String urlKey = Cache.key(url);
String prefix = Platform.get().getPrefix();
String entryMetadata = "" + "" + url + "\n" + "GET\n" + "0\n" + "HTTP/1.1 200 OK\n" + "4\n" + "Content-Length: 3\n" + prefix + "-Received-Millis: " + System.currentTimeMillis() + "\n" + prefix + "-Sent-Millis: " + System.currentTimeMillis() + "\n" + "Cache-Control: max-age=60\n" + "\n" + "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256\n" + "1\n" + "MIIBnDCCAQWgAwIBAgIBATANBgkqhkiG9w0BAQsFADAUMRIwEAYDVQQDEwlsb2NhbGhvc3QwHhcNMTUxMjIyMDEx" + "MTQwWhcNMTUxMjIzMDExMTQwWjAUMRIwEAYDVQQDEwlsb2NhbGhvc3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ" + "AoGBAJTn2Dh8xYmegvpOSmsKb2Os6Cxf1L4fYbnHr/turInUD5r1P7ZAuxurY880q3GT5bUDoirS3IfucddrT1Ac" + "AmUzEmk/FDjggiP8DlxFkY/XwXBlhRDVIp/mRuASPMGInckc0ZaixOkRFyrxADj+r1eaSmXCIvV5yTY6IaIokLj1" + "AgMBAAEwDQYJKoZIhvcNAQELBQADgYEAFblnedqtfRqI9j2WDyPPoG0NTZf9xwjeUu+ju+Ktty8u9k7Lgrrd/DH2" + "mQEtBD1Ctvp91MJfAClNg3faZzwClUyu5pd0QXRZEUwSwZQNen2QWDHRlVsItclBJ4t+AJLqTbwofWi4m4K8REOl" + "593hD55E4+lY22JZiVQyjsQhe6I=\n" + "0\n" + "TLSv1.2\n";
String entryBody = "abc";
String journalBody = "" + "libcore.io.DiskLruCache\n" + "1\n" + "201105\n" + "2\n" + "\n" + "DIRTY " + urlKey + "\n" + "CLEAN " + urlKey + " " + entryMetadata.length() + " " + entryBody.length() + "\n";
writeFile(cache.directory(), urlKey + ".0", entryMetadata);
writeFile(cache.directory(), urlKey + ".1", entryBody);
writeFile(cache.directory(), "journal", journalBody);
cache.close();
cache = new Cache(cache.directory(), Integer.MAX_VALUE, fileSystem);
client = client.newBuilder().cache(cache).build();
Response response = get(url);
assertEquals(entryBody, response.body().string());
assertEquals("3", response.header("Content-Length"));
}
use of okhttp3.HttpUrl 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("/");
assertEquals("A", get(url).body().string());
Request request = new Request.Builder().url(url).header("Cache-Control", "no-cache").build();
Response response = client.newCall(request).execute();
assertEquals("B", response.body().string());
}
use of okhttp3.HttpUrl in project okhttp by square.
the class CacheTest 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"));
HttpUrl url = server.url("/");
Request request = new Request.Builder().url(url).header("Accept-Language", "fr-CA").build();
Response response1 = client.newCall(request).execute();
assertEquals("A", response1.body().string());
Request request1 = new Request.Builder().url(url).header("Accept-Language", "fr-CA").build();
Response response2 = client.newCall(request1).execute();
assertEquals("A", response2.body().string());
}
Aggregations