Search in sources :

Example 86 with MockResponse

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

the class ResponseCacheTest method requestMaxStale.

@Test
public void requestMaxStale() throws IOException {
    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())));
    URLConnection connection = openConnection(server.url("/").url());
    connection.addRequestProperty("Cache-Control", "max-stale=180");
    assertEquals("A", readAscii(connection));
    assertEquals("110 HttpURLConnection \"Response is stale\"", connection.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 87 with MockResponse

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

the class ResponseCacheTest method emptyResponseHeaderNameFromCacheIsLenient.

@Test
public void emptyResponseHeaderNameFromCacheIsLenient() throws Exception {
    Headers.Builder headers = new Headers.Builder().add("Cache-Control: max-age=120");
    Internal.instance.addLenient(headers, ": A");
    server.enqueue(new MockResponse().setHeaders(headers.build()).setBody("body"));
    HttpURLConnection connection = openConnection(server.url("/").url());
    assertEquals("A", connection.getHeaderField(""));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) Headers(okhttp3.Headers) Test(org.junit.Test)

Example 88 with MockResponse

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

the class ResponseCacheTest method clientSuppliedConditionWithoutCachedResult.

@Test
public void clientSuppliedConditionWithoutCachedResult() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    HttpURLConnection connection = openConnection(server.url("/").url());
    String clientIfModifiedSince = formatDate(-24, TimeUnit.HOURS);
    connection.addRequestProperty("If-Modified-Since", clientIfModifiedSince);
    assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, connection.getResponseCode());
    assertEquals("", readAscii(connection));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) Test(org.junit.Test)

Example 89 with MockResponse

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

the class ResponseCacheTest method varyMultipleFieldsWithNoMatch.

@Test
public void varyMultipleFieldsWithNoMatch() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language, Accept-Charset").addHeader("Vary: Accept-Encoding").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));
    URL url = server.url("/").url();
    HttpURLConnection frenchConnection = openConnection(url);
    frenchConnection.setRequestProperty("Accept-Language", "fr-CA");
    frenchConnection.setRequestProperty("Accept-Charset", "UTF-8");
    frenchConnection.setRequestProperty("Accept-Encoding", "identity");
    assertEquals("A", readAscii(frenchConnection));
    HttpURLConnection englishConnection = openConnection(url);
    englishConnection.setRequestProperty("Accept-Language", "en-CA");
    englishConnection.setRequestProperty("Accept-Charset", "UTF-8");
    englishConnection.setRequestProperty("Accept-Encoding", "identity");
    assertEquals("B", readAscii(englishConnection));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) Test(org.junit.Test)

Example 90 with MockResponse

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

the class ResponseCacheTest method notModifiedSpecifiesEncoding.

@Test
public void notModifiedSpecifiesEncoding() throws Exception {
    server.enqueue(new MockResponse().setBody(gzip("ABCABCABC")).addHeader("Content-Encoding: gzip").addHeader("Last-Modified: " + formatDate(-2, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(-1, TimeUnit.HOURS)));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED).addHeader("Content-Encoding: gzip"));
    server.enqueue(new MockResponse().setBody("DEFDEFDEF"));
    assertEquals("ABCABCABC", readAscii(openConnection(server.url("/").url())));
    assertEquals("ABCABCABC", readAscii(openConnection(server.url("/").url())));
    assertEquals("DEFDEFDEF", readAscii(openConnection(server.url("/").url())));
}
Also used : 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