Skip to content

Commit 190dabb

Browse files
committed
Polishing contribution
Closes gh-34789
1 parent d15abd5 commit 190dabb

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

spring-test/src/main/java/org/springframework/mock/web/server/MockServerWebExchange.java

+18-6
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,20 @@
4141
* @since 5.0
4242
*/
4343
public final class MockServerWebExchange extends DefaultServerWebExchange {
44+
4445
private final Mono<Principal> principalMono;
4546

4647

4748
private MockServerWebExchange(
4849
MockServerHttpRequest request, @Nullable WebSessionManager sessionManager,
49-
@Nullable ApplicationContext applicationContext, Mono<Principal> principalMono) {
50+
@Nullable ApplicationContext applicationContext, @Nullable Principal principal) {
5051

5152
super(request, new MockServerHttpResponse(),
5253
sessionManager != null ? sessionManager : new DefaultWebSessionManager(),
5354
ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver(),
5455
applicationContext);
5556

56-
this.principalMono = principalMono;
57+
this.principalMono = (principal != null) ? Mono.just(principal) : Mono.empty();
5758
}
5859

5960

@@ -62,10 +63,14 @@ public MockServerHttpResponse getResponse() {
6263
return (MockServerHttpResponse) super.getResponse();
6364
}
6465

66+
/**
67+
* Return the user set via {@link Builder#principal(Principal)}.
68+
* @since 6.2.7
69+
*/
6570
@SuppressWarnings("unchecked")
6671
@Override
6772
public <T extends Principal> Mono<T> getPrincipal() {
68-
return (Mono<T>)this.principalMono;
73+
return (Mono<T>) this.principalMono;
6974
}
7075

7176

@@ -122,7 +127,8 @@ public static class Builder {
122127
@Nullable
123128
private ApplicationContext applicationContext;
124129

125-
private Mono<Principal> principalMono = Mono.empty();
130+
@Nullable
131+
private Principal principal;
126132

127133
public Builder(MockServerHttpRequest request) {
128134
this.request = request;
@@ -160,16 +166,22 @@ public Builder applicationContext(ApplicationContext applicationContext) {
160166
return this;
161167
}
162168

169+
/**
170+
* Provide a user to associate with the exchange.
171+
* @param principal the principal to use
172+
* @since 6.2.7
173+
*/
163174
public Builder principal(@Nullable Principal principal) {
164-
this.principalMono = (principal == null) ? Mono.empty() : Mono.just(principal);
175+
this.principal = principal;
165176
return this;
166177
}
167178

168179
/**
169180
* Build the {@code MockServerWebExchange} instance.
170181
*/
171182
public MockServerWebExchange build() {
172-
return new MockServerWebExchange(this.request, this.sessionManager, this.applicationContext, this.principalMono);
183+
return new MockServerWebExchange(
184+
this.request, this.sessionManager, this.applicationContext, this.principal);
173185
}
174186
}
175187

0 commit comments

Comments
 (0)