Search in sources :

Example 31 with MockResponse

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

the class ResponseCacheTest 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();
    URL url = server.url("/").url();
    HttpURLConnection connection = openConnection(url);
    assertEquals(expectedResponseCode, connection.getResponseCode());
    // Exhaust the content stream.
    readAscii(connection);
    CacheResponse cached = cache.get(url.toURI(), "GET", null);
    if (shouldPut) {
        assertNotNull(Integer.toString(responseCode), cached);
    } else {
        assertNull(Integer.toString(responseCode), cached);
    }
    // tearDown() isn't sufficient; this test starts multiple servers
    server.shutdown();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CacheResponse(java.net.CacheResponse) SecureCacheResponse(java.net.SecureCacheResponse) HttpURLConnection(java.net.HttpURLConnection) MockWebServer(okhttp3.mockwebserver.MockWebServer) URL(java.net.URL)

Example 32 with MockResponse

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

the class ResponseCacheTest method redirectToCachedResult.

@Test
public void redirectToCachedResult() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("ABC"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_PERM).addHeader("Location: /foo"));
    server.enqueue(new MockResponse().setBody("DEF"));
    assertEquals("ABC", readAscii(openConnection(server.url("/foo").url())));
    RecordedRequest request1 = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request1.getRequestLine());
    assertEquals(0, request1.getSequenceNumber());
    assertEquals("ABC", readAscii(openConnection(server.url("/bar").url())));
    RecordedRequest request2 = server.takeRequest();
    assertEquals("GET /bar HTTP/1.1", request2.getRequestLine());
    assertEquals(1, request2.getSequenceNumber());
    // an unrelated request should reuse the pooled connection
    assertEquals("DEF", readAscii(openConnection(server.url("/baz").url())));
    RecordedRequest request3 = server.takeRequest();
    assertEquals("GET /baz HTTP/1.1", request3.getRequestLine());
    assertEquals(2, request3.getSequenceNumber());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 33 with MockResponse

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

the class ResponseCacheTest method varyMatchesRemovedRequestHeaderField.

@Test
public void varyMatchesRemovedRequestHeaderField() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Foo").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));
    HttpURLConnection connection1 = openConnection(server.url("/").url());
    connection1.setRequestProperty("Foo", "bar");
    assertEquals("A", readAscii(connection1));
    assertEquals("B", readAscii(openConnection(server.url("/").url())));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) Test(org.junit.Test)

Example 34 with MockResponse

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

the class ResponseCacheTest 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"));
    URL url = server.url("/").url();
    assertEquals("A", readAscii(openConnection(url)));
    URLConnection connection = openConnection(url);
    connection.setRequestProperty("Cache-Control", "no-cache");
    assertEquals("B", readAscii(connection));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 35 with MockResponse

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

the class ResponseCacheTest method testMethodInvalidates.

private void testMethodInvalidates(String requestMethod) throws Exception {
    // 1. seed the cache
    // 2. invalidate it
    // 3. expect a cache miss
    server.enqueue(new MockResponse().setBody("A").addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)));
    server.enqueue(new MockResponse().setBody("B"));
    server.enqueue(new MockResponse().setBody("C"));
    URL url = server.url("/").url();
    assertEquals("A", readAscii(openConnection(url)));
    HttpURLConnection invalidateConnection = openConnection(url);
    invalidateConnection.setRequestMethod(requestMethod);
    assertEquals("B", readAscii(invalidateConnection));
    assertEquals("C", readAscii(openConnection(url)));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)1754 Test (org.junit.Test)1284 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)483 AtomicReference (java.util.concurrent.atomic.AtomicReference)258 Test (org.junit.jupiter.api.Test)216 MockWebServer (okhttp3.mockwebserver.MockWebServer)201 CountDownLatch (java.util.concurrent.CountDownLatch)196 IOException (java.io.IOException)158 HttpURLConnection (java.net.HttpURLConnection)157 ANError (com.androidnetworking.error.ANError)148 Response (okhttp3.Response)147 MockResponse (mockwebserver3.MockResponse)115 Buffer (okio.Buffer)104 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)89 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)84 List (java.util.List)84 URL (java.net.URL)78 URLConnection (java.net.URLConnection)76 Request (okhttp3.Request)70 ANResponse (com.androidnetworking.common.ANResponse)61