|
@@ -17,6 +17,9 @@
|
|
|
*/
|
|
|
package sample;
|
|
|
|
|
|
+import java.util.Map;
|
|
|
+import java.util.function.Consumer;
|
|
|
+
|
|
|
import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
import org.junit.runner.RunWith;
|
|
@@ -27,11 +30,11 @@ import org.springframework.test.context.ActiveProfiles;
|
|
|
import org.springframework.test.context.ContextConfiguration;
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
|
|
-import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
|
|
|
import org.springframework.web.reactive.function.server.RouterFunction;
|
|
|
|
|
|
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockUser;
|
|
|
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity;
|
|
|
+import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials;
|
|
|
import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;
|
|
|
|
|
|
/**
|
|
@@ -55,6 +58,8 @@ public class HelloWebfluxFnApplicationTests {
|
|
|
.bindToRouterFunction(this.routerFunction)
|
|
|
.webFilter(this.springSecurityFilterChain)
|
|
|
.apply(springSecurity())
|
|
|
+ .configureClient()
|
|
|
+ .filter(basicAuthentication())
|
|
|
.build();
|
|
|
}
|
|
|
|
|
@@ -70,11 +75,9 @@ public class HelloWebfluxFnApplicationTests {
|
|
|
@Test
|
|
|
public void basicWhenValidCredentialsThenOk() throws Exception {
|
|
|
this.rest
|
|
|
- .mutate()
|
|
|
- .filter(userCredentials())
|
|
|
- .build()
|
|
|
.get()
|
|
|
.uri("/")
|
|
|
+ .attributes(userCredentials())
|
|
|
.exchange()
|
|
|
.expectStatus().isOk()
|
|
|
.expectBody().json("{\"message\":\"Hello user!\"}");
|
|
@@ -83,11 +86,9 @@ public class HelloWebfluxFnApplicationTests {
|
|
|
@Test
|
|
|
public void basicWhenInvalidCredentialsThenUnauthorized() throws Exception {
|
|
|
this.rest
|
|
|
- .mutate()
|
|
|
- .filter(invalidPassword())
|
|
|
- .build()
|
|
|
.get()
|
|
|
.uri("/")
|
|
|
+ .attributes(invalidCredentials())
|
|
|
.exchange()
|
|
|
.expectStatus().isUnauthorized()
|
|
|
.expectBody().isEmpty();
|
|
@@ -110,11 +111,11 @@ public class HelloWebfluxFnApplicationTests {
|
|
|
.expectStatus().isUnauthorized();
|
|
|
}
|
|
|
|
|
|
- private ExchangeFilterFunction userCredentials() {
|
|
|
- return basicAuthentication("user","user");
|
|
|
+ private Consumer<Map<String, Object>> userCredentials() {
|
|
|
+ return basicAuthenticationCredentials("user","user");
|
|
|
}
|
|
|
|
|
|
- private ExchangeFilterFunction invalidPassword() {
|
|
|
- return basicAuthentication("user","INVALID");
|
|
|
+ private Consumer<Map<String, Object>> invalidCredentials() {
|
|
|
+ return basicAuthenticationCredentials("user","INVALID");
|
|
|
}
|
|
|
}
|