Search in sources :

Example 66 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method requestOnlyIfCachedWithUnhelpfulResponseCached.

@Test
public void requestOnlyIfCachedWithUnhelpfulResponseCached() throws IOException {
    server.enqueue(new MockResponse().setBody("A"));
    assertThat(get(server.url("/")).body().string()).isEqualTo("A");
    Request request = new Request.Builder().url(server.url("/")).header("Cache-Control", "only-if-cached").build();
    Response response = client.newCall(request).execute();
    assertThat(response.body().source().exhausted()).isTrue();
    assertThat(response.code()).isEqualTo(504);
    assertThat(cache.requestCount()).isEqualTo(2);
    assertThat(cache.networkCount()).isEqualTo(1);
    assertThat(cache.hitCount()).isEqualTo(0);
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest) Test(org.junit.jupiter.api.Test)

Example 67 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method varyMatchesRemovedRequestHeaderField.

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

Example 68 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method conditionalMissUpdatesCache.

/**
 * Test https://github.com/square/okhttp/issues/1712.
 */
@Test
public void conditionalMissUpdatesCache() throws Exception {
    server.enqueue(new MockResponse().addHeader("ETag: v1").setBody("A"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    server.enqueue(new MockResponse().addHeader("ETag: v2").setBody("B"));
    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("A");
    assertThat(get(url).body().string()).isEqualTo("B");
    assertThat(get(url).body().string()).isEqualTo("B");
    assertThat(server.takeRequest().getHeader("If-None-Match")).isNull();
    assertThat(server.takeRequest().getHeader("If-None-Match")).isEqualTo("v1");
    assertThat(server.takeRequest().getHeader("If-None-Match")).isEqualTo("v1");
    assertThat(server.takeRequest().getHeader("If-None-Match")).isEqualTo("v2");
}
Also used : MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 69 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class ConnectionCoalescingTest method skipsOnRedirectWhenCertificatePinningFails.

@Test
public void skipsOnRedirectWhenCertificatePinningFails() throws Exception {
    CertificatePinner pinner = new CertificatePinner.Builder().add("san.com", "sha1/afwiKY3RxoMmLkuRW1l7QsPZTJPwDS2pdDROQjXw8ig=").build();
    client = client.newBuilder().certificatePinner(pinner).build();
    server.enqueue(new MockResponse().setResponseCode(301).addHeader("Location", url.newBuilder().host("san.com").build()));
    server.enqueue(new MockResponse());
    try {
        execute(url);
        fail("expected a failed attempt to connect");
    } catch (SSLPeerUnverifiedException expected) {
    }
}
Also used : MockResponse(mockwebserver3.MockResponse) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) Test(org.junit.jupiter.api.Test)

Example 70 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class ConnectionCoalescingTest method commonThenAlternative.

/**
 * Test connecting to the main host then an alternative, although only subject alternative names
 * are used if present no special consideration of common name.
 */
@Test
public void commonThenAlternative() throws Exception {
    server.enqueue(new MockResponse());
    server.enqueue(new MockResponse());
    assert200Http2Response(execute(url), server.getHostName());
    HttpUrl sanUrl = url.newBuilder().host("san.com").build();
    assert200Http2Response(execute(sanUrl), "san.com");
    assertThat(client.connectionPool().connectionCount()).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