@Test public void all() { RequestPredicate predicate = RequestPredicates.all(); MockServerRequest request = MockServerRequest.builder().build(); assertTrue(predicate.test(request)); }
@Test public void toHttpHandlerHandlerReturnsException() { HandlerFunction<ServerResponse> handlerFunction = request -> Mono.error(new IllegalStateException()); 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.INTERNAL_SERVER_ERROR, httpResponse.getStatusCode()); }
@Test public void toHttpHandlerHandlerThrowsException() { HandlerFunction<ServerResponse> handlerFunction = request -> { throw new IllegalStateException(); }; 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.INTERNAL_SERVER_ERROR, httpResponse.getStatusCode()); }
@Override protected RouterFunction<?> routerFunction() { return RouterFunctions.route(RequestPredicates.GET("/"), request -> ServerResponse.ok().syncBody("FOO")) .andRoute(RequestPredicates.all(), request -> ServerResponse.ok().syncBody("BAR")); }
@Test public void toHttpHandlerWebFilter() { AtomicBoolean filterInvoked = new AtomicBoolean(); WebFilter webFilter = new WebFilter() { @Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { filterInvoked.set(true); return chain.filter(exchange); } }; HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.accepted().build(); RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(RequestPredicates.all(), handlerFunction); HandlerStrategies handlerStrategies = HandlerStrategies.builder() .webFilter(webFilter).build(); HttpHandler result = RouterFunctions.toHttpHandler(routerFunction, handlerStrategies); assertNotNull(result); MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build(); MockServerHttpResponse httpResponse = new MockServerHttpResponse(); result.handle(httpRequest, httpResponse).block(); assertEquals(HttpStatus.ACCEPTED, httpResponse.getStatusCode()); assertTrue(filterInvoked.get()); }
RouterFunctions.route(RequestPredicates.all(), handlerFunction);
@Test public void toHttpHandlerNormal() { 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()); }
@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); assertNotNull(result); MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build(); MockServerHttpResponse httpResponse = new MockServerHttpResponse(); result.handle(httpRequest, httpResponse).block(); assertEquals(HttpStatus.NOT_FOUND, httpResponse.getStatusCode()); }
RouterFunctions.route(RequestPredicates.all(), handlerFunction);
@Override protected RouterFunction<ServerResponse> getRoutingFunction(final ErrorAttributes errorAttributes) { return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse); }
@Override protected RouterFunction<?> routerFunction() { NestedHandler nestedHandler = new NestedHandler(); return nest(path("/foo/"), route(GET("/bar"), nestedHandler::pattern) .andRoute(GET("/baz"), nestedHandler::pattern)) .andNest(GET("/{foo}"), route(GET("/bar"), nestedHandler::variables).and( nest(GET("/{bar}"), route(GET("/{baz}"), nestedHandler::variables)))) .andRoute(path("/{qux}/quux").and(method(HttpMethod.GET)), nestedHandler::variables) .andRoute(all(), nestedHandler::variables); }
@Override protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) { return RouterFunctions.route(RequestPredicates.all(), this::handleException); }
@Override protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) { return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse); }
@Override protected RouterFunction<ServerResponse> getRoutingFunction(final ErrorAttributes errorAttributes) { return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse); }
static RouterFunction<?> doRoute(final ApiHandler apiHandler, final ErrorHandler errorHandler) { return nest(path(API_PATH), nest(accept(APPLICATION_JSON), route(GET(LOCATION_WITH_ADDRESS_PATH), apiHandler::getLocation) .andRoute(POST(LOCATION_PATH), apiHandler::postLocation) ).andOther(route(RequestPredicates.all(), errorHandler::notFound)) ); } }