Search in sources :

Example 11 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class EventListenerTest method multipleDnsLookupsForSingleCall.

@Test
public void multipleDnsLookupsForSingleCall() throws IOException {
    server.enqueue(new MockResponse().setResponseCode(301).setHeader("Location", "http://www.fakeurl:" + server.getPort()));
    server.enqueue(new MockResponse());
    FakeDns dns = new FakeDns();
    dns.set("fakeurl", client.dns().lookup(server.getHostName()));
    dns.set("www.fakeurl", client.dns().lookup(server.getHostName()));
    client = client.newBuilder().dns(dns).build();
    Call call = client.newCall(new Request.Builder().url("http://fakeurl:" + server.getPort()).build());
    Response response = call.execute();
    assertThat(response.code()).isEqualTo(200);
    response.body().close();
    listener.removeUpToEvent(DnsStart.class);
    listener.removeUpToEvent(DnsEnd.class);
    listener.removeUpToEvent(DnsStart.class);
    listener.removeUpToEvent(DnsEnd.class);
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 12 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class EventListenerTest method emptyResponseBodyConnectionClose.

@Test
public void emptyResponseBodyConnectionClose() throws IOException {
    server.enqueue(new MockResponse().addHeader("Connection", "close").setBody(""));
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    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 13 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class EventListenerTest method conditionalCacheMiss.

@Test
public void conditionalCacheMiss() throws IOException {
    enableCache();
    server.enqueue(new MockResponse().addHeader("ETag: v1").setBody("abc"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_OK).addHeader("ETag: v2").setBody("abd"));
    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("abd");
    response.close();
    assertThat(listener.recordedEventTypes()).containsExactly("CallStart", "CacheConditionalHit", "ConnectionAcquired", "RequestHeadersStart", "RequestHeadersEnd", "ResponseHeadersStart", "ResponseHeadersEnd", "CacheMiss", "ResponseBodyStart", "ResponseBodyEnd", "ConnectionReleased", "CallEnd");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 14 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class EventListenerTest method requestBodySuccess.

private void requestBodySuccess(RequestBody body, Matcher<Long> requestBodyBytes, Matcher<Long> responseHeaderLength) throws IOException {
    server.enqueue(new MockResponse().setResponseCode(200).setBody("World!"));
    Call call = client.newCall(new Request.Builder().url(server.url("/")).post(body).build());
    Response response = call.execute();
    assertThat(response.body().string()).isEqualTo("World!");
    assertBytesReadWritten(listener, any(Long.class), requestBodyBytes, responseHeaderLength, equalTo(6L));
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse)

Example 15 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method requestMaxAge.

@Test
public void requestMaxAge() throws IOException {
    server.enqueue(new MockResponse().setBody("A").addHeader("Last-Modified: " + formatDate(-2, TimeUnit.HOURS)).addHeader("Date: " + formatDate(-1, TimeUnit.MINUTES)).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)));
    server.enqueue(new MockResponse().setBody("B"));
    assertThat(get(server.url("/")).body().string()).isEqualTo("A");
    Request request = new Request.Builder().url(server.url("/")).header("Cache-Control", "max-age=30").build();
    Response response = client.newCall(request).execute();
    assertThat(response.body().string()).isEqualTo("B");
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest) 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