Search in sources :

Example 1 with RecordedRequest

use of mockwebserver3.RecordedRequest in project sonarlint-core by SonarSource.

the class MockWebServerExtension method beforeEach.

@Override
public void beforeEach(ExtensionContext context) throws Exception {
    server = new MockWebServer();
    responsesByPath.clear();
    final Dispatcher dispatcher = new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
            if (responsesByPath.containsKey(request.getPath())) {
                return responsesByPath.get(request.getPath());
            }
            return new MockResponse().setResponseCode(404);
        }
    };
    server.setDispatcher(dispatcher);
    server.start();
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) MockWebServer(mockwebserver3.MockWebServer) Dispatcher(mockwebserver3.Dispatcher)

Example 2 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class CacheTest method clientSuppliedIfNoneMatchSinceWithCachedResult.

@Test
public void clientSuppliedIfNoneMatchSinceWithCachedResult() throws Exception {
    String lastModifiedDate = formatDate(-3, TimeUnit.MINUTES);
    MockResponse response = new MockResponse().addHeader("Last-Modified: " + lastModifiedDate).addHeader("Date: " + formatDate(-2, TimeUnit.MINUTES)).addHeader("Cache-Control: max-age=0");
    RecordedRequest request = assertClientSuppliedCondition(response, "If-None-Match", "v1");
    assertThat(request.getHeader("If-None-Match")).isEqualTo("v1");
    assertThat(request.getHeader("If-Modified-Since")).isNull();
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 3 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class CacheTest method cacheControlNoCacheAndExpirationDateInTheFuture.

@Test
public void cacheControlNoCacheAndExpirationDateInTheFuture() throws Exception {
    String lastModifiedDate = formatDate(-2, TimeUnit.HOURS);
    RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("Last-Modified: " + lastModifiedDate).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).addHeader("Cache-Control: 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 4 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class CacheTest method assertConditionallyCached.

/**
 * @return the request with the conditional get headers.
 */
private RecordedRequest assertConditionallyCached(MockResponse response) throws Exception {
    // scenario 1: condition succeeds
    server.enqueue(response.setBody("A").setStatus("HTTP/1.1 200 A-OK"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    // scenario 2: condition fails
    server.enqueue(response.setBody("B").setStatus("HTTP/1.1 200 B-OK"));
    server.enqueue(new MockResponse().setStatus("HTTP/1.1 200 C-OK").setBody("C"));
    HttpUrl valid = server.url("/valid");
    Response response1 = get(valid);
    assertThat(response1.body().string()).isEqualTo("A");
    assertThat(response1.code()).isEqualTo(HttpURLConnection.HTTP_OK);
    assertThat(response1.message()).isEqualTo("A-OK");
    Response response2 = get(valid);
    assertThat(response2.body().string()).isEqualTo("A");
    assertThat(response2.code()).isEqualTo(HttpURLConnection.HTTP_OK);
    assertThat(response2.message()).isEqualTo("A-OK");
    HttpUrl invalid = server.url("/invalid");
    Response response3 = get(invalid);
    assertThat(response3.body().string()).isEqualTo("B");
    assertThat(response3.code()).isEqualTo(HttpURLConnection.HTTP_OK);
    assertThat(response3.message()).isEqualTo("B-OK");
    Response response4 = get(invalid);
    assertThat(response4.body().string()).isEqualTo("C");
    assertThat(response4.code()).isEqualTo(HttpURLConnection.HTTP_OK);
    assertThat(response4.message()).isEqualTo("C-OK");
    // regular get
    server.takeRequest();
    // conditional get
    return server.takeRequest();
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse)

Example 5 with RecordedRequest

use of mockwebserver3.RecordedRequest in project okhttp by square.

the class CacheTest method immutableIsNotCachedBeyondFreshnessLifetime.

@Test
public void immutableIsNotCachedBeyondFreshnessLifetime() throws Exception {
    // last modified: 115 seconds ago
    // served:  15 seconds ago
    // default lifetime: (115 - 15) / 10 = 10 seconds
    // expires:  10 seconds from served date = 5 seconds ago
    String lastModifiedDate = formatDate(-115, TimeUnit.SECONDS);
    RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("Cache-Control: immutable").addHeader("Last-Modified: " + lastModifiedDate).addHeader("Date: " + formatDate(-15, TimeUnit.SECONDS)));
    assertThat(conditionalRequest.getHeader("If-Modified-Since")).isEqualTo(lastModifiedDate);
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) 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