Search in sources :

Example 46 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest 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"));
    HttpUrl url = server.url("/");
    Request request1 = new Request.Builder().url(url).addHeader("Accept-Language", "fr-CA, fr-FR").addHeader("Accept-Language", "en-US").build();
    Response response1 = client.newCall(request1).execute();
    assertThat(response1.body().string()).isEqualTo("A");
    Request request2 = new Request.Builder().url(url).addHeader("Accept-Language", "fr-CA, fr-FR").addHeader("Accept-Language", "en-US").build();
    Response response2 = client.newCall(request2).execute();
    assertThat(response2.body().string()).isEqualTo("A");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest) Test(org.junit.jupiter.api.Test)

Example 47 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method serverReturnsDocumentOlderThanCache.

/**
 * When the server returns a full response body we will store it and return it regardless of what
 * its Last-Modified date is. This behavior was different prior to OkHttp 3.5 when we would prefer
 * the response with the later Last-Modified date.
 *
 * https://github.com/square/okhttp/issues/2886
 */
@Test
public void serverReturnsDocumentOlderThanCache() throws Exception {
    server.enqueue(new MockResponse().setBody("A").addHeader("Last-Modified: " + formatDate(-2, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(-1, TimeUnit.HOURS)));
    server.enqueue(new MockResponse().setBody("B").addHeader("Last-Modified: " + formatDate(-4, TimeUnit.HOURS)));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    HttpUrl url = server.url("/");
    assertThat(get(url).body().string()).isEqualTo("A");
    assertThat(get(url).body().string()).isEqualTo("B");
    assertThat(get(url).body().string()).isEqualTo("B");
}
Also used : MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 48 with MockResponse

use of mockwebserver3.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("/"));
    assertThat(response1.body().string()).isEqualTo("A");
    assertThat(response1.header("Allow")).isEqualTo("GET, HEAD");
    Response response2 = get(server.url("/"));
    assertThat(response2.body().string()).isEqualTo("A");
    assertThat(response2.header("Allow")).isEqualTo("GET, HEAD, PUT");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 49 with MockResponse

use of mockwebserver3.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"));
    assertThat(get(server.url("/")).body().string()).isEqualTo("A");
    assertThat(cache.requestCount()).isEqualTo(1);
    assertThat(cache.networkCount()).isEqualTo(1);
    assertThat(cache.hitCount()).isEqualTo(0);
    assertThat(get(server.url("/")).body().string()).isEqualTo("A");
    assertThat(get(server.url("/")).body().string()).isEqualTo("A");
    assertThat(cache.requestCount()).isEqualTo(3);
    assertThat(cache.networkCount()).isEqualTo(1);
    assertThat(cache.hitCount()).isEqualTo(2);
}
Also used : MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 50 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest 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"));
    assertThat(get(server.url("/")).body().string()).isEqualTo("A");
    Request request = new Request.Builder().url(server.url("/")).header("Cache-Control", "max-stale=180").build();
    Response response = client.newCall(request).execute();
    assertThat(response.body().string()).isEqualTo("A");
    assertThat(response.header("Warning")).isEqualTo("110 HttpURLConnection \"Response is stale\"");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest) Test(org.junit.jupiter.api.Test)

Aggregations

MockResponse (mockwebserver3.MockResponse)283 Test (org.junit.jupiter.api.Test)261 RecordedRequest (mockwebserver3.RecordedRequest)79 IOException (java.io.IOException)41 Response (okhttp3.Response)39 BufferedSink (okio.BufferedSink)28 WebSocket (okhttp3.WebSocket)27 AtomicReference (java.util.concurrent.atomic.AtomicReference)22 MockWebServer (mockwebserver3.MockWebServer)22 Request (okhttp3.Request)21 Buffer (okio.Buffer)21 InetAddress (java.net.InetAddress)20 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)17 Assertions.fail (org.junit.jupiter.api.Assertions.fail)17 BeforeEach (org.junit.jupiter.api.BeforeEach)17 Tag (org.junit.jupiter.api.Tag)17 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)17 SocketTimeoutException (java.net.SocketTimeoutException)15 Duration (java.time.Duration)15 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)15