Search in sources :

Example 71 with MockResponse

use of okhttp3.mockwebserver.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("/");
    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 72 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project okhttp by square.

the class CacheTest method clientSuppliedIfNoneMatchSinceWithCachedResult.

@Test
public void clientSuppliedIfNoneMatchSinceWithCachedResult() throws Exception {
    String lastModifiedDate = formatDate(-3, TimeUnit.MINUTES);
    MockResponse response = new MockResponse().addHeader("Last-Modified: " + lastModifiedDate).addHeader("Date: " + formatDate(-2, TimeUnit.MINUTES)).addHeader("Cache-Control: max-age=0");
    RecordedRequest request = assertClientSuppliedCondition(response, "If-None-Match", "v1");
    assertEquals("v1", request.getHeader("If-None-Match"));
    assertNull(request.getHeader("If-Modified-Since"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 73 with MockResponse

use of okhttp3.mockwebserver.MockResponse 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 74 with MockResponse

use of okhttp3.mockwebserver.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"));
    assertEquals("A", get(server.url("/")).body().string());
    Response response = get(server.url("/"));
    assertEquals("A", response.body().string());
    assertEquals("113 HttpURLConnection \"Heuristic expiration\"", response.header("Warning"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 75 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project okhttp by square.

the class CacheTest method cachedRedirect.

/** https://github.com/square/okhttp/issues/2198 */
@Test
public void cachedRedirect() throws IOException {
    server.enqueue(new MockResponse().setResponseCode(301).addHeader("Cache-Control: max-age=60").addHeader("Location: /bar"));
    server.enqueue(new MockResponse().setBody("ABC"));
    server.enqueue(new MockResponse().setBody("ABC"));
    Request request1 = new Request.Builder().url(server.url("/")).build();
    Response response1 = client.newCall(request1).execute();
    assertEquals("ABC", response1.body().string());
    Request request2 = new Request.Builder().url(server.url("/")).build();
    Response response2 = client.newCall(request2).execute();
    assertEquals("ABC", response2.body().string());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)839 Test (org.junit.Test)749 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)287 HttpURLConnection (java.net.HttpURLConnection)156 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)85 IOException (java.io.IOException)76 URLConnection (java.net.URLConnection)76 URL (java.net.URL)74 Response (okhttp3.Response)61 Request (okhttp3.Request)52 AtomicReference (java.util.concurrent.atomic.AtomicReference)46 OkHttpURLConnection (okhttp3.internal.huc.OkHttpURLConnection)43 Call (okhttp3.Call)42 Buffer (okio.Buffer)42 MockWebServer (okhttp3.mockwebserver.MockWebServer)32 CompositeException (io.reactivex.exceptions.CompositeException)28 InputStream (java.io.InputStream)26 CountDownLatch (java.util.concurrent.CountDownLatch)26 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)25 OutputStream (java.io.OutputStream)19