瀏覽代碼

Polish ExchangeMutatorWebFilter Support

Issue gh-4343
Rob Winch 8 年之前
父節點
當前提交
915de03f42

+ 3 - 2
samples/javaconfig/hellowebflux/src/test/java/sample/HelloWebfluxApplicationTests.java

@@ -161,16 +161,17 @@ public class HelloWebfluxApplicationTests {
 
 	@Test
 	public void mockSupport() throws Exception {
-		ExchangeMutatorWebFilter exchangeMutator = new ExchangeMutatorWebFilter(withUser());
+		ExchangeMutatorWebFilter exchangeMutator = new ExchangeMutatorWebFilter();
 		WebTestClient mockRest = WebTestClient.bindToApplicationContext(this.context).webFilter(exchangeMutator).build();
 
 		mockRest
+			.filter(exchangeMutator.perClient(withUser()))
 			.get()
 			.uri("/principal")
 			.exchange()
 			.expectStatus().isOk();
 
-		this.rest
+		mockRest
 			.get()
 			.uri("/principal")
 			.exchange()

+ 4 - 3
samples/javaconfig/hellowebfluxfn/src/test/java/sample/HelloWebfluxFnApplicationTests.java

@@ -167,16 +167,17 @@ public class HelloWebfluxFnApplicationTests {
 
 	@Test
 	public void mockSupport() throws Exception {
-		ExchangeMutatorWebFilter exchangeMutator = new ExchangeMutatorWebFilter(withUser());
-		WebTestClient mockRest = WebTestClient.bindToRouterFunction(this.routerFunction).webFilter(exchangeMutator).build();
+		ExchangeMutatorWebFilter exchangeMutator = new ExchangeMutatorWebFilter();
+		WebTestClient mockRest = WebTestClient.bindToRouterFunction(this.routerFunction).webFilter(exchangeMutator, springSecurityFilterChain).build();
 
 		mockRest
+			.filter(exchangeMutator.perClient(withUser()))
 			.get()
 			.uri("/principal")
 			.exchange()
 			.expectStatus().isOk();
 
-		this.rest
+		mockRest
 			.get()
 			.uri("/principal")
 			.exchange()

+ 12 - 15
test/src/main/java/org/springframework/security/test/web/reactive/server/SecurityExchangeMutators.java

@@ -28,11 +28,13 @@ import reactor.core.publisher.Mono;
 
 import java.security.Principal;
 import java.util.Collection;
+import java.util.function.Function;
 import java.util.function.UnaryOperator;
 
 /**
  * Test utilities for working with Spring Security and
- * {{@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}}.
+ * {{@link org.springframework.test.web.reactive.server.WebTestClient}} using
+ * {{{@link org.springframework.test.web.reactive.server.ExchangeMutatorWebFilter}}}.
  *
  * @author Rob Winch
  * @since 5.0
@@ -42,10 +44,9 @@ public class SecurityExchangeMutators {
 	 * Updates the ServerWebExchange to use the provided Principal
 	 *
 	 * @param principal the principal to use.
-	 * @return the {@link UnaryOperator<ServerWebExchange>}} to provide to
-	 * {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}
+	 * @return the {@link  Function<ServerWebExchange, ServerWebExchange>}} to use
 	 */
-	public static UnaryOperator<ServerWebExchange> withPrincipal(Principal principal) {
+	public static  Function<ServerWebExchange, ServerWebExchange> withPrincipal(Principal principal) {
 		return m -> m.mutate().principal(Mono.just(principal)).build();
 	}
 
@@ -53,10 +54,9 @@ public class SecurityExchangeMutators {
 	 * Updates the ServerWebExchange to use the provided Authentication as the Principal
 	 *
 	 * @param authentication the Authentication to use.
-	 * @return the {@link UnaryOperator<ServerWebExchange>}} to provide to
-	 * {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}
+	 * @return the {@link  Function<ServerWebExchange, ServerWebExchange>}} to use
 	 */
-	public static UnaryOperator<ServerWebExchange> withAuthentication(Authentication authentication) {
+	public static Function<ServerWebExchange, ServerWebExchange> withAuthentication(Authentication authentication) {
 		return withPrincipal(authentication);
 	}
 
@@ -65,10 +65,9 @@ public class SecurityExchangeMutators {
 	 * the Principal
 	 *
 	 * @param userDetails the UserDetails to use.
-	 * @return the {@link UnaryOperator<ServerWebExchange>}} to provide to
-	 * {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}
+	 * @return the {@link  Function<ServerWebExchange, ServerWebExchange>}} to use
 	 */
-	public static UnaryOperator<ServerWebExchange> withUser(UserDetails userDetails) {
+	public static  Function<ServerWebExchange, ServerWebExchange> withUser(UserDetails userDetails) {
 		return withAuthentication(new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities()));
 	}
 
@@ -77,8 +76,7 @@ public class SecurityExchangeMutators {
 	 * the Principal. This uses a default username of "user", password of "password", and granted authorities of
 	 * "ROLE_USER".
 	 *
-	 * @return the {@link UnaryOperator<ServerWebExchange>}} to provide to
-	 * {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}
+	 * @return the {@link  Function<ServerWebExchange, ServerWebExchange>}} to use
 	 */
 	public static UserExchangeMutator withUser() {
 		return withUser("user");
@@ -90,8 +88,7 @@ public class SecurityExchangeMutators {
 	 * the Principal. This uses a default password of "password" and granted authorities of
 	 * "ROLE_USER".
 	 *
-	 * @return the {@link UnaryOperator<ServerWebExchange>}} to provide to
-	 * {@link org.springframework.test.web.reactive.server.WebTestClient#exchangeMutator(UnaryOperator)}
+	 * @return the {@link  Function<ServerWebExchange, ServerWebExchange>}} to use
 	 */
 	public static UserExchangeMutator withUser(String username) {
 		return new UserExchangeMutator(username);
@@ -101,7 +98,7 @@ public class SecurityExchangeMutators {
 	 * Updates the WebServerExchange using {@code SecurityExchangeMutators#withUser(UserDetails)}. Defaults to use a
 	 * password of "password" and granted authorities of "ROLE_USER".
 	 */
-	public static class UserExchangeMutator implements UnaryOperator<ServerWebExchange> {
+	public static class UserExchangeMutator implements Function<ServerWebExchange, ServerWebExchange> {
 		private final User.UserBuilder userBuilder;
 
 		private UserExchangeMutator(String username) {