use of org.springframework.mock.http.server.reactive.test.MockServerHttpRequest in project spring-framework by spring-projects.
the class RouterFunctionsTests method toHttpHandlerNormal.
@Test
public void toHttpHandlerNormal() throws Exception {
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.accepted().build();
RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
assertNotNull(result);
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build();
MockServerHttpResponse httpResponse = new MockServerHttpResponse();
result.handle(httpRequest, httpResponse).block();
assertEquals(HttpStatus.ACCEPTED, httpResponse.getStatusCode());
}
use of org.springframework.mock.http.server.reactive.test.MockServerHttpRequest in project spring-framework by spring-projects.
the class RouterFunctionsTests method toHttpHandlerHandlerReturnResponseStatusExceptionInResponseWriteTo.
@Test
public void toHttpHandlerHandlerReturnResponseStatusExceptionInResponseWriteTo() throws Exception {
HandlerFunction<ServerResponse> handlerFunction = // Mono.<ServerResponse> is required for compilation in Eclipse
request -> Mono.<ServerResponse>just(new ServerResponse() {
@Override
public HttpStatus statusCode() {
return HttpStatus.OK;
}
@Override
public HttpHeaders headers() {
return new HttpHeaders();
}
@Override
public Mono<Void> writeTo(ServerWebExchange exchange, HandlerStrategies strategies) {
return Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found"));
}
});
RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
assertNotNull(result);
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build();
MockServerHttpResponse httpResponse = new MockServerHttpResponse();
result.handle(httpRequest, httpResponse).block();
assertEquals(HttpStatus.NOT_FOUND, httpResponse.getStatusCode());
}
use of org.springframework.mock.http.server.reactive.test.MockServerHttpRequest in project spring-framework by spring-projects.
the class ServerSentEventHttpMessageReaderTests method readString.
@Test
public void readString() {
String body = "data:foo\ndata:bar\n\ndata:baz\n\n";
MockServerHttpRequest request = MockServerHttpRequest.post("/").body(body);
Flux<String> data = messageReader.read(ResolvableType.forClass(String.class), request, Collections.emptyMap()).cast(String.class);
StepVerifier.create(data).expectNextMatches(elem -> elem.equals("foo\nbar")).expectNextMatches(elem -> elem.equals("baz")).expectComplete().verify();
}
use of org.springframework.mock.http.server.reactive.test.MockServerHttpRequest in project spring-framework by spring-projects.
the class ServerSentEventHttpMessageReaderTests method readServerSentEventsWithMultipleChunks.
@Test
public void readServerSentEventsWithMultipleChunks() {
MockServerHttpRequest request = MockServerHttpRequest.post("/").body(Flux.just(stringBuffer("id:c42\nev"), stringBuffer("ent:foo\nretry:123\n:bla\n:bla bla\n:bla bla bla\ndata:"), stringBuffer("bar\n\nid:c43\nevent:bar\nretry:456\ndata:baz\n\n")));
Flux<ServerSentEvent> events = messageReader.read(ResolvableType.forClassWithGenerics(ServerSentEvent.class, String.class), request, Collections.emptyMap()).cast(ServerSentEvent.class);
StepVerifier.create(events).consumeNextWith(event -> {
assertEquals("c42", event.id().get());
assertEquals("foo", event.event().get());
assertEquals(Duration.ofMillis(123), event.retry().get());
assertEquals("bla\nbla bla\nbla bla bla", event.comment().get());
assertEquals("bar", event.data().get());
}).consumeNextWith(event -> {
assertEquals("c43", event.id().get());
assertEquals("bar", event.event().get());
assertEquals(Duration.ofMillis(456), event.retry().get());
assertFalse(event.comment().isPresent());
assertEquals("baz", event.data().get());
}).expectComplete().verify();
}
use of org.springframework.mock.http.server.reactive.test.MockServerHttpRequest in project spring-framework by spring-projects.
the class ServerSentEventHttpMessageReaderTests method readPojo.
@Test
public void readPojo() {
MockServerHttpRequest request = MockServerHttpRequest.post("/").body("data:{\"foo\": \"foofoo\", \"bar\": \"barbar\"}\n\n" + "data:{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}\n\n");
Flux<Pojo> data = messageReader.read(ResolvableType.forClass(Pojo.class), request, Collections.emptyMap()).cast(Pojo.class);
StepVerifier.create(data).consumeNextWith(pojo -> {
assertEquals("foofoo", pojo.getFoo());
assertEquals("barbar", pojo.getBar());
}).consumeNextWith(pojo -> {
assertEquals("foofoofoo", pojo.getFoo());
assertEquals("barbarbar", pojo.getBar());
}).expectComplete().verify();
}
Aggregations