use of org.springframework.http.HttpStatus in project spring-framework by spring-projects.
the class RouterFunctionBuilderTests method filters.
@Test
public void filters() {
AtomicInteger filterCount = new AtomicInteger();
RouterFunction<?> route = RouterFunctions.route().GET("/foo", request -> ServerResponse.ok().build()).GET("/bar", request -> Mono.error(new IllegalStateException())).before(request -> {
int count = filterCount.getAndIncrement();
assertThat(count).isEqualTo(0);
return request;
}).after((request, response) -> {
int count = filterCount.getAndIncrement();
assertThat(count).isEqualTo(3);
return response;
}).filter((request, next) -> {
int count = filterCount.getAndIncrement();
assertThat(count).isEqualTo(1);
Mono<ServerResponse> responseMono = next.handle(request);
count = filterCount.getAndIncrement();
assertThat(count).isEqualTo(2);
return responseMono;
}).onError(IllegalStateException.class, (e, request) -> ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).build()).build();
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://localhost/foo").build();
ServerRequest fooRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
Mono<ServerResponse> fooResponseMono = route.route(fooRequest).flatMap(handlerFunction -> handlerFunction.handle(fooRequest));
StepVerifier.create(fooResponseMono).consumeNextWith(serverResponse -> assertThat(filterCount.get()).isEqualTo(4)).verifyComplete();
filterCount.set(0);
mockRequest = MockServerHttpRequest.get("https://localhost/bar").build();
ServerRequest barRequest = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
Mono<Integer> barResponseMono = route.route(barRequest).flatMap(handlerFunction -> handlerFunction.handle(barRequest)).map(ServerResponse::statusCode).map(HttpStatus::value);
StepVerifier.create(barResponseMono).expectNext(500).verifyComplete();
}
use of org.springframework.http.HttpStatus in project spring-framework by spring-projects.
the class RouterFunctionsTests method toHttpHandlerHandlerThrowResponseStatusExceptionInResponseWriteTo.
@Test
public void toHttpHandlerHandlerThrowResponseStatusExceptionInResponseWriteTo() {
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) {
throw 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.HttpStatus in project spring-framework by spring-projects.
the class DefaultServerResponseBuilderTests method statusCode.
@Test
public void statusCode() {
HttpStatus statusCode = HttpStatus.ACCEPTED;
Mono<ServerResponse> result = ServerResponse.status(statusCode).build();
StepVerifier.create(result).expectNextMatches(response -> statusCode.equals(response.statusCode())).expectComplete().verify();
}
use of org.springframework.http.HttpStatus in project spring-framework by spring-projects.
the class RouterFunctionBuilderTests method filters.
@Test
void filters() {
AtomicInteger filterCount = new AtomicInteger();
RouterFunction<ServerResponse> route = RouterFunctions.route().GET("/foo", request -> ServerResponse.ok().build()).GET("/bar", request -> {
throw new IllegalStateException();
}).before(request -> {
int count = filterCount.getAndIncrement();
assertThat(count).isEqualTo(0);
return request;
}).after((request, response) -> {
int count = filterCount.getAndIncrement();
assertThat(count).isEqualTo(3);
return response;
}).filter((request, next) -> {
int count = filterCount.getAndIncrement();
assertThat(count).isEqualTo(1);
ServerResponse responseMono = next.handle(request);
count = filterCount.getAndIncrement();
assertThat(count).isEqualTo(2);
return responseMono;
}).onError(IllegalStateException.class, (e, request) -> ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).build()).build();
ServerRequest fooRequest = initRequest("GET", "/foo");
route.route(fooRequest).map(handlerFunction -> handle(handlerFunction, fooRequest));
assertThat(filterCount.get()).isEqualTo(4);
filterCount.set(0);
ServerRequest barRequest = initRequest("GET", "/bar");
Optional<HttpStatus> responseStatus = route.route(barRequest).map(handlerFunction -> handle(handlerFunction, barRequest)).map(ServerResponse::statusCode);
assertThat(responseStatus).contains(HttpStatus.INTERNAL_SERVER_ERROR);
}
use of org.springframework.http.HttpStatus in project spring-framework by spring-projects.
the class RouterFunctionBuilderTests method resources.
@Test
void resources() {
Resource resource = new ClassPathResource("/org/springframework/web/servlet/function/");
assertThat(resource.exists()).isTrue();
RouterFunction<ServerResponse> route = RouterFunctions.route().resources("/resources/**", resource).build();
ServerRequest resourceRequest = initRequest("GET", "/resources/response.txt");
Optional<HttpStatus> responseStatus = route.route(resourceRequest).map(handlerFunction -> handle(handlerFunction, resourceRequest)).map(ServerResponse::statusCode);
assertThat(responseStatus).contains(HttpStatus.OK);
ServerRequest invalidRequest = initRequest("POST", "/resources/foo.txt");
responseStatus = route.route(invalidRequest).map(handlerFunction -> handle(handlerFunction, invalidRequest)).map(ServerResponse::statusCode);
assertThat(responseStatus).isEmpty();
}
Aggregations