Search in sources :

Example 21 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class EventSourceHttpTest method cancelInEventShortCircuits.

@Test
public void cancelInEventShortCircuits() throws IOException {
    server.enqueue(new MockResponse().setBody("" + "data: hey\n" + "\n").setHeader("content-type", "text/event-stream"));
    // Will cancel in onOpen().
    listener.enqueueCancel();
    newEventSource();
    listener.assertOpen();
    listener.assertFailure("canceled");
}
Also used : MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 22 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class EventSourcesHttpTest method processResponse.

@Test
public void processResponse() throws IOException {
    server.enqueue(new MockResponse().setBody("" + "data: hey\n" + "\n").setHeader("content-type", "text/event-stream"));
    Request request = new Request.Builder().url(server.url("/")).build();
    Response response = client.newCall(request).execute();
    EventSources.processResponse(response, listener);
    listener.assertOpen();
    listener.assertEvent(null, null, "hey");
    listener.assertClose();
}
Also used : Response(okhttp3.Response) MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Request(okhttp3.Request) Test(org.junit.jupiter.api.Test)

Example 23 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method clientSuppliedIfNoneMatchSinceWithCachedResult.

@Test
public void clientSuppliedIfNoneMatchSinceWithCachedResult() throws Exception {
    String lastModifiedDate = formatDate(-3, TimeUnit.MINUTES);
    MockResponse response = new MockResponse().addHeader("Last-Modified: " + lastModifiedDate).addHeader("Date: " + formatDate(-2, TimeUnit.MINUTES)).addHeader("Cache-Control: max-age=0");
    RecordedRequest request = assertClientSuppliedCondition(response, "If-None-Match", "v1");
    assertThat(request.getHeader("If-None-Match")).isEqualTo("v1");
    assertThat(request.getHeader("If-Modified-Since")).isNull();
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 24 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method conditionalHitHeadersCombined.

@Test
public void conditionalHitHeadersCombined() throws Exception {
    server.enqueue(new MockResponse().addHeader("Etag", "a").addHeader("Cache-Control: max-age=0").addHeader("A: a1").addHeader("B: b2").addHeader("B: b3").setBody("abcd"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED).addHeader("B: b4").addHeader("B: b5").addHeader("C: c6"));
    Response response1 = get(server.url("/"));
    assertThat(response1.body().string()).isEqualTo("abcd");
    assertThat(response1.headers()).isEqualTo(Headers.of("Etag", "a", "Cache-Control", "max-age=0", "A", "a1", "B", "b2", "B", "b3", "Content-Length", "4"));
    // The original 'A' header is retained because the network response doesn't have one.
    // The original 'B' headers are replaced by the network response.
    // The network's 'C' header is added.
    Response response2 = get(server.url("/"));
    assertThat(response2.body().string()).isEqualTo("abcd");
    assertThat(response2.headers()).isEqualTo(Headers.of("Etag", "a", "Cache-Control", "max-age=0", "A", "a1", "Content-Length", "4", "B", "b4", "B", "b5", "C", "c6"));
}
Also used : MockResponse(mockwebserver3.MockResponse) MockResponse(mockwebserver3.MockResponse) Test(org.junit.jupiter.api.Test)

Example 25 with MockResponse

use of mockwebserver3.MockResponse in project okhttp by square.

the class CacheTest method cacheControlNoCacheAndExpirationDateInTheFuture.

@Test
public void cacheControlNoCacheAndExpirationDateInTheFuture() throws Exception {
    String lastModifiedDate = formatDate(-2, TimeUnit.HOURS);
    RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("Last-Modified: " + lastModifiedDate).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).addHeader("Cache-Control: no-cache"));
    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