Search in sources :

Example 91 with MockResponse

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

the class ResponseCacheTest method varyMultipleFieldValuesWithMatch.

@Test
public void varyMultipleFieldValuesWithMatch() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));
    URL url = server.url("/").url();
    HttpURLConnection multiConnection1 = openConnection(url);
    multiConnection1.setRequestProperty("Accept-Language", "fr-CA, fr-FR");
    multiConnection1.addRequestProperty("Accept-Language", "en-US");
    assertEquals("A", readAscii(multiConnection1));
    HttpURLConnection multiConnection2 = openConnection(url);
    multiConnection2.setRequestProperty("Accept-Language", "fr-CA, fr-FR");
    multiConnection2.addRequestProperty("Accept-Language", "en-US");
    assertEquals("A", readAscii(multiConnection2));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) Test(org.junit.Test)

Example 92 with MockResponse

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

the class CacheTest method assertCached.

private void assertCached(boolean shouldPut, int responseCode) throws Exception {
    int expectedResponseCode = responseCode;
    server = new MockWebServer();
    MockResponse mockResponse = new MockResponse().addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).setResponseCode(responseCode).setBody("ABCDE").addHeader("WWW-Authenticate: challenge");
    if (responseCode == HttpURLConnection.HTTP_PROXY_AUTH) {
        mockResponse.addHeader("Proxy-Authenticate: Basic realm=\"protected area\"");
    } else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
        mockResponse.addHeader("WWW-Authenticate: Basic realm=\"protected area\"");
    } else if (responseCode == HttpURLConnection.HTTP_NO_CONTENT || responseCode == HttpURLConnection.HTTP_RESET) {
        // We forbid bodies for 204 and 205.
        mockResponse.setBody("");
    }
    server.enqueue(mockResponse);
    if (responseCode == HttpURLConnection.HTTP_CLIENT_TIMEOUT) {
        // 408's are a bit of an outlier because we may repeat the request if we encounter this
        // response code. In this scenario, there are 2 responses: the initial 408 and then the 200
        // because of the retry. We just want to ensure the initial 408 isn't cached.
        expectedResponseCode = 200;
        server.enqueue(new MockResponse().setHeader("Cache-Control", "no-store").setBody("FGHIJ"));
    }
    server.start();
    Request request = new Request.Builder().url(server.url("/")).build();
    Response response = client.newCall(request).execute();
    assertEquals(expectedResponseCode, response.code());
    // Exhaust the content stream.
    response.body().string();
    Response cached = cache.get(request);
    if (shouldPut) {
        assertNotNull(Integer.toString(responseCode), cached);
        cached.body().close();
    } else {
        assertNull(Integer.toString(responseCode), cached);
    }
    // tearDown() isn't sufficient; this test starts multiple servers
    server.shutdown();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) RecordedRequest(okhttp3.mockwebserver.RecordedRequest)

Example 93 with MockResponse

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

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

the class CacheTest method statisticsFullCacheHit.

@Test
public void statisticsFullCacheHit() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
    assertEquals("A", get(server.url("/")).body().string());
    assertEquals(1, cache.requestCount());
    assertEquals(1, cache.networkCount());
    assertEquals(0, cache.hitCount());
    assertEquals("A", get(server.url("/")).body().string());
    assertEquals("A", get(server.url("/")).body().string());
    assertEquals(3, cache.requestCount());
    assertEquals(1, cache.networkCount());
    assertEquals(2, cache.hitCount());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 95 with MockResponse

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

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