Search in sources :

Example 41 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class EventListenerTest method requestBodyFail.

private void requestBodyFail(@Nullable Protocol expectedProtocol) {
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_DURING_REQUEST_BODY));
    NonCompletingRequestBody request = new NonCompletingRequestBody();
    Call call = client.newCall(new Request.Builder().url(server.url("/")).post(request).build());
    try {
        call.execute();
        fail();
    } catch (IOException expected) {
    }
    if (expectedProtocol != null) {
        ConnectionAcquired connectionAcquired = listener.removeUpToEvent(ConnectionAcquired.class);
        assertThat(connectionAcquired.getConnection().protocol()).isEqualTo(expectedProtocol);
    }
    CallFailed callFailed = listener.removeUpToEvent(CallFailed.class);
    assertThat(callFailed.getIoe()).isNotNull();
    assertThat(request.ioe).isNotNull();
}
Also used : MockResponse(mockwebserver3.MockResponse) ConnectionAcquired(okhttp3.CallEvent.ConnectionAcquired) CallFailed(okhttp3.CallEvent.CallFailed) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 42 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class EventListenerTest method successfulCallEventSequenceForEnqueue.

@Test
public void successfulCallEventSequenceForEnqueue() throws Exception {
    server.enqueue(new MockResponse().setBody("abc"));
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    final CountDownLatch completionLatch = new CountDownLatch(1);
    Callback callback = new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            completionLatch.countDown();
        }

        @Override
        public void onResponse(Call call, Response response) {
            response.close();
            completionLatch.countDown();
        }
    };
    call.enqueue(callback);
    completionLatch.await();
    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) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 43 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class EventListenerTest method cacheHit.

@Test
public void cacheHit() throws IOException {
    enableCache();
    server.enqueue(new MockResponse().setBody("abc").addHeader("cache-control: public, max-age=300"));
    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.close();
    listener.clearAllEvents();
    call = call.clone();
    response = call.execute();
    assertThat(response.code()).isEqualTo(200);
    assertThat(response.body().string()).isEqualTo("abc");
    response.close();
    assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "CacheHit", "CallEnd");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 44 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class EventListenerTest method successfulConnect.

@Test
public void successfulConnect() throws IOException {
    server.enqueue(new MockResponse());
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertThat(response.code()).isEqualTo(200);
    response.body().close();
    InetAddress address = client.dns().lookup(server.getHostName()).get(0);
    InetSocketAddress expectedAddress = new InetSocketAddress(address, server.getPort());
    ConnectStart connectStart = listener.removeUpToEvent(ConnectStart.class);
    assertThat(connectStart.getCall()).isSameAs(call);
    assertThat(connectStart.getInetSocketAddress()).isEqualTo(expectedAddress);
    assertThat(connectStart.getProxy()).isEqualTo(Proxy.NO_PROXY);
    ConnectEnd connectEnd = listener.removeUpToEvent(ConnectEnd.class);
    assertThat(connectEnd.getCall()).isSameAs(call);
    assertThat(connectEnd.getInetSocketAddress()).isEqualTo(expectedAddress);
    assertThat(connectEnd.getProtocol()).isEqualTo(Protocol.HTTP_1_1);
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) SecureConnectEnd(okhttp3.CallEvent.SecureConnectEnd) ConnectEnd(okhttp3.CallEvent.ConnectEnd) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) SecureConnectStart(okhttp3.CallEvent.SecureConnectStart) ConnectStart(okhttp3.CallEvent.ConnectStart) Test(org.junit.jupiter.api.Test)

Example 45 with MockResponse

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

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