Search in sources :

Example 11 with HttpUrl

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());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 12 with HttpUrl

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"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 13 with HttpUrl

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"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ResponseCache(java.net.ResponseCache) Test(org.junit.Test)

Example 14 with HttpUrl

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());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 15 with HttpUrl

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());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Aggregations

HttpUrl (okhttp3.HttpUrl)64 Test (org.junit.Test)51 MockResponse (okhttp3.mockwebserver.MockResponse)49 Request (okhttp3.Request)26 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)25 IOException (java.io.IOException)19 Response (okhttp3.Response)15 RequestBody (okhttp3.RequestBody)12 MockWebServer (okhttp3.mockwebserver.MockWebServer)10 Moshi (com.squareup.moshi.Moshi)6 CookieManager (java.net.CookieManager)6 Call (retrofit2.Call)6 Callback (retrofit2.Callback)6 Response (retrofit2.Response)6 LoginFailedException (com.pokegoapi.exceptions.request.LoginFailedException)5 HttpCookie (java.net.HttpCookie)5 QueryRequest (zipkin.storage.QueryRequest)5 ResponseCache (java.net.ResponseCache)4 Map (java.util.Map)4 Call (okhttp3.Call)4