Search in sources :

Example 1 with ServerSentEvent

use of org.springframework.http.codec.ServerSentEvent in project spring-framework by spring-projects.

the class BodyInsertersTests method ofServerSentEventFlux.

@Test
public void ofServerSentEventFlux() throws Exception {
    ServerSentEvent<String> event = ServerSentEvent.builder("foo").build();
    Flux<ServerSentEvent<String>> body = Flux.just(event);
    BodyInserter<Flux<ServerSentEvent<String>>, ServerHttpResponse> inserter = BodyInserters.fromServerSentEvents(body);
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, this.context);
    StepVerifier.create(result).expectNextCount(0).expectComplete().verify();
}
Also used : ServerSentEvent(org.springframework.http.codec.ServerSentEvent) Flux(reactor.core.publisher.Flux) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test)

Example 2 with ServerSentEvent

use of org.springframework.http.codec.ServerSentEvent 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

Test (org.junit.Test)2 ServerSentEvent (org.springframework.http.codec.ServerSentEvent)2 Flux (reactor.core.publisher.Flux)2 Duration (java.time.Duration)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Before (org.junit.Before)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 AbstractHttpHandlerIntegrationTests (org.springframework.http.server.reactive.AbstractHttpHandlerIntegrationTests)1 HttpHandler (org.springframework.http.server.reactive.HttpHandler)1 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)1 MockServerHttpResponse (org.springframework.mock.http.server.reactive.test.MockServerHttpResponse)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 RestController (org.springframework.web.bind.annotation.RestController)1 DispatcherHandler (org.springframework.web.reactive.DispatcherHandler)1