Polish
Issue: SPR-15206
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2017 the original author or authors.
|
* Copyright 2002-2017 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.
|
||||||
@@ -13,26 +13,28 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.web.filter.reactive;
|
package org.springframework.web.filter.reactive;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
import org.springframework.web.server.WebFilter;
|
import org.springframework.web.server.WebFilter;
|
||||||
import org.springframework.web.server.WebFilterChain;
|
import org.springframework.web.server.WebFilterChain;
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
|
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reactive {@link WebFilter} that converts posted method parameters into HTTP methods,
|
* Reactive {@link WebFilter} that converts posted method parameters into HTTP methods,
|
||||||
* retrievable via {@link ServerHttpRequest#getMethod()}. Since browsers currently only
|
* retrievable via {@link ServerHttpRequest#getMethod()}. Since browsers currently only
|
||||||
* support GET and POST, a common technique - used by the Prototype library, for instance -
|
* support GET and POST, a common technique is to use a normal POST with an additional
|
||||||
* is to use a normal POST with an additional hidden form field ({@code _method})
|
* hidden form field ({@code _method}) to pass the "real" HTTP method along.
|
||||||
* to pass the "real" HTTP method along. This filter reads that parameter and changes
|
* This filter reads that parameter and changes the {@link ServerHttpRequest#getMethod()}
|
||||||
* the {@link ServerHttpRequest#getMethod()} return value using {@link ServerWebExchange#mutate()}.
|
* return value using {@link ServerWebExchange#mutate()}.
|
||||||
*
|
*
|
||||||
* <p>The name of the request parameter defaults to {@code _method}, but can be
|
* <p>The name of the request parameter defaults to {@code _method}, but can be
|
||||||
* adapted via the {@link #setMethodParam(String) methodParam} property.
|
* adapted via the {@link #setMethodParam(String) methodParam} property.
|
||||||
@@ -61,37 +63,41 @@ public class HiddenHttpMethodFilter implements WebFilter {
|
|||||||
*
|
*
|
||||||
* @param exchange the current server exchange
|
* @param exchange the current server exchange
|
||||||
* @param chain provides a way to delegate to the next filter
|
* @param chain provides a way to delegate to the next filter
|
||||||
* @return
|
* @return {@code Mono<Void>} to indicate when request processing is complete
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||||
|
|
||||||
if (exchange.getRequest().getMethod() == HttpMethod.POST) {
|
if (exchange.getRequest().getMethod() == HttpMethod.POST) {
|
||||||
return exchange.getFormData()
|
return exchange.getFormData()
|
||||||
.map(map -> Optional.ofNullable(map.getFirst(methodParam)))
|
.map(formData -> {
|
||||||
.map(method -> convertedRequest(exchange, method))
|
String method = formData.getFirst(methodParam);
|
||||||
.then(convertedExchange -> chain.filter(convertedExchange));
|
if (StringUtils.hasLength(method)) {
|
||||||
} else {
|
return convertedRequest(exchange, method);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return exchange;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(convertedExchange -> chain.filter(convertedExchange));
|
||||||
|
}
|
||||||
|
else {
|
||||||
return chain.filter(exchange);
|
return chain.filter(exchange);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mutate exchange into a new HTTP method.
|
* Mutate exchange into a new HTTP request method.
|
||||||
*
|
*
|
||||||
* @param exchange - original request
|
* @param exchange original {@link ServerWebExchange}
|
||||||
* @param method - request HTTP method based on form data
|
* @param method request HTTP method based on form data
|
||||||
* @return a mutated {@link ServerWebExchange}
|
* @return a mutated {@link ServerWebExchange}
|
||||||
*/
|
*/
|
||||||
private ServerWebExchange convertedRequest(ServerWebExchange exchange, Optional<String> method) {
|
private ServerWebExchange convertedRequest(ServerWebExchange exchange, String method) {
|
||||||
|
HttpMethod resolved = HttpMethod.resolve(method.toUpperCase(Locale.ENGLISH));
|
||||||
String upperMethod = method
|
Assert.notNull(resolved, () -> "HttpMethod '" + method + "' is not supported");
|
||||||
.map(String::toString)
|
|
||||||
.orElse(HttpMethod.POST.toString())
|
|
||||||
.toUpperCase(Locale.ENGLISH);
|
|
||||||
|
|
||||||
return exchange.mutate()
|
return exchange.mutate()
|
||||||
.request(builder -> builder.method(HttpMethod.resolve(upperMethod)))
|
.request(builder -> builder.method(resolved))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2017 the original author or authors.
|
* Copyright 2002-2017 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.
|
||||||
@@ -13,9 +13,16 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.web.filter.reactive;
|
package org.springframework.web.filter.reactive;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.hamcrest.Matchers;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
import reactor.test.StepVerifier;
|
||||||
|
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
@@ -24,14 +31,13 @@ import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse
|
|||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
import org.springframework.web.server.WebFilterChain;
|
import org.springframework.web.server.WebFilterChain;
|
||||||
import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
||||||
import reactor.core.publisher.Mono;
|
|
||||||
import reactor.test.StepVerifier;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Tests for {@link HiddenHttpMethodFilter}
|
||||||
|
*
|
||||||
* @author Greg Turnquist
|
* @author Greg Turnquist
|
||||||
*/
|
*/
|
||||||
public class HiddenHttpMethodFilterTests {
|
public class HiddenHttpMethodFilterTests {
|
||||||
@@ -48,8 +54,22 @@ public class HiddenHttpMethodFilterTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
StepVerifier.create(filter.filter(mockExchange, filterChain))
|
StepVerifier.create(filter.filter(mockExchange, filterChain))
|
||||||
.expectComplete()
|
.expectComplete()
|
||||||
.verify();
|
.verify();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void filterWithInvalidParameter() {
|
||||||
|
ServerWebExchange mockExchange = createExchange(Optional.of("INVALID"));
|
||||||
|
|
||||||
|
WebFilterChain filterChain = exchange -> Mono.empty();
|
||||||
|
|
||||||
|
StepVerifier.create(filter.filter(mockExchange, filterChain))
|
||||||
|
.consumeErrorWith(error -> {
|
||||||
|
assertThat(error, Matchers.instanceOf(IllegalArgumentException.class));
|
||||||
|
assertEquals(error.getMessage(), "HttpMethod 'INVALID' is not supported");
|
||||||
|
})
|
||||||
|
.verify();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -62,8 +82,22 @@ public class HiddenHttpMethodFilterTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
StepVerifier.create(filter.filter(mockExchange, filterChain))
|
StepVerifier.create(filter.filter(mockExchange, filterChain))
|
||||||
.expectComplete()
|
.expectComplete()
|
||||||
.verify();
|
.verify();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void filterWithEmptyStringParameter() {
|
||||||
|
ServerWebExchange mockExchange = createExchange(Optional.of(""));
|
||||||
|
|
||||||
|
WebFilterChain filterChain = exchange -> {
|
||||||
|
assertEquals("Invalid method", HttpMethod.POST, exchange.getRequest().getMethod());
|
||||||
|
return Mono.empty();
|
||||||
|
};
|
||||||
|
|
||||||
|
StepVerifier.create(filter.filter(mockExchange, filterChain))
|
||||||
|
.expectComplete()
|
||||||
|
.verify();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -78,15 +112,15 @@ public class HiddenHttpMethodFilterTests {
|
|||||||
filter.setMethodParam("_foo");
|
filter.setMethodParam("_foo");
|
||||||
|
|
||||||
StepVerifier.create(filter.filter(mockExchange, filterChain))
|
StepVerifier.create(filter.filter(mockExchange, filterChain))
|
||||||
.expectComplete()
|
.expectComplete()
|
||||||
.verify();
|
.verify();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void filterWithoutPost() {
|
public void filterWithoutPost() {
|
||||||
ServerWebExchange mockExchange = createExchange(Optional.of("DELETE")).mutate()
|
ServerWebExchange mockExchange = createExchange(Optional.of("DELETE")).mutate()
|
||||||
.request(builder -> builder.method(HttpMethod.PUT))
|
.request(builder -> builder.method(HttpMethod.PUT))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
WebFilterChain filterChain = exchange -> {
|
WebFilterChain filterChain = exchange -> {
|
||||||
assertEquals("Invalid method", HttpMethod.PUT, exchange.getRequest().getMethod());
|
assertEquals("Invalid method", HttpMethod.PUT, exchange.getRequest().getMethod());
|
||||||
@@ -94,8 +128,8 @@ public class HiddenHttpMethodFilterTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
StepVerifier.create(filter.filter(mockExchange, filterChain))
|
StepVerifier.create(filter.filter(mockExchange, filterChain))
|
||||||
.expectComplete()
|
.expectComplete()
|
||||||
.verify();
|
.verify();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ServerWebExchange createExchange(Optional<String> optionalMethod) {
|
private ServerWebExchange createExchange(Optional<String> optionalMethod) {
|
||||||
@@ -104,12 +138,12 @@ public class HiddenHttpMethodFilterTests {
|
|||||||
|
|
||||||
private ServerWebExchange createExchange(String methodName, Optional<String> optionalBody) {
|
private ServerWebExchange createExchange(String methodName, Optional<String> optionalBody) {
|
||||||
MockServerHttpRequest.BodyBuilder builder = MockServerHttpRequest
|
MockServerHttpRequest.BodyBuilder builder = MockServerHttpRequest
|
||||||
.post("/hotels")
|
.post("/hotels")
|
||||||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
|
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
|
||||||
|
|
||||||
MockServerHttpRequest request = optionalBody
|
MockServerHttpRequest request = optionalBody
|
||||||
.map(method -> builder.body(methodName + "=" + method))
|
.map(method -> builder.body(methodName + "=" + method))
|
||||||
.orElse(builder.build());
|
.orElse(builder.build());
|
||||||
|
|
||||||
MockServerHttpResponse response = new MockServerHttpResponse();
|
MockServerHttpResponse response = new MockServerHttpResponse();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user