Skip to content

Commit 490302e

Browse files
committed
Added RequestPredicates.methods(HttpMethod...)
Added a predicate that tests for multiple HTTP methods.
1 parent 91e96d8 commit 490302e

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.ArrayList;
2323
import java.util.Arrays;
2424
import java.util.Collections;
25+
import java.util.EnumSet;
2526
import java.util.HashSet;
2627
import java.util.LinkedHashMap;
2728
import java.util.List;
@@ -79,14 +80,26 @@ public static RequestPredicate all() {
7980

8081

8182
/**
82-
* Return a {@code RequestPredicate} that tests against the given HTTP method.
83-
* @param httpMethod the HTTP method to match to
83+
* Return a {@code RequestPredicate} that matches if the request's HTTP method is equal to the
84+
* given method
85+
* @param httpMethod the HTTP method to match against
8486
* @return a predicate that tests against the given HTTP method
8587
*/
8688
public static RequestPredicate method(HttpMethod httpMethod) {
8789
return new HttpMethodPredicate(httpMethod);
8890
}
8991

92+
/**
93+
* Return a {@code RequestPredicate} that matches if the request's HTTP method is equal to one
94+
* the of the given methods.
95+
* @param httpMethods the HTTP methods to match against
96+
* @return a predicate that tests against the given HTTP methods
97+
* @since 5.1
98+
*/
99+
public static RequestPredicate methods(HttpMethod... httpMethods) {
100+
return new HttpMethodPredicate(httpMethods);
101+
}
102+
90103
/**
91104
* Return a {@code RequestPredicate} that tests the request path against the given path pattern.
92105
* @param pattern the pattern to match to
@@ -336,23 +349,34 @@ private static void traceMatch(String prefix, Object desired, @Nullable Object a
336349

337350
private static class HttpMethodPredicate implements RequestPredicate {
338351

339-
private final HttpMethod httpMethod;
352+
private final Set<HttpMethod> httpMethods;
340353

341354
public HttpMethodPredicate(HttpMethod httpMethod) {
342355
Assert.notNull(httpMethod, "HttpMethod must not be null");
343-
this.httpMethod = httpMethod;
356+
this.httpMethods = EnumSet.of(httpMethod);
357+
}
358+
359+
public HttpMethodPredicate(HttpMethod... httpMethods) {
360+
Assert.notEmpty(httpMethods, "HttpMethods must not be empty");
361+
362+
this.httpMethods = EnumSet.copyOf(Arrays.asList(httpMethods));
344363
}
345364

346365
@Override
347366
public boolean test(ServerRequest request) {
348-
boolean match = this.httpMethod == request.method();
349-
traceMatch("Method", this.httpMethod, request.method(), match);
367+
boolean match = this.httpMethods.contains(request.method());
368+
traceMatch("Method", this.httpMethods, request.method(), match);
350369
return match;
351370
}
352371

353372
@Override
354373
public String toString() {
355-
return this.httpMethod.toString();
374+
if (this.httpMethods.size() == 1) {
375+
return this.httpMethods.iterator().next().toString();
376+
}
377+
else {
378+
return this.httpMethods.toString();
379+
}
356380
}
357381
}
358382

spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,6 +53,19 @@ public void method() {
5353

5454
@Test
5555
public void methods() {
56+
RequestPredicate predicate = RequestPredicates.methods(HttpMethod.GET, HttpMethod.HEAD);
57+
MockServerRequest request = MockServerRequest.builder().method(HttpMethod.GET).build();
58+
assertTrue(predicate.test(request));
59+
60+
request = MockServerRequest.builder().method(HttpMethod.HEAD).build();
61+
assertTrue(predicate.test(request));
62+
63+
request = MockServerRequest.builder().method(HttpMethod.POST).build();
64+
assertFalse(predicate.test(request));
65+
}
66+
67+
@Test
68+
public void allMethods() {
5669
URI uri = URI.create("http://localhost/path");
5770

5871
RequestPredicate predicate = RequestPredicates.GET("/p*");

0 commit comments

Comments
 (0)