@Override public RouterFunctions.Builder GET(String pattern, HandlerFunction<ServerResponse> handlerFunction) { return add(RequestPredicates.GET(pattern), handlerFunction); }
@Override public RouterFunctions.Builder GET(String pattern, RequestPredicate predicate, HandlerFunction<ServerResponse> handlerFunction) { return add(RequestPredicates.GET(pattern).and(predicate), handlerFunction); }
@Bean public RouterFunction<EntityResponse<Person>> monoRouterFunction(PersonHandler personHandler) { return route(RequestPredicates.GET("/mono"), personHandler::mono); }
@Bean public RouterFunction<ServerResponse> fluxRouterFunction(PersonHandler personHandler) { return route(RequestPredicates.GET("/flux"), personHandler::flux); }
@Bean RouterFunction<ServerResponse> routerFunction(ExceptionFallbackHandler exceptionFallbackHandler) { return route(GET("/exceptionFallback"), exceptionFallbackHandler::retrieveExceptionInfo); } }
@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<?> routerFunction() { RenderingResponseHandler handler = new RenderingResponseHandler(); RouterFunction<RenderingResponse> normalRoute = route(GET("/normal"), handler::render); RouterFunction<RenderingResponse> filteredRoute = route(GET("/filter"), handler::render) .filter(ofResponseProcessor( response -> RenderingResponse.from(response) .modelAttribute("qux", "quux") .build())); return normalRoute.and(filteredRoute); }
@Before public void setUp() throws Exception { RouterFunction<?> route = route(GET("/test"), request -> ServerResponse.ok().syncBody("It works!")); this.testClient = WebTestClient.bindToRouterFunction(route).build(); }
@Override protected RouterFunction<?> routerFunction() { return RouterFunctions.route(RequestPredicates.GET("/"), request -> ServerResponse.ok().syncBody("FOO")) .andRoute(RequestPredicates.all(), request -> ServerResponse.ok().syncBody("BAR")); }
@Before public void start() throws Exception { HttpHandler httpHandler = RouterFunctions.toHttpHandler( route(GET("/test"), request -> ServerResponse.ok().syncBody("It works!"))); this.server = new ReactorHttpServer(); this.server.setHandler(httpHandler); this.server.afterPropertiesSet(); this.server.start(); this.client = WebTestClient.bindToServer() .baseUrl("http://localhost:" + this.server.getPort()) .build(); }
@Bean public RouterFunction<ServerResponse> route(TestHandler testHandler) { return RouterFunctions .route(RequestPredicates.GET("/route") .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), testHandler::route) .andRoute(RequestPredicates.POST("/route2") .and(RequestPredicates.contentType(MediaType.APPLICATION_JSON)), testHandler::route2) .andRoute(RequestPredicates.HEAD("/route3"), request -> ServerResponse.ok().build()); }
@Test public void predicates() { testPredicate(methods(HttpMethod.GET), "GET"); testPredicate(methods(HttpMethod.GET, HttpMethod.POST), "[GET, POST]"); testPredicate(path("/foo"), "/foo"); testPredicate(pathExtension("foo"), "*.foo"); testPredicate(contentType(MediaType.APPLICATION_JSON), "Content-Type: application/json"); testPredicate(contentType(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN), "Content-Type: [application/json, text/plain]"); testPredicate(accept(MediaType.APPLICATION_JSON), "Accept: application/json"); testPredicate(queryParam("foo", "bar"), "?foo == bar"); testPredicate(method(HttpMethod.GET).and(path("/foo")), "(GET && /foo)"); testPredicate(method(HttpMethod.GET).or(path("/foo")), "(GET || /foo)"); testPredicate(method(HttpMethod.GET).negate(), "!(GET)"); testPredicate(GET("/foo") .or(contentType(MediaType.TEXT_PLAIN)) .and(accept(MediaType.APPLICATION_JSON).negate()), "(((GET && /foo) || Content-Type: text/plain) && !(Accept: application/json))"); }
@Test public void allMethods() { URI uri = URI.create("http://localhost/path"); RequestPredicate predicate = RequestPredicates.GET("/p*"); MockServerRequest request = MockServerRequest.builder().method(HttpMethod.GET).uri(uri).build(); assertTrue(predicate.test(request)); predicate = RequestPredicates.HEAD("/p*"); request = MockServerRequest.builder().method(HttpMethod.HEAD).uri(uri).build(); assertTrue(predicate.test(request)); predicate = RequestPredicates.POST("/p*"); request = MockServerRequest.builder().method(HttpMethod.POST).uri(uri).build(); assertTrue(predicate.test(request)); predicate = RequestPredicates.PUT("/p*"); request = MockServerRequest.builder().method(HttpMethod.PUT).uri(uri).build(); assertTrue(predicate.test(request)); predicate = RequestPredicates.PATCH("/p*"); request = MockServerRequest.builder().method(HttpMethod.PATCH).uri(uri).build(); assertTrue(predicate.test(request)); predicate = RequestPredicates.DELETE("/p*"); request = MockServerRequest.builder().method(HttpMethod.DELETE).uri(uri).build(); assertTrue(predicate.test(request)); predicate = RequestPredicates.OPTIONS("/p*"); request = MockServerRequest.builder().method(HttpMethod.OPTIONS).uri(uri).build(); assertTrue(predicate.test(request)); }
@Test public void httpRequestWithCustomPort() { ExchangeResult result = WebTestClient .bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> null)) .configureClient().baseUrl("http://localhost:8080").build().get() .uri("/foo").exchange().expectBody().returnResult(); OperationRequest request = this.converter.convert(result); assertThat(request.getUri()).isEqualTo(URI.create("http://localhost:8080/foo")); assertThat(request.getMethod()).isEqualTo(HttpMethod.GET); }
@Test public void httpsRequestWithCustomPort() { ExchangeResult result = WebTestClient .bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> null)) .configureClient().baseUrl("https://localhost:8443").build().get() .uri("/foo").exchange().expectBody().returnResult(); OperationRequest request = this.converter.convert(result); assertThat(request.getUri()).isEqualTo(URI.create("https://localhost:8443/foo")); assertThat(request.getMethod()).isEqualTo(HttpMethod.GET); }
@Test public void httpRequest() { ExchangeResult result = WebTestClient .bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> null)) .configureClient().baseUrl("http://localhost").build().get().uri("/foo") .exchange().expectBody().returnResult(); OperationRequest request = this.converter.convert(result); assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo")); assertThat(request.getMethod()).isEqualTo(HttpMethod.GET); }
@Test public void httpsRequest() { ExchangeResult result = WebTestClient .bindToRouterFunction(RouterFunctions.route(GET("/foo"), (req) -> null)) .configureClient().baseUrl("https://localhost").build().get().uri("/foo") .exchange().expectBody().returnResult(); OperationRequest request = this.converter.convert(result); assertThat(request.getUri()).isEqualTo(URI.create("https://localhost/foo")); assertThat(request.getMethod()).isEqualTo(HttpMethod.GET); }