Added RequestPredicates.methods(HttpMethod...)
Added a predicate that tests for multiple HTTP methods.
This commit is contained in:
@@ -22,6 +22,7 @@ import java.security.Principal;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.EnumSet;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -79,14 +80,26 @@ public abstract class RequestPredicates {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a {@code RequestPredicate} that tests against the given HTTP method.
|
* Return a {@code RequestPredicate} that matches if the request's HTTP method is equal to the
|
||||||
* @param httpMethod the HTTP method to match to
|
* given method
|
||||||
|
* @param httpMethod the HTTP method to match against
|
||||||
* @return a predicate that tests against the given HTTP method
|
* @return a predicate that tests against the given HTTP method
|
||||||
*/
|
*/
|
||||||
public static RequestPredicate method(HttpMethod httpMethod) {
|
public static RequestPredicate method(HttpMethod httpMethod) {
|
||||||
return new HttpMethodPredicate(httpMethod);
|
return new HttpMethodPredicate(httpMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a {@code RequestPredicate} that matches if the request's HTTP method is equal to one
|
||||||
|
* the of the given methods.
|
||||||
|
* @param httpMethods the HTTP methods to match against
|
||||||
|
* @return a predicate that tests against the given HTTP methods
|
||||||
|
* @since 5.1
|
||||||
|
*/
|
||||||
|
public static RequestPredicate methods(HttpMethod... httpMethods) {
|
||||||
|
return new HttpMethodPredicate(httpMethods);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a {@code RequestPredicate} that tests the request path against the given path pattern.
|
* Return a {@code RequestPredicate} that tests the request path against the given path pattern.
|
||||||
* @param pattern the pattern to match to
|
* @param pattern the pattern to match to
|
||||||
@@ -336,23 +349,34 @@ public abstract class RequestPredicates {
|
|||||||
|
|
||||||
private static class HttpMethodPredicate implements RequestPredicate {
|
private static class HttpMethodPredicate implements RequestPredicate {
|
||||||
|
|
||||||
private final HttpMethod httpMethod;
|
private final Set<HttpMethod> httpMethods;
|
||||||
|
|
||||||
public HttpMethodPredicate(HttpMethod httpMethod) {
|
public HttpMethodPredicate(HttpMethod httpMethod) {
|
||||||
Assert.notNull(httpMethod, "HttpMethod must not be null");
|
Assert.notNull(httpMethod, "HttpMethod must not be null");
|
||||||
this.httpMethod = httpMethod;
|
this.httpMethods = EnumSet.of(httpMethod);
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpMethodPredicate(HttpMethod... httpMethods) {
|
||||||
|
Assert.notEmpty(httpMethods, "HttpMethods must not be empty");
|
||||||
|
|
||||||
|
this.httpMethods = EnumSet.copyOf(Arrays.asList(httpMethods));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean test(ServerRequest request) {
|
public boolean test(ServerRequest request) {
|
||||||
boolean match = this.httpMethod == request.method();
|
boolean match = this.httpMethods.contains(request.method());
|
||||||
traceMatch("Method", this.httpMethod, request.method(), match);
|
traceMatch("Method", this.httpMethods, request.method(), match);
|
||||||
return match;
|
return match;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return this.httpMethod.toString();
|
if (this.httpMethods.size() == 1) {
|
||||||
|
return this.httpMethods.iterator().next().toString();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return this.httpMethods.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2017 the original author or authors.
|
* Copyright 2002-2018 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -53,6 +53,19 @@ public class RequestPredicatesTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void methods() {
|
public void methods() {
|
||||||
|
RequestPredicate predicate = RequestPredicates.methods(HttpMethod.GET, HttpMethod.HEAD);
|
||||||
|
MockServerRequest request = MockServerRequest.builder().method(HttpMethod.GET).build();
|
||||||
|
assertTrue(predicate.test(request));
|
||||||
|
|
||||||
|
request = MockServerRequest.builder().method(HttpMethod.HEAD).build();
|
||||||
|
assertTrue(predicate.test(request));
|
||||||
|
|
||||||
|
request = MockServerRequest.builder().method(HttpMethod.POST).build();
|
||||||
|
assertFalse(predicate.test(request));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void allMethods() {
|
||||||
URI uri = URI.create("http://localhost/path");
|
URI uri = URI.create("http://localhost/path");
|
||||||
|
|
||||||
RequestPredicate predicate = RequestPredicates.GET("/p*");
|
RequestPredicate predicate = RequestPredicates.GET("/p*");
|
||||||
|
|||||||
Reference in New Issue
Block a user