Search in sources :

Example 1 with ResponseStatusException

use of org.springframework.web.server.ResponseStatusException in project spring-framework by spring-projects.

the class RouterFunctionsTests method toHttpHandlerHandlerThrowResponseStatusExceptionInResponseWriteTo.

@Test
public void toHttpHandlerHandlerThrowResponseStatusExceptionInResponseWriteTo() 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) {
            throw 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());
}
Also used : HttpStatus(org.springframework.http.HttpStatus) Mockito(org.mockito.Mockito) StepVerifier(reactor.test.StepVerifier) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Assert(org.junit.Assert) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHeaders(org.springframework.http.HttpHeaders) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHandler(org.springframework.http.server.reactive.HttpHandler) HttpStatus(org.springframework.http.HttpStatus) Mono(reactor.core.publisher.Mono) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) Test(org.junit.Test)

Example 2 with ResponseStatusException

use of org.springframework.web.server.ResponseStatusException in project spring-framework by spring-projects.

the class ResponseStatusExceptionResolverTests method responseStatusException.

@Test
public void responseStatusException() throws Exception {
    ResponseStatusException ex = new ResponseStatusException(HttpStatus.BAD_REQUEST, "The reason");
    ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
    assertResolved(mav, 400, null);
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) ResponseStatusException(org.springframework.web.server.ResponseStatusException) Test(org.junit.Test)

Example 3 with ResponseStatusException

use of org.springframework.web.server.ResponseStatusException in project spring-framework by spring-projects.

the class RouterFunctionsTests method toHttpHandlerHandlerResponseStatusException.

@Test
public void toHttpHandlerHandlerResponseStatusException() throws Exception {
    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);
    assertNotNull(result);
    MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build();
    MockServerHttpResponse httpResponse = new MockServerHttpResponse();
    result.handle(httpRequest, httpResponse).block();
    assertEquals(HttpStatus.NOT_FOUND, httpResponse.getStatusCode());
}
Also used : HttpStatus(org.springframework.http.HttpStatus) Mockito(org.mockito.Mockito) StepVerifier(reactor.test.StepVerifier) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Assert(org.junit.Assert) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test)

Example 4 with ResponseStatusException

use of org.springframework.web.server.ResponseStatusException 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());
}
Also used : HttpStatus(org.springframework.http.HttpStatus) Mockito(org.mockito.Mockito) StepVerifier(reactor.test.StepVerifier) HttpHandler(org.springframework.http.server.reactive.HttpHandler) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Assert(org.junit.Assert) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHeaders(org.springframework.http.HttpHeaders) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHandler(org.springframework.http.server.reactive.HttpHandler) HttpStatus(org.springframework.http.HttpStatus) Mono(reactor.core.publisher.Mono) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 ResponseStatusException (org.springframework.web.server.ResponseStatusException)4 Assert (org.junit.Assert)3 Mockito (org.mockito.Mockito)3 HttpHeaders (org.springframework.http.HttpHeaders)3 HttpStatus (org.springframework.http.HttpStatus)3 HttpHandler (org.springframework.http.server.reactive.HttpHandler)3 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)3 MockServerHttpResponse (org.springframework.mock.http.server.reactive.test.MockServerHttpResponse)3 ServerWebExchange (org.springframework.web.server.ServerWebExchange)3 Mono (reactor.core.publisher.Mono)3 StepVerifier (reactor.test.StepVerifier)3 ModelAndView (org.springframework.web.servlet.ModelAndView)1