Skip to content

Commit a99fe3e

Browse files
committed
Polish HttpHiddenMethodFilter
1 parent 37592ea commit a99fe3e

File tree

4 files changed

+87
-115
lines changed

4 files changed

+87
-115
lines changed

spring-web/src/main/java/org/springframework/web/filter/reactive/HiddenHttpMethodFilter.java

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,33 @@
3737
* return value using {@link ServerWebExchange#mutate()}.
3838
*
3939
* <p>The name of the request parameter defaults to {@code _method}, but can be
40-
* adapted via the {@link #setMethodParam(String) methodParam} property.
40+
* adapted via the {@link #setMethodParamName(String) methodParamName} property.
4141
*
4242
* @author Greg Turnquist
43+
* @author Rossen Stoyanchev
4344
* @since 5.0
4445
*/
4546
public class HiddenHttpMethodFilter implements WebFilter {
4647

47-
/** Default method parameter: {@code _method} */
48-
public static final String DEFAULT_METHOD_PARAM = "_method";
48+
/** Default name of the form parameter with the HTTP method to use */
49+
public static final String DEFAULT_METHOD_PARAMETER_NAME = "_method";
50+
51+
52+
private String methodParamName = DEFAULT_METHOD_PARAMETER_NAME;
4953

50-
private String methodParam = DEFAULT_METHOD_PARAM;
5154

5255
/**
53-
* Set the parameter name to look for HTTP methods.
54-
* @see #DEFAULT_METHOD_PARAM
56+
* Set the name of the form parameter with the HTTP method to use.
57+
* <p>By default this is set to {@code "_method"}.
5558
*/
56-
public void setMethodParam(String methodParam) {
57-
Assert.hasText(methodParam, "'methodParam' must not be empty");
58-
this.methodParam = methodParam;
59+
public void setMethodParamName(String methodParamName) {
60+
Assert.hasText(methodParamName, "'methodParamName' must not be empty");
61+
this.methodParamName = methodParamName;
5962
}
6063

64+
6165
/**
62-
* Transform an HTTP POST into another method based on {@code methodParam}
66+
* Transform an HTTP POST into another method based on {@code methodParamName}
6367
*
6468
* @param exchange the current server exchange
6569
* @param chain provides a way to delegate to the next filter
@@ -68,36 +72,22 @@ public void setMethodParam(String methodParam) {
6872
@Override
6973
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
7074

71-
if (exchange.getRequest().getMethod() == HttpMethod.POST) {
72-
return exchange.getFormData()
73-
.map(formData -> {
74-
String method = formData.getFirst(methodParam);
75-
if (StringUtils.hasLength(method)) {
76-
return convertedRequest(exchange, method);
77-
}
78-
else {
79-
return exchange;
80-
}
81-
})
82-
.then(convertedExchange -> chain.filter(convertedExchange));
83-
}
84-
else {
75+
if (exchange.getRequest().getMethod() != HttpMethod.POST) {
8576
return chain.filter(exchange);
8677
}
78+
79+
return exchange.getFormData()
80+
.map(formData -> {
81+
String method = formData.getFirst(this.methodParamName);
82+
return StringUtils.hasLength(method) ? mapExchange(exchange, method) : exchange;
83+
})
84+
.then((exchange1) -> chain.filter(exchange1));
8785
}
8886

89-
/**
90-
* Mutate exchange into a new HTTP request method.
91-
*
92-
* @param exchange original {@link ServerWebExchange}
93-
* @param method request HTTP method based on form data
94-
* @return a mutated {@link ServerWebExchange}
95-
*/
96-
private ServerWebExchange convertedRequest(ServerWebExchange exchange, String method) {
97-
HttpMethod resolved = HttpMethod.resolve(method.toUpperCase(Locale.ENGLISH));
98-
Assert.notNull(resolved, () -> "HttpMethod '" + method + "' is not supported");
99-
return exchange.mutate()
100-
.request(builder -> builder.method(resolved))
101-
.build();
87+
private ServerWebExchange mapExchange(ServerWebExchange exchange, String methodParamValue) {
88+
HttpMethod httpMethod = HttpMethod.resolve(methodParamValue.toUpperCase(Locale.ENGLISH));
89+
Assert.notNull(httpMethod, () -> "HttpMethod '" + methodParamValue + "' not supported");
90+
return exchange.mutate().request(builder -> builder.method(httpMethod)).build();
10291
}
92+
10393
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* {@link org.springframework.web.server.WebFilter} implementations for use in
3+
* reactive web applications.
4+
*/
5+
package org.springframework.web.filter.reactive;

spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,10 @@ private static Mono<MultiValueMap<String, String>> initFormData(ServerHttpReques
104104
try {
105105
contentType = request.getHeaders().getContentType();
106106
if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(contentType)) {
107-
Map<String, Object> hints = Collections.emptyMap();
108-
return FORM_READER.readMono(FORM_DATA_VALUE_TYPE, request, hints).cache();
107+
return FORM_READER
108+
.readMono(FORM_DATA_VALUE_TYPE, request, Collections.emptyMap())
109+
.otherwiseIfEmpty(EMPTY_FORM_DATA)
110+
.cache();
109111
}
110112
}
111113
catch (InvalidMediaTypeException ex) {

spring-web/src/test/java/org/springframework/web/filter/reactive/HiddenHttpMethodFilterTests.java

Lines changed: 51 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.web.filter.reactive;
1818

19-
import java.util.Optional;
19+
import java.time.Duration;
2020

2121
import org.hamcrest.Matchers;
2222
import org.junit.Test;
@@ -27,123 +27,98 @@
2727
import org.springframework.http.HttpMethod;
2828
import org.springframework.http.MediaType;
2929
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
30+
import org.springframework.mock.http.server.reactive.test.MockServerWebExchange;
3031
import org.springframework.web.server.ServerWebExchange;
3132
import org.springframework.web.server.WebFilterChain;
3233

3334
import static org.junit.Assert.assertEquals;
3435
import static org.junit.Assert.assertThat;
3536

3637
/**
37-
* Tests for {@link HiddenHttpMethodFilter}
38-
*
38+
* Tests for {@link HiddenHttpMethodFilter}.
3939
* @author Greg Turnquist
40+
* @author Rossen Stoyanchev
4041
*/
4142
public class HiddenHttpMethodFilterTests {
4243

4344
private final HiddenHttpMethodFilter filter = new HiddenHttpMethodFilter();
4445

46+
private final TestWebFilterChain filterChain = new TestWebFilterChain();
47+
48+
4549
@Test
4650
public void filterWithParameter() {
47-
ServerWebExchange mockExchange = createExchange(Optional.of("DELETE"));
48-
49-
WebFilterChain filterChain = exchange -> {
50-
assertEquals("Invalid method", HttpMethod.DELETE, exchange.getRequest().getMethod());
51-
return Mono.empty();
52-
};
51+
postForm("_method=DELETE").block(Duration.ZERO);
52+
assertEquals(HttpMethod.DELETE, this.filterChain.getHttpMethod());
53+
}
5354

54-
StepVerifier.create(filter.filter(mockExchange, filterChain))
55-
.expectComplete()
56-
.verify();
55+
@Test
56+
public void filterWithNoParameter() {
57+
postForm("").block(Duration.ZERO);
58+
assertEquals(HttpMethod.POST, this.filterChain.getHttpMethod());
5759
}
5860

5961
@Test
60-
public void filterWithInvalidParameter() {
61-
ServerWebExchange mockExchange = createExchange(Optional.of("INVALID"));
62+
public void filterWithEmptyStringParameter() {
63+
postForm("_method=").block(Duration.ZERO);
64+
assertEquals(HttpMethod.POST, this.filterChain.getHttpMethod());
65+
}
6266

63-
WebFilterChain filterChain = exchange -> Mono.empty();
67+
@Test
68+
public void filterWithDifferentMethodParam() {
69+
this.filter.setMethodParamName("_foo");
70+
postForm("_foo=DELETE").block(Duration.ZERO);
71+
assertEquals(HttpMethod.DELETE, this.filterChain.getHttpMethod());
72+
}
6473

65-
StepVerifier.create(filter.filter(mockExchange, filterChain))
74+
@Test
75+
public void filterWithInvalidMethodValue() {
76+
StepVerifier.create(postForm("_method=INVALID"))
6677
.consumeErrorWith(error -> {
6778
assertThat(error, Matchers.instanceOf(IllegalArgumentException.class));
68-
assertEquals(error.getMessage(), "HttpMethod 'INVALID' is not supported");
79+
assertEquals(error.getMessage(), "HttpMethod 'INVALID' not supported");
6980
})
7081
.verify();
7182
}
7283

7384
@Test
74-
public void filterWithNoParameter() {
75-
ServerWebExchange mockExchange = createExchange(Optional.empty());
85+
public void filterWithHttpPut() {
7686

77-
WebFilterChain filterChain = exchange -> {
78-
assertEquals("Invalid method", HttpMethod.POST, exchange.getRequest().getMethod());
79-
return Mono.empty();
80-
};
87+
ServerWebExchange exchange = MockServerHttpRequest.put("/")
88+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
89+
.body("_method=DELETE")
90+
.toExchange();
8191

82-
StepVerifier.create(filter.filter(mockExchange, filterChain))
83-
.expectComplete()
84-
.verify();
92+
this.filter.filter(exchange, this.filterChain).block(Duration.ZERO);
93+
assertEquals(HttpMethod.PUT, this.filterChain.getHttpMethod());
8594
}
8695

87-
@Test
88-
public void filterWithEmptyStringParameter() {
89-
ServerWebExchange mockExchange = createExchange(Optional.of(""));
90-
91-
WebFilterChain filterChain = exchange -> {
92-
assertEquals("Invalid method", HttpMethod.POST, exchange.getRequest().getMethod());
93-
return Mono.empty();
94-
};
95-
96-
StepVerifier.create(filter.filter(mockExchange, filterChain))
97-
.expectComplete()
98-
.verify();
99-
}
100-
101-
@Test
102-
public void filterWithDifferentMethodParam() {
103-
ServerWebExchange mockExchange = createExchange("_foo", Optional.of("DELETE"));
10496

105-
WebFilterChain filterChain = exchange -> {
106-
assertEquals("Invalid method", HttpMethod.DELETE, exchange.getRequest().getMethod());
107-
return Mono.empty();
108-
};
97+
private Mono<Void> postForm(String body) {
10998

110-
filter.setMethodParam("_foo");
99+
MockServerWebExchange exchange = MockServerHttpRequest.post("/")
100+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
101+
.body(body)
102+
.toExchange();
111103

112-
StepVerifier.create(filter.filter(mockExchange, filterChain))
113-
.expectComplete()
114-
.verify();
104+
return this.filter.filter(exchange, this.filterChain);
115105
}
116106

117-
@Test
118-
public void filterWithoutPost() {
119-
ServerWebExchange mockExchange = createExchange(Optional.of("DELETE")).mutate()
120-
.request(builder -> builder.method(HttpMethod.PUT))
121-
.build();
122107

123-
WebFilterChain filterChain = exchange -> {
124-
assertEquals("Invalid method", HttpMethod.PUT, exchange.getRequest().getMethod());
125-
return Mono.empty();
126-
};
108+
private static class TestWebFilterChain implements WebFilterChain {
127109

128-
StepVerifier.create(filter.filter(mockExchange, filterChain))
129-
.expectComplete()
130-
.verify();
131-
}
132-
133-
private ServerWebExchange createExchange(Optional<String> optionalMethod) {
134-
return createExchange("_method", optionalMethod);
135-
}
110+
private HttpMethod httpMethod;
136111

137-
private ServerWebExchange createExchange(String methodName, Optional<String> optionalBody) {
138-
MockServerHttpRequest.BodyBuilder builder = MockServerHttpRequest
139-
.post("/hotels")
140-
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
141112

142-
MockServerHttpRequest request = optionalBody
143-
.map(method -> builder.body(methodName + "=" + method))
144-
.orElse(builder.build());
113+
public HttpMethod getHttpMethod() {
114+
return this.httpMethod;
115+
}
145116

146-
return request.toExchange();
117+
@Override
118+
public Mono<Void> filter(ServerWebExchange exchange) {
119+
this.httpMethod = exchange.getRequest().getMethod();
120+
return Mono.empty();
121+
}
147122
}
148123

149124
}

0 commit comments

Comments
 (0)