Search in sources :

Example 6 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class CacheTest method redirectToCachedResult.

@Test
public void redirectToCachedResult() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("ABC"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_PERM).addHeader("Location: /foo"));
    server.enqueue(new MockResponse().setBody("DEF"));
    Request request1 = new Request.Builder().url(server.url("/foo")).build();
    Response response1 = client.newCall(request1).execute();
    assertThat(response1.body().string()).isEqualTo("ABC");
    RecordedRequest recordedRequest1 = server.takeRequest();
    assertThat(recordedRequest1.getRequestLine()).isEqualTo("GET /foo HTTP/1.1");
    assertThat(recordedRequest1.getSequenceNumber()).isEqualTo(0);
    Request request2 = new Request.Builder().url(server.url("/bar")).build();
    Response response2 = client.newCall(request2).execute();
    assertThat(response2.body().string()).isEqualTo("ABC");
    RecordedRequest recordedRequest2 = server.takeRequest();
    assertThat(recordedRequest2.getRequestLine()).isEqualTo("GET /bar HTTP/1.1");
    assertThat(recordedRequest2.getSequenceNumber()).isEqualTo(1);
    // an unrelated request should reuse the pooled connection
    Request request3 = new Request.Builder().url(server.url("/baz")).build();
    Response response3 = client.newCall(request3).execute();
    assertThat(response3.body().string()).isEqualTo("DEF");
    RecordedRequest recordedRequest3 = server.takeRequest();
    assertThat(recordedRequest3.getRequestLine()).isEqualTo("GET /baz HTTP/1.1");
    assertThat(recordedRequest3.getSequenceNumber()).isEqualTo(2);
}
Also used : MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest) Test(org.junit.jupiter.api.Test)

Example 7 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class CacheTest method pragmaNoCacheAndExpirationDateInTheFuture.

@Test
public void pragmaNoCacheAndExpirationDateInTheFuture() throws Exception {
    String lastModifiedDate = formatDate(-2, TimeUnit.HOURS);
    RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("Last-Modified: " + lastModifiedDate).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).addHeader("Pragma: no-cache"));
    assertThat(conditionalRequest.getHeader("If-Modified-Since")).isEqualTo(lastModifiedDate);
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 8 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class CacheTest method etagAndExpirationDateInThePast.

/**
 * If both If-Modified-Since and If-None-Match conditions apply, send only If-None-Match.
 */
@Test
public void etagAndExpirationDateInThePast() throws Exception {
    String lastModifiedDate = formatDate(-2, TimeUnit.HOURS);
    RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("ETag: v1").addHeader("Last-Modified: " + lastModifiedDate).addHeader("Expires: " + formatDate(-1, TimeUnit.HOURS)));
    assertThat(conditionalRequest.getHeader("If-None-Match")).isEqualTo("v1");
    assertThat(conditionalRequest.getHeader("If-Modified-Since")).isNull();
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 9 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class CacheTest method etag.

@Test
public void etag() throws Exception {
    RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("ETag: v1"));
    assertThat(conditionalRequest.getHeader("If-None-Match")).isEqualTo("v1");
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 10 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class DnsOverHttpsTest method usesCache.

// TODO GET preferred order - with tests to confirm this
// 1. successful fresh cached GET response
// 2. unsuccessful (404, 500) fresh cached GET response
// 3. successful network response
// 4. successful stale cached GET response
// 5. unsuccessful response
// TODO how closely to follow POST rules on caching?
@Test
public void usesCache() throws Exception {
    Cache cache = new Cache(Path.get("cache"), 100 * 1024, cacheFs);
    OkHttpClient cachedClient = bootstrapClient.newBuilder().cache(cache).build();
    DnsOverHttps cachedDns = buildLocalhost(cachedClient, false);
    server.enqueue(dnsResponse("0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c00050001" + "00000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c04200010" + "0010000003b00049df00112").setHeader("cache-control", "private, max-age=298"));
    List<InetAddress> result = cachedDns.lookup("google.com");
    assertThat(result).containsExactly(address("157.240.1.18"));
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest.getMethod()).isEqualTo("GET");
    assertThat(recordedRequest.getPath()).isEqualTo("/lookup?ct&dns=AAABAAABAAAAAAAABmdvb2dsZQNjb20AAAEAAQ");
    result = cachedDns.lookup("google.com");
    assertThat(result).isEqualTo(singletonList(address("157.240.1.18")));
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) OkHttpClient(okhttp3.OkHttpClient) InetAddress(java.net.InetAddress) Cache(okhttp3.Cache) Test(org.junit.jupiter.api.Test)

Aggregations

RecordedRequest (mockwebserver3.RecordedRequest)30 MockResponse (mockwebserver3.MockResponse)26 Test (org.junit.jupiter.api.Test)25 CookieManager (java.net.CookieManager)5 InetAddress (java.net.InetAddress)5 URI (java.net.URI)3 Arrays.asList (java.util.Arrays.asList)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 Map (java.util.Map)3 MockWebServer (mockwebserver3.MockWebServer)3 HttpCookie (java.net.HttpCookie)2 Dispatcher (mockwebserver3.Dispatcher)2 Cache (okhttp3.Cache)2 OkHttpClient (okhttp3.OkHttpClient)2 WebSocket (okhttp3.WebSocket)2 Buffer (okio.Buffer)2 File (java.io.File)1 IOException (java.io.IOException)1 CookieHandler (java.net.CookieHandler)1