Search in sources :

Example 56 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method etagConditionCanBeNonAscii.

@Test
public void etagConditionCanBeNonAscii() throws Exception {
    server.enqueue(new MockResponse().addHeaderLenient("Etag", "α").addHeader("Cache-Control: max-age=0").setBody("abcd"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    Response response1 = get(server.url("/"));
    assertThat(response1.body().string()).isEqualTo("abcd");
    Response response2 = get(server.url("/"));
    assertThat(response2.body().string()).isEqualTo("abcd");
    assertThat(server.takeRequest().getHeader("If-None-Match")).isNull();
    assertThat(server.takeRequest().getHeader("If-None-Match")).isEqualTo("α");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 57 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method varyAndHttps.

@Test
public void varyAndHttps() throws Exception {
    server.useHttps(handshakeCertificates.sslSocketFactory(), false);
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));
    client = client.newBuilder().sslSocketFactory(handshakeCertificates.sslSocketFactory(), handshakeCertificates.trustManager()).hostnameVerifier(NULL_HOSTNAME_VERIFIER).build();
    HttpUrl url = server.url("/");
    Request request1 = new Request.Builder().url(url).header("Accept-Language", "en-US").build();
    Response response1 = client.newCall(request1).execute();
    assertThat(response1.body().string()).isEqualTo("A");
    Request request2 = new Request.Builder().url(url).header("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 58 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method defaultExpirationDateFullyCachedForLessThan24Hours.

@Test
public void defaultExpirationDateFullyCachedForLessThan24Hours() throws Exception {
    // last modified: 105 seconds ago
    // served:   5 seconds ago
    // default lifetime: (105 - 5) / 10 = 10 seconds
    // expires:  10 seconds from served date = 5 seconds from now
    server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-105, TimeUnit.SECONDS)).addHeader("Date: " + formatDate(-5, TimeUnit.SECONDS)).setBody("A"));
    HttpUrl url = server.url("/");
    Response response1 = get(url);
    assertThat(response1.body().string()).isEqualTo("A");
    Response response2 = get(url);
    assertThat(response2.body().string()).isEqualTo("A");
    assertThat(response2.header("Warning")).isNull();
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 59 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method authorizationRequestFullyCached.

@Test
public void authorizationRequestFullyCached() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));
    HttpUrl url = server.url("/");
    Request request = new Request.Builder().url(url).header("Authorization", "password").build();
    Response response = client.newCall(request).execute();
    assertThat(response.body().string()).isEqualTo("A");
    assertThat(get(url).body().string()).isEqualTo("A");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest) Test(org.junit.jupiter.api.Test)

Example 60 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method requestMaxStaleNotHonoredWithMustRevalidate.

@Test
public void requestMaxStaleNotHonoredWithMustRevalidate() throws IOException {
    server.enqueue(new MockResponse().setBody("A").addHeader("Cache-Control: max-age=120, must-revalidate").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("B");
}
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