Fix regression in WebFlux support for WebDAV methods
This commit ensures that WebFlux's RequestMethodsRequestCondition supports HTTP methods that are not in the RequestMethod enum. - RequestMethod::resolve is introduced, to convert from a HttpMethod (name) to enum values. - RequestMethod::asHttpMethod is introduced, to convert from enum value to HttpMethod. - HttpMethod::valueOf replaced Map-based lookup to a switch statement - Enabled tests that check for WebDAV methods See gh-27697 Closes gh-29981
This commit is contained in:
@@ -46,9 +46,9 @@ public final class RequestMethodsRequestCondition extends AbstractRequestConditi
|
||||
|
||||
static {
|
||||
requestMethodConditionCache = CollectionUtils.newHashMap(RequestMethod.values().length);
|
||||
for (RequestMethod method : RequestMethod.values()) {
|
||||
requestMethodConditionCache.put(
|
||||
HttpMethod.valueOf(method.name()), new RequestMethodsRequestCondition(method));
|
||||
for (RequestMethod requestMethod : RequestMethod.values()) {
|
||||
requestMethodConditionCache.put(requestMethod.asHttpMethod(),
|
||||
new RequestMethodsRequestCondition(requestMethod));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,16 +150,15 @@ public final class RequestMethodsRequestCondition extends AbstractRequestConditi
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private RequestMethodsRequestCondition matchRequestMethod(@Nullable HttpMethod httpMethod) {
|
||||
if (httpMethod == null) {
|
||||
return null;
|
||||
}
|
||||
RequestMethod requestMethod = RequestMethod.valueOf(httpMethod.name());
|
||||
if (getMethods().contains(requestMethod)) {
|
||||
return requestMethodConditionCache.get(httpMethod);
|
||||
}
|
||||
if (requestMethod.equals(RequestMethod.HEAD) && getMethods().contains(RequestMethod.GET)) {
|
||||
return requestMethodConditionCache.get(HttpMethod.GET);
|
||||
private RequestMethodsRequestCondition matchRequestMethod(HttpMethod httpMethod) {
|
||||
RequestMethod requestMethod = RequestMethod.resolve(httpMethod);
|
||||
if (requestMethod != null) {
|
||||
if (getMethods().contains(requestMethod)) {
|
||||
return requestMethodConditionCache.get(httpMethod);
|
||||
}
|
||||
if (requestMethod.equals(RequestMethod.HEAD) && getMethods().contains(RequestMethod.GET)) {
|
||||
return requestMethodConditionCache.get(HttpMethod.GET);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ package org.springframework.web.reactive.result.condition;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@@ -44,8 +43,6 @@ import static org.springframework.web.bind.annotation.RequestMethod.PUT;
|
||||
*/
|
||||
public class RequestMethodsRequestConditionTests {
|
||||
|
||||
// TODO: custom method, CORS pre-flight (see @Disabled)
|
||||
|
||||
@Test
|
||||
public void getMatchingCondition() throws Exception {
|
||||
testMatch(new RequestMethodsRequestCondition(GET), GET);
|
||||
@@ -73,7 +70,6 @@ public class RequestMethodsRequestConditionTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void getMatchingConditionWithCustomMethod() throws Exception {
|
||||
ServerWebExchange exchange = getExchange("PROPFIND");
|
||||
assertThat(new RequestMethodsRequestCondition().getMatchingCondition(exchange)).isNotNull();
|
||||
@@ -81,11 +77,12 @@ public class RequestMethodsRequestConditionTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void getMatchingConditionWithCorsPreFlight() throws Exception {
|
||||
ServerWebExchange exchange = getExchange("OPTIONS");
|
||||
exchange.getRequest().getHeaders().add("Origin", "https://example.com");
|
||||
exchange.getRequest().getHeaders().add(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "PUT");
|
||||
public void getMatchingConditionWithCorsPreFlight() {
|
||||
MockServerHttpRequest request = MockServerHttpRequest.method(HttpMethod.valueOf("OPTIONS"), "/")
|
||||
.header("Origin", "https://example.com")
|
||||
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "PUT")
|
||||
.build();
|
||||
ServerWebExchange exchange = MockServerWebExchange.from(request);
|
||||
|
||||
assertThat(new RequestMethodsRequestCondition().getMatchingCondition(exchange)).isNotNull();
|
||||
assertThat(new RequestMethodsRequestCondition(PUT).getMatchingCondition(exchange)).isNotNull();
|
||||
|
||||
Reference in New Issue
Block a user