Search in sources :

Example 41 with MockServerHttpResponse

use of org.springframework.mock.http.server.reactive.test.MockServerHttpResponse in project spring-framework by spring-projects.

the class ServerSentEventHttpMessageWriterTests method writeServerSentEvent.

@Test
public void writeServerSentEvent() {
    ServerSentEvent<String> event = ServerSentEvent.<String>builder().data("bar").id("c42").event("foo").comment("bla\nbla bla\nbla bla bla").retry(Duration.ofMillis(123L)).build();
    Mono<ServerSentEvent<String>> source = Mono.just(event);
    MockServerHttpResponse outputMessage = new MockServerHttpResponse();
    messageWriter.write(source, ResolvableType.forClass(ServerSentEvent.class), new MediaType("text", "event-stream"), outputMessage, Collections.emptyMap()).block(Duration.ofMillis(5000));
    StepVerifier.create(outputMessage.getBodyAsString()).expectNext("id:c42\n" + "event:foo\n" + "retry:123\n" + ":bla\n" + ":bla bla\n" + ":bla bla bla\n" + "data:bar\n\n").expectComplete().verify();
}
Also used : MediaType(org.springframework.http.MediaType) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test)

Example 42 with MockServerHttpResponse

use of org.springframework.mock.http.server.reactive.test.MockServerHttpResponse in project spring-framework by spring-projects.

the class ServerSentEventHttpMessageWriterTests method writePojoWithPrettyPrint.

// SPR-14899
@Test
public void writePojoWithPrettyPrint() {
    ObjectMapper mapper = Jackson2ObjectMapperBuilder.json().indentOutput(true).build();
    this.messageWriter = new ServerSentEventHttpMessageWriter(Collections.singletonList(new Jackson2JsonEncoder(mapper)));
    Flux<Pojo> source = Flux.just(new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
    MockServerHttpResponse outputMessage = new MockServerHttpResponse();
    messageWriter.write(source, ResolvableType.forClass(Pojo.class), MediaType.TEXT_EVENT_STREAM, outputMessage, Collections.emptyMap()).block(Duration.ofMillis(5000));
    StepVerifier.create(outputMessage.getBodyAsString()).expectNext("data:{\n" + "data:  \"foo\" : \"foofoo\",\n" + "data:  \"bar\" : \"barbar\"\n" + "data:}\n\n" + "data:{\n" + "data:  \"foo\" : \"foofoofoo\",\n" + "data:  \"bar\" : \"barbarbar\"\n" + "data:}\n\n").expectComplete().verify();
}
Also used : MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Test(org.junit.Test)

Aggregations

MockServerHttpResponse (org.springframework.mock.http.server.reactive.test.MockServerHttpResponse)42 Test (org.junit.Test)37 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)14 HttpHandler (org.springframework.http.server.reactive.HttpHandler)9 HttpHeaders (org.springframework.http.HttpHeaders)8 HttpStatus (org.springframework.http.HttpStatus)8 ServerWebExchange (org.springframework.web.server.ServerWebExchange)8 Mono (reactor.core.publisher.Mono)8 StepVerifier (reactor.test.StepVerifier)8 MediaType (org.springframework.http.MediaType)7 Assert (org.junit.Assert)6 Mockito (org.mockito.Mockito)6 ReactiveHttpOutputMessage (org.springframework.http.ReactiveHttpOutputMessage)6 ResponseStatusException (org.springframework.web.server.ResponseStatusException)6 HashMap (java.util.HashMap)5 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)4 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)4 Flux (reactor.core.publisher.Flux)4 Before (org.junit.Before)3 DataBuffer (org.springframework.core.io.buffer.DataBuffer)3