Search in sources :

Example 6 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class EventListenerTest method conditionalCache.

@Test
public void conditionalCache() throws IOException {
    enableCache();
    server.enqueue(new MockResponse().addHeader("ETag: v1").setBody("abc"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertThat(response.code()).isEqualTo(200);
    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", "CacheConditionalHit", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "ResponseBodyStart", "ResponseBodyEnd", "CacheHit", "ConnectionReleased", "CallEnd");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 7 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class EventListenerTest method successfulDnsLookup.

@Test
public void successfulDnsLookup() 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();
    DnsStart dnsStart = listener.removeUpToEvent(DnsStart.class);
    assertThat(dnsStart.getCall()).isSameAs(call);
    assertThat(dnsStart.getDomainName()).isEqualTo(server.getHostName());
    DnsEnd dnsEnd = listener.removeUpToEvent(DnsEnd.class);
    assertThat(dnsEnd.getCall()).isSameAs(call);
    assertThat(dnsEnd.getDomainName()).isEqualTo(server.getHostName());
    assertThat(dnsEnd.getInetAddressList().size()).isEqualTo(1);
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) DnsStart(okhttp3.CallEvent.DnsStart) DnsEnd(okhttp3.CallEvent.DnsEnd) Test(org.junit.jupiter.api.Test)

Example 8 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class EventListenerTest method pooledConnectionFound.

@Test
public void pooledConnectionFound() throws IOException {
    server.enqueue(new MockResponse());
    server.enqueue(new MockResponse());
    // Seed the pool.
    Call call1 = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response1 = call1.execute();
    assertThat(response1.code()).isEqualTo(200);
    response1.body().close();
    ConnectionAcquired connectionAcquired1 = listener.removeUpToEvent(ConnectionAcquired.class);
    listener.clearAllEvents();
    Call call2 = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response2 = call2.execute();
    assertThat(response2.code()).isEqualTo(200);
    response2.body().close();
    ConnectionAcquired connectionAcquired2 = listener.removeUpToEvent(ConnectionAcquired.class);
    assertThat(connectionAcquired2.getConnection()).isSameAs(connectionAcquired1.getConnection());
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) ConnectionAcquired(okhttp3.CallEvent.ConnectionAcquired) Test(org.junit.jupiter.api.Test)

Example 9 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class EventListenerTest method successfulCallEventSequence.

@Test
public void successfulCallEventSequence() throws IOException {
    server.enqueue(new MockResponse().setBody("abc"));
    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) Test(org.junit.jupiter.api.Test)

Example 10 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class EventListenerTest method timeToFirstByteGapBetweenResponseHeaderStartAndEnd.

@Test
public void timeToFirstByteGapBetweenResponseHeaderStartAndEnd() throws IOException {
    long responseHeadersStartDelay = 250L;
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.EXPECT_CONTINUE).setHeadersDelay(responseHeadersStartDelay, TimeUnit.MILLISECONDS));
    Request request = new Request.Builder().url(server.url("/")).header("Expect", "100-continue").post(RequestBody.create("abc", MediaType.get("text/plain"))).build();
    Call call = client.newCall(request);
    try (Response response = call.execute()) {
        assertThat(response.body().string()).isEqualTo("");
    }
    listener.removeUpToEvent(ResponseHeadersStart.class);
    listener.takeEvent(RequestBodyStart.class, 0L);
    listener.takeEvent(RequestBodyEnd.class, 0L);
    listener.takeEvent(ResponseHeadersEnd.class, responseHeadersStartDelay);
}
Also used : MockResponse(mockwebserver3.MockResponse) 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