Search in sources :

Example 1 with ServerSentEvent

use of cn.taketoday.http.codec.ServerSentEvent in project today-infrastructure by TAKETODAY.

the class BodyInsertersTests method ofServerSentEventFlux.

@Test
public void ofServerSentEventFlux() {
    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(cn.taketoday.http.codec.ServerSentEvent) Flux(reactor.core.publisher.Flux) MockServerHttpResponse(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpResponse) ServerHttpResponse(cn.taketoday.http.server.reactive.ServerHttpResponse) MockServerHttpResponse(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 2 with ServerSentEvent

use of cn.taketoday.http.codec.ServerSentEvent in project today-framework by TAKETODAY.

the class ReactiveTypeHandlerTests method writeServerSentEventsWithBuilder.

@Test
public void writeServerSentEventsWithBuilder() throws Exception {
    ResolvableType type = ResolvableType.fromClassWithGenerics(ServerSentEvent.class, String.class);
    Sinks.Many<ServerSentEvent<?>> sink = Sinks.many().unicast().onBackpressureBuffer();
    SseEmitter sseEmitter = (SseEmitter) handleValue(sink.asFlux(), Flux.class, type);
    EmitterHandler emitterHandler = new EmitterHandler();
    sseEmitter.initialize(emitterHandler);
    sink.tryEmitNext(ServerSentEvent.builder("foo").id("1").build());
    sink.tryEmitNext(ServerSentEvent.builder("bar").id("2").build());
    sink.tryEmitNext(ServerSentEvent.builder("baz").id("3").build());
    sink.tryEmitComplete();
    assertThat(emitterHandler.getValuesAsText()).isEqualTo("id:1\ndata:foo\n\nid:2\ndata:bar\n\nid:3\ndata:baz\n\n");
}
Also used : Sinks(reactor.core.publisher.Sinks) ServerSentEvent(cn.taketoday.http.codec.ServerSentEvent) Flux(reactor.core.publisher.Flux) ResolvableType(cn.taketoday.core.ResolvableType) Test(org.junit.jupiter.api.Test)

Example 3 with ServerSentEvent

use of cn.taketoday.http.codec.ServerSentEvent in project today-framework by TAKETODAY.

the class BodyInsertersTests method ofServerSentEventFlux.

@Test
public void ofServerSentEventFlux() {
    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(cn.taketoday.http.codec.ServerSentEvent) Flux(reactor.core.publisher.Flux) MockServerHttpResponse(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpResponse) ServerHttpResponse(cn.taketoday.http.server.reactive.ServerHttpResponse) MockServerHttpResponse(cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 4 with ServerSentEvent

use of cn.taketoday.http.codec.ServerSentEvent in project today-infrastructure by TAKETODAY.

the class ReactiveTypeHandlerTests method writeServerSentEventsWithBuilder.

@Test
public void writeServerSentEventsWithBuilder() throws Exception {
    ResolvableType type = ResolvableType.fromClassWithGenerics(ServerSentEvent.class, String.class);
    Sinks.Many<ServerSentEvent<?>> sink = Sinks.many().unicast().onBackpressureBuffer();
    SseEmitter sseEmitter = (SseEmitter) handleValue(sink.asFlux(), Flux.class, type);
    EmitterHandler emitterHandler = new EmitterHandler();
    sseEmitter.initialize(emitterHandler);
    sink.tryEmitNext(ServerSentEvent.builder("foo").id("1").build());
    sink.tryEmitNext(ServerSentEvent.builder("bar").id("2").build());
    sink.tryEmitNext(ServerSentEvent.builder("baz").id("3").build());
    sink.tryEmitComplete();
    assertThat(emitterHandler.getValuesAsText()).isEqualTo("id:1\ndata:foo\n\nid:2\ndata:bar\n\nid:3\ndata:baz\n\n");
}
Also used : Sinks(reactor.core.publisher.Sinks) ServerSentEvent(cn.taketoday.http.codec.ServerSentEvent) Flux(reactor.core.publisher.Flux) ResolvableType(cn.taketoday.core.ResolvableType) Test(org.junit.jupiter.api.Test)

Aggregations

ServerSentEvent (cn.taketoday.http.codec.ServerSentEvent)4 Test (org.junit.jupiter.api.Test)4 Flux (reactor.core.publisher.Flux)4 ResolvableType (cn.taketoday.core.ResolvableType)2 ServerHttpResponse (cn.taketoday.http.server.reactive.ServerHttpResponse)2 MockServerHttpResponse (cn.taketoday.web.testfixture.http.server.reactive.MockServerHttpResponse)2 Sinks (reactor.core.publisher.Sinks)2