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.
This commit is contained in:
Arjen Poutsma 2017-02-15 10:57:21 +01:00
parent 7dd0f358ed
commit 36db6b2753
4 changed files with 23 additions and 124 deletions

View File

@ -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 {

View File

@ -133,18 +133,6 @@ class RouterDsl {
routes += RouterFunctions.route(RequestPredicates.queryParam(name, predicate), HandlerFunction { f(it) })
}
fun json(f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.json(), HandlerFunction { f(it) })
}
fun html(f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.html(), HandlerFunction { f(it) })
}
fun xml(f: (ServerRequest) -> Mono<ServerResponse>) {
routes += RouterFunctions.route(RequestPredicates.xml(), HandlerFunction { f(it) })
}
fun resources(path: String, location: Resource) {
routes += RouterFunctions.resources(path, location)
}

View File

@ -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));
}
}

View File

@ -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) }