16
16
17
17
package org .springframework .mock .web .server ;
18
18
19
+ import java .security .Principal ;
20
+
19
21
import reactor .core .publisher .Mono ;
20
22
21
23
import org .springframework .context .ApplicationContext ;
39
41
* @since 5.0
40
42
*/
41
43
public final class MockServerWebExchange extends DefaultServerWebExchange {
44
+ private final Mono <Principal > principalMono ;
42
45
43
46
44
47
private MockServerWebExchange (
45
48
MockServerHttpRequest request , @ Nullable WebSessionManager sessionManager ,
46
- @ Nullable ApplicationContext applicationContext ) {
49
+ @ Nullable ApplicationContext applicationContext , Mono < Principal > principalMono ) {
47
50
48
51
super (request , new MockServerHttpResponse (),
49
52
sessionManager != null ? sessionManager : new DefaultWebSessionManager (),
50
53
ServerCodecConfigurer .create (), new AcceptHeaderLocaleContextResolver (),
51
54
applicationContext );
55
+
56
+ this .principalMono = principalMono ;
52
57
}
53
58
54
59
@@ -57,6 +62,12 @@ public MockServerHttpResponse getResponse() {
57
62
return (MockServerHttpResponse ) super .getResponse ();
58
63
}
59
64
65
+ @ SuppressWarnings ("unchecked" )
66
+ @ Override
67
+ public <T extends Principal > Mono <T > getPrincipal () {
68
+ return (Mono <T >)this .principalMono ;
69
+ }
70
+
60
71
61
72
/**
62
73
* Create a {@link MockServerWebExchange} from the given mock request.
@@ -111,6 +122,8 @@ public static class Builder {
111
122
@ Nullable
112
123
private ApplicationContext applicationContext ;
113
124
125
+ private Mono <Principal > principalMono = Mono .empty ();
126
+
114
127
public Builder (MockServerHttpRequest request ) {
115
128
this .request = request ;
116
129
}
@@ -147,11 +160,16 @@ public Builder applicationContext(ApplicationContext applicationContext) {
147
160
return this ;
148
161
}
149
162
163
+ public Builder principal (@ Nullable Principal principal ) {
164
+ this .principalMono = (principal == null ) ? Mono .empty () : Mono .just (principal );
165
+ return this ;
166
+ }
167
+
150
168
/**
151
169
* Build the {@code MockServerWebExchange} instance.
152
170
*/
153
171
public MockServerWebExchange build () {
154
- return new MockServerWebExchange (this .request , this .sessionManager , this .applicationContext );
172
+ return new MockServerWebExchange (this .request , this .sessionManager , this .applicationContext , this . principalMono );
155
173
}
156
174
}
157
175
0 commit comments