Search in sources :

Example 36 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method postInvalidatesCacheWithUncacheableResponse.

@Test
public void postInvalidatesCacheWithUncacheableResponse() throws Exception {
    // 1. Seed the cache.
    // 2. Invalidate it with an uncacheable response.
    // 3. Expect a cache miss.
    server.enqueue(new MockResponse().setBody("A").addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)));
    server.enqueue(new MockResponse().setBody("B").setResponseCode(500));
    server.enqueue(new MockResponse().setBody("C"));
    HttpUrl url = server.url("/");
    assertThat(get(url).body().string()).isEqualTo("A");
    Request request = new Request.Builder().url(url).method("POST", requestBodyOrNull("POST")).build();
    Response invalidate = client.newCall(request).execute();
    assertThat(invalidate.body().string()).isEqualTo("B");
    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 37 with MockResponse

use of mockwebserver3.MockResponse 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 38 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method getHeadersReturnsCachedHopByHopHeaders.

@Test
public void getHeadersReturnsCachedHopByHopHeaders() throws Exception {
    server.enqueue(new MockResponse().addHeader("Transfer-Encoding: identity").addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").setBody("A"));
    server.enqueue(new MockResponse().addHeader("Transfer-Encoding: none").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    Response response1 = get(server.url("/"));
    assertThat(response1.body().string()).isEqualTo("A");
    assertThat(response1.header("Transfer-Encoding")).isEqualTo("identity");
    Response response2 = get(server.url("/"));
    assertThat(response2.body().string()).isEqualTo("A");
    assertThat(response2.header("Transfer-Encoding")).isEqualTo("identity");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 39 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class EventListenerTest method successfulCallEventSequenceWithListener.

@Test
public void successfulCallEventSequenceWithListener() throws IOException {
    server.enqueue(new MockResponse().setBody("abc"));
    client = client.newBuilder().addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build();
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertThat(response.code()).isEqualTo(200);
    assertThat(response.body().string()).isEqualTo("abc");
    response.body().close();
    assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "ProxySelectStart", "ProxySelectEnd", "DnsStart", "DnsEnd", "ConnectStart", "ConnectEnd", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "ConnectionReleased", "CallEnd");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Test(org.junit.jupiter.api.Test)

Example 40 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class EventListenerTest method authenticatingTunnelProxyConnect.

@Test
public void authenticatingTunnelProxyConnect() throws IOException {
    enableTlsWithTunnel(true);
    server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\"").addHeader("Connection: close"));
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END));
    server.enqueue(new MockResponse());
    client = client.newBuilder().proxy(server.toProxyAddress()).proxyAuthenticator(new RecordingOkAuthenticator("password", "Basic")).build();
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertThat(response.code()).isEqualTo(200);
    response.body().close();
    listener.removeUpToEvent(ConnectStart.class);
    ConnectEnd connectEnd = listener.removeUpToEvent(ConnectEnd.class);
    assertThat(connectEnd.getProtocol()).isNull();
    listener.removeUpToEvent(ConnectStart.class);
    listener.removeUpToEvent(ConnectEnd.class);
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) SecureConnectEnd(okhttp3.CallEvent.SecureConnectEnd) ConnectEnd(okhttp3.CallEvent.ConnectEnd) 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