Add option to set Principal in MockServerWebExchange
See gh-34789 Signed-off-by: blake_bauman <blake_bauman@apple.com>
This commit is contained in:
committed by
rstoyanchev
parent
44500cf868
commit
d15abd58b4
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.mock.web.server;
|
||||
|
||||
import java.security.Principal;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -39,16 +41,19 @@ import org.springframework.web.server.session.WebSessionManager;
|
||||
* @since 5.0
|
||||
*/
|
||||
public final class MockServerWebExchange extends DefaultServerWebExchange {
|
||||
private final Mono<Principal> principalMono;
|
||||
|
||||
|
||||
private MockServerWebExchange(
|
||||
MockServerHttpRequest request, @Nullable WebSessionManager sessionManager,
|
||||
@Nullable ApplicationContext applicationContext) {
|
||||
@Nullable ApplicationContext applicationContext, Mono<Principal> principalMono) {
|
||||
|
||||
super(request, new MockServerHttpResponse(),
|
||||
sessionManager != null ? sessionManager : new DefaultWebSessionManager(),
|
||||
ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver(),
|
||||
applicationContext);
|
||||
|
||||
this.principalMono = principalMono;
|
||||
}
|
||||
|
||||
|
||||
@@ -57,6 +62,12 @@ public final class MockServerWebExchange extends DefaultServerWebExchange {
|
||||
return (MockServerHttpResponse) super.getResponse();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T extends Principal> Mono<T> getPrincipal() {
|
||||
return (Mono<T>)this.principalMono;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a {@link MockServerWebExchange} from the given mock request.
|
||||
@@ -111,6 +122,8 @@ public final class MockServerWebExchange extends DefaultServerWebExchange {
|
||||
@Nullable
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private Mono<Principal> principalMono = Mono.empty();
|
||||
|
||||
public Builder(MockServerHttpRequest request) {
|
||||
this.request = request;
|
||||
}
|
||||
@@ -147,11 +160,16 @@ public final class MockServerWebExchange extends DefaultServerWebExchange {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder principal(@Nullable Principal principal) {
|
||||
this.principalMono = (principal == null) ? Mono.empty() : Mono.just(principal);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the {@code MockServerWebExchange} instance.
|
||||
*/
|
||||
public MockServerWebExchange build() {
|
||||
return new MockServerWebExchange(this.request, this.sessionManager, this.applicationContext);
|
||||
return new MockServerWebExchange(this.request, this.sessionManager, this.applicationContext, this.principalMono);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user