Search in sources :

Example 26 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method putInvalidatesWithNoContentResponse.

@Test
public void putInvalidatesWithNoContentResponse() 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().clearHeaders().setResponseCode(HttpURLConnection.HTTP_NO_CONTENT));
    server.enqueue(new MockResponse().setBody("C"));
    HttpUrl url = server.url("/");
    assertThat(get(url).body().string()).isEqualTo("A");
    Request request = new Request.Builder().url(url).put(RequestBody.create("foo", MediaType.get("text/plain"))).build();
    Response invalidate = client.newCall(request).execute();
    assertThat(invalidate.body().string()).isEqualTo("");
    assertThat(get(url).body().string()).isEqualTo("C");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest) Test(org.junit.jupiter.api.Test)

Example 27 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest 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"));
    HttpUrl url = server.url("/");
    assertThat(get(url).body().string()).isEqualTo("A");
    Request request = new Request.Builder().url(url).header("Cache-Control", "no-cache").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)

Example 28 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method iteratorRemoveFromCache.

@Test
public void iteratorRemoveFromCache() throws Exception {
    // Put a response in the cache.
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("a"));
    HttpUrl url = server.url("/a");
    assertThat(get(url).body().string()).isEqualTo("a");
    // Remove it with iteration.
    Iterator<String> i = cache.urls();
    assertThat(i.next()).isEqualTo(url.toString());
    i.remove();
    // Confirm that subsequent requests suffer a cache miss.
    server.enqueue(new MockResponse().setBody("b"));
    assertThat(get(url).body().string()).isEqualTo("b");
}
Also used : MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 29 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method defaultExpirationDateFullyCachedForMoreThan24Hours.

@Test
public void defaultExpirationDateFullyCachedForMoreThan24Hours() throws Exception {
    // last modified: 105 days ago
    // served:   5 days ago
    // default lifetime: (105 - 5) / 10 = 10 days
    // expires:  10 days from served date = 5 days from now
    server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-105, TimeUnit.DAYS)).addHeader("Date: " + formatDate(-5, TimeUnit.DAYS)).setBody("A"));
    assertThat(get(server.url("/")).body().string()).isEqualTo("A");
    Response response = get(server.url("/"));
    assertThat(response.body().string()).isEqualTo("A");
    assertThat(response.header("Warning")).isEqualTo("113 HttpURLConnection \"Heuristic expiration\"");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 30 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method conditionalCacheHitIsNotDoublePooled.

@Test
public void conditionalCacheHitIsNotDoublePooled() throws Exception {
    clientTestRule.ensureAllConnectionsReleased();
    server.enqueue(new MockResponse().addHeader("ETag: v1").setBody("A"));
    server.enqueue(new MockResponse().clearHeaders().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    assertThat(get(server.url("/")).body().string()).isEqualTo("A");
    assertThat(get(server.url("/")).body().string()).isEqualTo("A");
    assertThat(client.connectionPool().idleConnectionCount()).isEqualTo(1);
}
Also used : MockResponse(mockwebserver3.MockResponse) 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