Search in sources :

Example 1 with TEXT_EVENT_STREAM

use of org.springframework.http.MediaType.TEXT_EVENT_STREAM in project spring-framework by spring-projects.

the class SseIntegrationTests method sseAsEvent.

@Test
public void sseAsEvent() throws Exception {
    ResolvableType type = forClassWithGenerics(ServerSentEvent.class, String.class);
    Flux<ServerSentEvent<String>> result = this.webClient.get().uri("/event").accept(TEXT_EVENT_STREAM).exchange().flatMap(response -> response.body(toFlux(type)));
    StepVerifier.create(result).consumeNextWith(event -> {
        assertEquals("0", event.id().get());
        assertEquals("foo", event.data().get());
        assertEquals("bar", event.comment().get());
        assertFalse(event.event().isPresent());
        assertFalse(event.retry().isPresent());
    }).consumeNextWith(event -> {
        assertEquals("1", event.id().get());
        assertEquals("foo", event.data().get());
        assertEquals("bar", event.comment().get());
        assertFalse(event.event().isPresent());
        assertFalse(event.retry().isPresent());
    }).thenCancel().verify(Duration.ofSeconds(5L));
}
Also used : StepVerifier(reactor.test.StepVerifier) WebHttpHandlerBuilder(org.springframework.web.server.adapter.WebHttpHandlerBuilder) BodyExtractors.toFlux(org.springframework.web.reactive.function.BodyExtractors.toFlux) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) WebClient(org.springframework.web.reactive.function.client.WebClient) Test(org.junit.Test) DispatcherHandler(org.springframework.web.reactive.DispatcherHandler) RestController(org.springframework.web.bind.annotation.RestController) TEXT_EVENT_STREAM(org.springframework.http.MediaType.TEXT_EVENT_STREAM) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Configuration(org.springframework.context.annotation.Configuration) Flux(reactor.core.publisher.Flux) ServerSentEvent(org.springframework.http.codec.ServerSentEvent) HttpHandler(org.springframework.http.server.reactive.HttpHandler) EnableWebFlux(org.springframework.web.reactive.config.EnableWebFlux) Assert.assertFalse(org.junit.Assert.assertFalse) Duration(java.time.Duration) AbstractHttpHandlerIntegrationTests(org.springframework.http.server.reactive.AbstractHttpHandlerIntegrationTests) ResolvableType.forClassWithGenerics(org.springframework.core.ResolvableType.forClassWithGenerics) Bean(org.springframework.context.annotation.Bean) ResolvableType(org.springframework.core.ResolvableType) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) ServerSentEvent(org.springframework.http.codec.ServerSentEvent) ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.Test)

Aggregations

Duration (java.time.Duration)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Before (org.junit.Before)1 Test (org.junit.Test)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 Bean (org.springframework.context.annotation.Bean)1 Configuration (org.springframework.context.annotation.Configuration)1 ResolvableType (org.springframework.core.ResolvableType)1 ResolvableType.forClassWithGenerics (org.springframework.core.ResolvableType.forClassWithGenerics)1 TEXT_EVENT_STREAM (org.springframework.http.MediaType.TEXT_EVENT_STREAM)1 ServerSentEvent (org.springframework.http.codec.ServerSentEvent)1 AbstractHttpHandlerIntegrationTests (org.springframework.http.server.reactive.AbstractHttpHandlerIntegrationTests)1 HttpHandler (org.springframework.http.server.reactive.HttpHandler)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 RestController (org.springframework.web.bind.annotation.RestController)1 DispatcherHandler (org.springframework.web.reactive.DispatcherHandler)1 EnableWebFlux (org.springframework.web.reactive.config.EnableWebFlux)1 BodyExtractors.toFlux (org.springframework.web.reactive.function.BodyExtractors.toFlux)1 WebClient (org.springframework.web.reactive.function.client.WebClient)1