From 36db6b2753c829b2a7893d98a7c8c7f8809d328e Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 15 Feb 2017 10:57:21 +0100 Subject: [PATCH] Removed json(), html() and xml() predicates Removed `json()`, `html()` and `xml()` from `RequestPredicates`, since they were confusingly named and would also require a counterpart that reads the request `Content-Type` instead of the `Accept` header. --- .../function/server/RequestPredicates.java | 44 +----------- .../server/RouterFunctionExtensions.kt | 12 ---- .../server/RequestPredicatesTests.java | 72 ++----------------- .../server/RouterFunctionExtensionsTests.kt | 19 ++++- 4 files changed, 23 insertions(+), 124 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java index ced3a43365..4f4c36ac8b 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java @@ -233,7 +233,7 @@ public abstract class RequestPredicates { /** * Return a {@code RequestPredicate} that matches if the request's path has the given extension. - * @param extension the path extension to match against + * @param extension the path extension to match against, ignoring case * @return a predicate that matches if the request's path has the given file extension */ public static RequestPredicate pathExtension(String extension) { @@ -272,48 +272,6 @@ public abstract class RequestPredicates { }; } - /** - * Return a {@code RequestPredicate} that matches JSON requests. The returned predicate - * matches if the request has {@code application/json} in the {@code Accept} header, or if the - * request path has a {@code .json} file extension. - * - * @return a predicate that matches JSON - * @see #accept(MediaType...) - * @see #pathExtension(String) - */ - public static RequestPredicate json() { - return accept(MediaType.APPLICATION_JSON) - .or(pathExtension("json")); - } - - /** - * Return a {@code RequestPredicate} that matches HTML requests. The returned predicate - * matches if the request has {@code text/html} in the {@code Accept} header, or if the request - * path has a {@code .html} file extension. - * - * @return a predicate that matches HTML requests - * @see #accept(MediaType...) - * @see #pathExtension(String) - */ - public static RequestPredicate html() { - return accept(MediaType.TEXT_HTML) - .or(pathExtension("html")); - } - - /** - * Return a {@code RequestPredicate} that matches XML requests. The returned predicate - * matches if the request has {@code text/xml} or {@code application/xml} in the {@code Accept} - * header, or if the request path has a {@code .xml} file extension. - * - * @return a predicate that matches XML requests - * @see #accept(MediaType...) - * @see #pathExtension(String) - */ - public static RequestPredicate xml() { - return accept(MediaType.TEXT_XML) - .or(accept(MediaType.APPLICATION_XML)) - .or(pathExtension("xml")); - } private static class HttpMethodPredicate implements RequestPredicate { diff --git a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt index d84ec84f64..d2882b83fb 100644 --- a/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt +++ b/spring-webflux/src/main/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensions.kt @@ -133,18 +133,6 @@ class RouterDsl { routes += RouterFunctions.route(RequestPredicates.queryParam(name, predicate), HandlerFunction { f(it) }) } - fun json(f: (ServerRequest) -> Mono) { - routes += RouterFunctions.route(RequestPredicates.json(), HandlerFunction { f(it) }) - } - - fun html(f: (ServerRequest) -> Mono) { - routes += RouterFunctions.route(RequestPredicates.html(), HandlerFunction { f(it) }) - } - - fun xml(f: (ServerRequest) -> Mono) { - routes += RouterFunctions.route(RequestPredicates.xml(), HandlerFunction { f(it) }) - } - fun resources(path: String, location: Resource) { routes += RouterFunctions.resources(path, location) } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java index 2ca8f033f1..2b1dec7498 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java @@ -146,11 +146,16 @@ public class RequestPredicatesTests { @Test public void pathExtension() throws Exception { - URI uri = URI.create("http://localhost/file.txt"); RequestPredicate predicate = RequestPredicates.pathExtension("txt"); + + URI uri = URI.create("http://localhost/file.txt"); MockServerRequest request = MockServerRequest.builder().uri(uri).build(); assertTrue(predicate.test(request)); + uri = URI.create("http://localhost/FILE.TXT"); + request = MockServerRequest.builder().uri(uri).build(); + assertTrue(predicate.test(request)); + predicate = RequestPredicates.pathExtension("bar"); assertFalse(predicate.test(request)); @@ -170,70 +175,5 @@ public class RequestPredicatesTests { assertFalse(predicate.test(request)); } - @Test - public void json() throws Exception { - RequestPredicate predicate = RequestPredicates.json(); - MockServerRequest request = MockServerRequest.builder().header("Accept", MediaType.APPLICATION_JSON.toString()).build(); - assertTrue(predicate.test(request)); - - request = MockServerRequest.builder().header("Accept", MediaType.TEXT_HTML.toString()).build(); - assertFalse(predicate.test(request)); - - URI uri = URI.create("http://localhost/file.json"); - request = MockServerRequest.builder().uri(uri).build(); - assertTrue(predicate.test(request)); - - uri = URI.create("http://localhost/file.html"); - request = MockServerRequest.builder().uri(uri).build(); - assertFalse(predicate.test(request)); - - request = MockServerRequest.builder().build(); - assertFalse(predicate.test(request)); - } - - @Test - public void html() throws Exception { - RequestPredicate predicate = RequestPredicates.html(); - MockServerRequest request = MockServerRequest.builder().header("Accept", MediaType.TEXT_HTML.toString()).build(); - assertTrue(predicate.test(request)); - - request = MockServerRequest.builder().header("Accept", MediaType.APPLICATION_JSON.toString()).build(); - assertFalse(predicate.test(request)); - - URI uri = URI.create("http://localhost/file.html"); - request = MockServerRequest.builder().uri(uri).build(); - assertTrue(predicate.test(request)); - - uri = URI.create("http://localhost/file.json"); - request = MockServerRequest.builder().uri(uri).build(); - assertFalse(predicate.test(request)); - - request = MockServerRequest.builder().build(); - assertFalse(predicate.test(request)); - } - - @Test - public void xml() throws Exception { - RequestPredicate predicate = RequestPredicates.xml(); - MockServerRequest request = MockServerRequest.builder().header("Accept", MediaType.TEXT_XML.toString()).build(); - assertTrue(predicate.test(request)); - - request = MockServerRequest.builder().header("Accept", MediaType.APPLICATION_XML.toString()).build(); - assertTrue(predicate.test(request)); - - request = MockServerRequest.builder().header("Accept", MediaType.TEXT_HTML.toString()).build(); - assertFalse(predicate.test(request)); - - URI uri = URI.create("http://localhost/file.xml"); - request = MockServerRequest.builder().uri(uri).build(); - assertTrue(predicate.test(request)); - - uri = URI.create("http://localhost/file.json"); - request = MockServerRequest.builder().uri(uri).build(); - assertFalse(predicate.test(request)); - - request = MockServerRequest.builder().build(); - assertFalse(predicate.test(request)); - } } \ No newline at end of file diff --git a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt index f76faa40b3..a71045a611 100644 --- a/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt +++ b/spring-webflux/src/test/kotlin/org/springframework/web/reactive/function/server/RouterFunctionExtensionsTests.kt @@ -1,3 +1,19 @@ +/* + * Copyright 2002-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.web.reactive.function.server import org.junit.Test @@ -101,9 +117,6 @@ class RouterFunctionExtensionsTests { PUT("/api/foo/") { handleFromClass(req) } DELETE("/api/foo/") { handleFromClass(req) } } - html().apply { - GET("/page") { handleFromClass(req) } - } accept(APPLICATION_ATOM_XML, ::handle) contentType(APPLICATION_OCTET_STREAM) { handle(req) } method(HttpMethod.PATCH) { handle(req) }