41
41
* @since 5.0
42
42
*/
43
43
public final class MockServerWebExchange extends DefaultServerWebExchange {
44
+
44
45
private final Mono <Principal > principalMono ;
45
46
46
47
47
48
private MockServerWebExchange (
48
49
MockServerHttpRequest request , @ Nullable WebSessionManager sessionManager ,
49
- @ Nullable ApplicationContext applicationContext , Mono < Principal > principalMono ) {
50
+ @ Nullable ApplicationContext applicationContext , @ Nullable Principal principal ) {
50
51
51
52
super (request , new MockServerHttpResponse (),
52
53
sessionManager != null ? sessionManager : new DefaultWebSessionManager (),
53
54
ServerCodecConfigurer .create (), new AcceptHeaderLocaleContextResolver (),
54
55
applicationContext );
55
56
56
- this .principalMono = principalMono ;
57
+ this .principalMono = ( principal != null ) ? Mono . just ( principal ) : Mono . empty () ;
57
58
}
58
59
59
60
@@ -62,10 +63,14 @@ public MockServerHttpResponse getResponse() {
62
63
return (MockServerHttpResponse ) super .getResponse ();
63
64
}
64
65
66
+ /**
67
+ * Return the user set via {@link Builder#principal(Principal)}.
68
+ * @since 6.2.7
69
+ */
65
70
@ SuppressWarnings ("unchecked" )
66
71
@ Override
67
72
public <T extends Principal > Mono <T > getPrincipal () {
68
- return (Mono <T >)this .principalMono ;
73
+ return (Mono <T >) this .principalMono ;
69
74
}
70
75
71
76
@@ -122,7 +127,8 @@ public static class Builder {
122
127
@ Nullable
123
128
private ApplicationContext applicationContext ;
124
129
125
- private Mono <Principal > principalMono = Mono .empty ();
130
+ @ Nullable
131
+ private Principal principal ;
126
132
127
133
public Builder (MockServerHttpRequest request ) {
128
134
this .request = request ;
@@ -160,16 +166,22 @@ public Builder applicationContext(ApplicationContext applicationContext) {
160
166
return this ;
161
167
}
162
168
169
+ /**
170
+ * Provide a user to associate with the exchange.
171
+ * @param principal the principal to use
172
+ * @since 6.2.7
173
+ */
163
174
public Builder principal (@ Nullable Principal principal ) {
164
- this .principalMono = ( principal == null ) ? Mono . empty () : Mono . just ( principal ) ;
175
+ this .principal = principal ;
165
176
return this ;
166
177
}
167
178
168
179
/**
169
180
* Build the {@code MockServerWebExchange} instance.
170
181
*/
171
182
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 );
173
185
}
174
186
}
175
187
0 commit comments