use of org.springframework.http.server.reactive.HttpHandler in project spring-framework by spring-projects.
the class RouterFunctionsTests method toHttpHandlerHandlerResponseStatusException.
@Test
public void toHttpHandlerHandlerResponseStatusException() {
HandlerFunction<ServerResponse> handlerFunction = request -> Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found"));
RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
assertThat(result).isNotNull();
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
MockServerHttpResponse httpResponse = new MockServerHttpResponse();
result.handle(httpRequest, httpResponse).block();
assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
use of org.springframework.http.server.reactive.HttpHandler in project spring-framework by spring-projects.
the class RouterFunctionsTests method toHttpHandlerRouteNotFoundReturnsResponseStatusException.
@Test
public void toHttpHandlerRouteNotFoundReturnsResponseStatusException() {
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.accepted().build();
RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.GET("/path"), handlerFunction);
HandlerStrategies handlerStrategies = HandlerStrategies.empty().exceptionHandler((exchange, ex) -> {
exchange.getResponse().setStatusCode(HttpStatus.NOT_FOUND);
DataBuffer buffer = exchange.getResponse().bufferFactory().wrap("Custom response".getBytes(StandardCharsets.UTF_8));
return exchange.getResponse().writeWith(Flux.just(buffer));
}).build();
HttpHandler result = RouterFunctions.toHttpHandler(routerFunction, handlerStrategies);
assertThat(result).isNotNull();
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
MockServerHttpResponse httpResponse = new MockServerHttpResponse();
result.handle(httpRequest, httpResponse).block();
assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
assertThat(httpResponse.getBodyAsString().block()).isEqualTo("Custom response");
}
use of org.springframework.http.server.reactive.HttpHandler in project spring-framework by spring-projects.
the class RouterFunctionsTests method toHttpHandlerNormal.
@Test
public void toHttpHandlerNormal() {
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.accepted().build();
RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
assertThat(result).isNotNull();
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
MockServerHttpResponse httpResponse = new MockServerHttpResponse();
result.handle(httpRequest, httpResponse).block();
assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
}
use of org.springframework.http.server.reactive.HttpHandler in project spring-framework by spring-projects.
the class RouterFunctionsTests method toHttpHandlerHandlerReturnResponseStatusExceptionInResponseWriteTo.
@Test
public void toHttpHandlerHandlerReturnResponseStatusExceptionInResponseWriteTo() {
HandlerFunction<ServerResponse> handlerFunction = // Mono.<ServerResponse> is required for compilation in Eclipse
request -> Mono.just(new ServerResponse() {
@Override
public HttpStatus statusCode() {
return HttpStatus.OK;
}
@Override
public int rawStatusCode() {
return 200;
}
@Override
public HttpHeaders headers() {
return new HttpHeaders();
}
@Override
public MultiValueMap<String, ResponseCookie> cookies() {
return new LinkedMultiValueMap<>();
}
@Override
public Mono<Void> writeTo(ServerWebExchange exchange, Context context) {
return Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND, "Not found"));
}
});
RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction);
HttpHandler result = RouterFunctions.toHttpHandler(routerFunction);
assertThat(result).isNotNull();
MockServerHttpRequest httpRequest = MockServerHttpRequest.get("https://localhost").build();
MockServerHttpResponse httpResponse = new MockServerHttpResponse();
result.handle(httpRequest, httpResponse).block();
assertThat(httpResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
Aggregations