Search in sources :

Example 1 with EventSource

use of okhttp3.sse.EventSource in project okhttp by square.

the class EventSourceHttpTest method retainsAccept.

@Test
public void retainsAccept() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("" + "data: hey\n" + "\n").setHeader("content-type", "text/event-stream"));
    EventSource source = newEventSource("text/plain");
    listener.assertOpen();
    listener.assertEvent(null, null, "hey");
    listener.assertClose();
    assertThat(server.takeRequest().getHeader("Accept")).isEqualTo("text/plain");
}
Also used : MockResponse(mockwebserver3.MockResponse) EventSource(okhttp3.sse.EventSource) Test(org.junit.jupiter.api.Test)

Example 2 with EventSource

use of okhttp3.sse.EventSource in project okhttp by square.

the class EventSourceHttpTest method setsMissingAccept.

@Test
public void setsMissingAccept() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("" + "data: hey\n" + "\n").setHeader("content-type", "text/event-stream"));
    EventSource source = newEventSource();
    listener.assertOpen();
    listener.assertEvent(null, null, "hey");
    listener.assertClose();
    assertThat(server.takeRequest().getHeader("Accept")).isEqualTo("text/event-stream");
}
Also used : MockResponse(mockwebserver3.MockResponse) EventSource(okhttp3.sse.EventSource) Test(org.junit.jupiter.api.Test)

Example 3 with EventSource

use of okhttp3.sse.EventSource in project okhttp by square.

the class EventSourceHttpTest method fullCallTimeoutDoesNotApplyOnceConnected.

@Test
public void fullCallTimeoutDoesNotApplyOnceConnected() throws Exception {
    client = client.newBuilder().callTimeout(250, TimeUnit.MILLISECONDS).build();
    server.enqueue(new MockResponse().setBodyDelay(500, TimeUnit.MILLISECONDS).setHeader("content-type", "text/event-stream").setBody("data: hey\n\n"));
    EventSource source = newEventSource();
    assertThat(source.request().url().encodedPath()).isEqualTo("/");
    listener.assertOpen();
    listener.assertEvent(null, null, "hey");
    listener.assertClose();
}
Also used : MockResponse(mockwebserver3.MockResponse) EventSource(okhttp3.sse.EventSource) Test(org.junit.jupiter.api.Test)

Example 4 with EventSource

use of okhttp3.sse.EventSource in project okhttp by square.

the class EventSourceHttpTest method event.

@Test
public void event() {
    server.enqueue(new MockResponse().setBody("" + "data: hey\n" + "\n").setHeader("content-type", "text/event-stream"));
    EventSource source = newEventSource();
    assertThat(source.request().url().encodedPath()).isEqualTo("/");
    listener.assertOpen();
    listener.assertEvent(null, null, "hey");
    listener.assertClose();
}
Also used : MockResponse(mockwebserver3.MockResponse) EventSource(okhttp3.sse.EventSource) Test(org.junit.jupiter.api.Test)

Example 5 with EventSource

use of okhttp3.sse.EventSource in project okhttp by square.

the class EventSourceHttpTest method newEventSource.

private EventSource newEventSource(@Nullable String accept) {
    Request.Builder builder = new Request.Builder().url(server.url("/"));
    if (accept != null) {
        builder.header("Accept", accept);
    }
    Request request = builder.build();
    EventSource.Factory factory = EventSources.createFactory(client);
    return factory.newEventSource(request, listener);
}
Also used : EventSource(okhttp3.sse.EventSource) Request(okhttp3.Request)

Aggregations

EventSource (okhttp3.sse.EventSource)5 MockResponse (mockwebserver3.MockResponse)4 Test (org.junit.jupiter.api.Test)4 Request (okhttp3.Request)1