Search in sources :

Example 66 with MockResponse

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

the class ResponseCacheTest method requestOnlyIfCachedWithConditionalResponseCached.

@Test
public void requestOnlyIfCachedWithConditionalResponseCached() throws IOException {
    server.enqueue(new MockResponse().setBody("A").addHeader("Cache-Control: max-age=30").addHeader("Date: " + formatDate(-1, TimeUnit.MINUTES)));
    assertEquals("A", readAscii(openConnection(server.url("/").url())));
    HttpURLConnection connection = openConnection(server.url("/").url());
    connection.addRequestProperty("Cache-Control", "only-if-cached");
    assertGatewayTimeout(connection);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) Test(org.junit.Test)

Example 67 with MockResponse

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

the class ResponseCacheTest method requestMaxStaleDirectiveWithNoValue.

@Test
public void requestMaxStaleDirectiveWithNoValue() throws IOException {
    // Add a stale response to the cache.
    server.enqueue(new MockResponse().setBody("A").addHeader("Cache-Control: max-age=120").addHeader("Date: " + formatDate(-4, TimeUnit.MINUTES)));
    server.enqueue(new MockResponse().setBody("B"));
    assertEquals("A", readAscii(openConnection(server.url("/").url())));
    // With max-stale, we'll return that stale response.
    URLConnection maxStaleConnection = openConnection(server.url("/").url());
    maxStaleConnection.setRequestProperty("Cache-Control", "max-stale");
    assertEquals("A", readAscii(maxStaleConnection));
    assertEquals("110 HttpURLConnection \"Response is stale\"", maxStaleConnection.getHeaderField("Warning"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 68 with MockResponse

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

the class CacheTest method assertConditionallyCached.

/** @return the request with the conditional get headers. */
private RecordedRequest assertConditionallyCached(MockResponse response) throws Exception {
    // scenario 1: condition succeeds
    server.enqueue(response.setBody("A").setStatus("HTTP/1.1 200 A-OK"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    // scenario 2: condition fails
    server.enqueue(response.setBody("B").setStatus("HTTP/1.1 200 B-OK"));
    server.enqueue(new MockResponse().setStatus("HTTP/1.1 200 C-OK").setBody("C"));
    HttpUrl valid = server.url("/valid");
    Response response1 = get(valid);
    assertEquals("A", response1.body().string());
    assertEquals(HttpURLConnection.HTTP_OK, response1.code());
    assertEquals("A-OK", response1.message());
    Response response2 = get(valid);
    assertEquals("A", response2.body().string());
    assertEquals(HttpURLConnection.HTTP_OK, response2.code());
    assertEquals("A-OK", response2.message());
    HttpUrl invalid = server.url("/invalid");
    Response response3 = get(invalid);
    assertEquals("B", response3.body().string());
    assertEquals(HttpURLConnection.HTTP_OK, response3.code());
    assertEquals("B-OK", response3.message());
    Response response4 = get(invalid);
    assertEquals("C", response4.body().string());
    assertEquals(HttpURLConnection.HTTP_OK, response4.code());
    assertEquals("C-OK", response4.message());
    // regular get
    server.takeRequest();
    // conditional get
    return server.takeRequest();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse)

Example 69 with MockResponse

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

the class CacheTest method clientSideNoStore.

@Test
public void clientSideNoStore() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("B"));
    Request request1 = new Request.Builder().url(server.url("/")).cacheControl(new CacheControl.Builder().noStore().build()).build();
    Response response1 = client.newCall(request1).execute();
    assertEquals("A", response1.body().string());
    Request request2 = new Request.Builder().url(server.url("/")).build();
    Response response2 = client.newCall(request2).execute();
    assertEquals("B", response2.body().string());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 70 with MockResponse

use of okhttp3.mockwebserver.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("/"));
    assertEquals("A", response1.body().string());
    assertEquals("GET, HEAD", response1.header("Allow"));
    Response response2 = get(server.url("/"));
    assertEquals("A", response2.body().string());
    assertEquals("GET, HEAD, PUT", response2.header("Allow"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) 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