Search in sources :

Example 36 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 37 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 38 with MockResponse

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

the class ResponseCacheTest method temporaryRedirectNotCachedWithoutCachingHeader.

private void temporaryRedirectNotCachedWithoutCachingHeader(int responseCode) throws Exception {
    server.enqueue(new MockResponse().setResponseCode(responseCode).addHeader("Location", "/a"));
    server.enqueue(new MockResponse().setBody("a"));
    server.enqueue(new MockResponse().setBody("b"));
    URL url = server.url("/").url();
    assertEquals("a", readAscii(openConnection(url)));
    assertEquals("b", readAscii(openConnection(url)));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) URL(java.net.URL)

Example 39 with MockResponse

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

the class ResponseCacheTest method otherStacks_cacheMissWithVaryAcceptEncoding.

// Other stacks (e.g. older versions of OkHttp bundled inside Android apps) can interact with the
// default ResponseCache. We can't keep the Vary case working because we can't get to the Vary
// request headers after connect(). Accept-Encoding has special behavior so we test it explicitly.
@Test
public void otherStacks_cacheMissWithVaryAcceptEncoding() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Encoding").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));
    // Set the cache as the shared cache.
    ResponseCache.setDefault(cache);
    // Use the platform's HTTP stack.
    URLConnection connection = server.url("/").url().openConnection();
    assertFalse(connection instanceof OkHttpURLConnection);
    assertEquals("A", readAscii(connection));
    URLConnection connection2 = server.url("/").url().openConnection();
    assertFalse(connection2 instanceof OkHttpURLConnection);
    assertEquals("B", readAscii(connection2));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 40 with MockResponse

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

the class ResponseCacheTest 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"));
    URL url = server.url("/").url();
    HttpURLConnection frenchConnection1 = openConnection(url);
    frenchConnection1.setRequestProperty("Accept-Language", "fr-CA");
    assertEquals("A", readAscii(frenchConnection1));
    HttpURLConnection frenchConnection2 = openConnection(url);
    frenchConnection2.setRequestProperty("Accept-Language", "fr-CA");
    assertEquals("A", readAscii(frenchConnection2));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) Test(org.junit.Test)

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