16
16
17
17
package org .springframework .test .web .servlet .samples .spr ;
18
18
19
+ import java .io .IOException ;
20
+
21
+ import javax .servlet .FilterChain ;
22
+ import javax .servlet .ServletException ;
23
+ import javax .servlet .ServletRequest ;
24
+ import javax .servlet .ServletResponse ;
19
25
import javax .servlet .http .HttpServletRequest ;
20
26
21
27
import org .junit .Before ;
39
45
import org .springframework .web .context .WebApplicationContext ;
40
46
import org .springframework .web .context .request .RequestAttributes ;
41
47
import org .springframework .web .context .request .RequestContextHolder ;
48
+ import org .springframework .web .context .request .ServletRequestAttributes ;
49
+ import org .springframework .web .filter .GenericFilterBean ;
42
50
import org .springframework .web .servlet .config .annotation .EnableWebMvc ;
43
51
import org .springframework .web .servlet .config .annotation .WebMvcConfigurerAdapter ;
44
52
47
55
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
48
56
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
49
57
import static org .springframework .test .web .servlet .setup .MockMvcBuilders .*;
50
- import static org .springframework .web .context .request .RequestAttributes .*;
51
58
52
59
/**
53
- * Test for SPR-10025 (access to request attributes via RequestContextHolder).
60
+ * Tests for SPR-10025 (access to request attributes via RequestContextHolder)
61
+ * and SPR-13211 (re-use of mock request from the TestContext framework).
54
62
*
55
63
* @author Rossen Stoyanchev
56
64
* @author Sam Brannen
61
69
@ DirtiesContext
62
70
public class RequestContextHolderTests {
63
71
64
- private static final String FOO = "foo " ;
65
- private static final String BAR = "bar " ;
66
- private static final String BAZ = "baz " ;
67
- private static final String QUUX = "quux " ;
68
- private static final String ENIGMA = "enigma" ;
69
- private static final String PUZZLE = "puzzle" ;
72
+ private static final String FROM_TCF_MOCK = "fromTestContextFrameworkMock " ;
73
+ private static final String FROM_MVC_TEST_DEFAULT = "fromSpringMvcTestDefault " ;
74
+ private static final String FROM_MVC_TEST_MOCK = "fromSpringMvcTestMock " ;
75
+ private static final String FROM_FILTER = "fromFilter " ;
76
+
77
+ private static final String ENIGMA = "puzzle" ;
70
78
71
79
@ Autowired
72
80
private WebApplicationContext wac ;
@@ -75,83 +83,188 @@ public class RequestContextHolderTests {
75
83
private MockHttpServletRequest mockRequest ;
76
84
77
85
@ Autowired
78
- private MyScopedController myScopedController ;
86
+ private RequestScopedController requestScopedController ;
87
+
88
+ @ Autowired
89
+ private RequestScopedService requestScopedService ;
90
+
91
+ @ Autowired
92
+ private SessionScopedService sessionScopedService ;
79
93
80
94
private MockMvc mockMvc ;
81
95
82
96
83
97
@ Before
84
98
public void setup () {
85
- this .mockRequest .setAttribute (FOO , BAR );
99
+ this .mockRequest .setAttribute (FROM_TCF_MOCK , ENIGMA );
86
100
87
101
this .mockMvc = webAppContextSetup (this .wac )
88
- .defaultRequest (get ("/" ).requestAttr (ENIGMA , PUZZLE ))
102
+ .addFilter (new AbcFilter ())
103
+ .defaultRequest (get ("/" ).requestAttr (FROM_MVC_TEST_DEFAULT , ENIGMA ))
89
104
.alwaysExpect (status ().isOk ())
90
105
.build ();
91
106
}
92
107
93
108
@ Test
94
109
public void singletonController () throws Exception {
95
- this .mockMvc .perform (get ("/singleton " ).requestAttr (BAZ , QUUX ));
110
+ this .mockMvc .perform (get ("/singletonController " ).requestAttr (FROM_MVC_TEST_MOCK , ENIGMA ));
96
111
}
97
112
98
113
@ Test
99
114
public void requestScopedController () throws Exception {
100
- assertTrue ("request-scoped controller must be a CGLIB proxy" , AopUtils .isCglibProxy (this .myScopedController ));
101
- this .mockMvc .perform (get ("/requestScoped" ).requestAttr (BAZ , QUUX ));
115
+ assertTrue ("request-scoped controller must be a CGLIB proxy" , AopUtils .isCglibProxy (this .requestScopedController ));
116
+ this .mockMvc .perform (get ("/requestScopedController" ).requestAttr (FROM_MVC_TEST_MOCK , ENIGMA ));
117
+ }
118
+
119
+ @ Test
120
+ public void requestScopedService () throws Exception {
121
+ assertTrue ("request-scoped service must be a CGLIB proxy" , AopUtils .isCglibProxy (this .requestScopedService ));
122
+ this .mockMvc .perform (get ("/requestScopedService" ).requestAttr (FROM_MVC_TEST_MOCK , ENIGMA ));
123
+ }
124
+
125
+ @ Test
126
+ public void sessionScopedService () throws Exception {
127
+ assertTrue ("session-scoped service must be a CGLIB proxy" , AopUtils .isCglibProxy (this .sessionScopedService ));
128
+ this .mockMvc .perform (get ("/sessionScopedService" ).requestAttr (FROM_MVC_TEST_MOCK , ENIGMA ));
102
129
}
103
130
104
131
132
+ // -------------------------------------------------------------------
133
+
105
134
@ Configuration
106
135
@ EnableWebMvc
107
136
static class WebConfig extends WebMvcConfigurerAdapter {
108
137
109
138
@ Bean
110
- public MyController myController () {
111
- return new MyController ();
139
+ public SingletonController singletonController () {
140
+ return new SingletonController ();
112
141
}
113
142
114
143
@ Bean
115
144
@ Scope (name = "request" , proxyMode = ScopedProxyMode .TARGET_CLASS )
116
- public MyScopedController myScopedController () {
117
- return new MyScopedController ();
145
+ public RequestScopedController requestScopedController () {
146
+ return new RequestScopedController ();
147
+ }
148
+
149
+ @ Bean
150
+ @ Scope (name = "request" , proxyMode = ScopedProxyMode .TARGET_CLASS )
151
+ public RequestScopedService requestScopedService () {
152
+ return new RequestScopedService ();
153
+ }
154
+
155
+ @ Bean
156
+ public ControllerWithRequestScopedService controllerWithRequestScopedService () {
157
+ return new ControllerWithRequestScopedService ();
158
+ }
159
+
160
+ @ Bean
161
+ @ Scope (name = "session" , proxyMode = ScopedProxyMode .TARGET_CLASS )
162
+ public SessionScopedService sessionScopedService () {
163
+ return new SessionScopedService ();
164
+ }
165
+
166
+ @ Bean
167
+ public ControllerWithSessionScopedService controllerWithSessionScopedService () {
168
+ return new ControllerWithSessionScopedService ();
118
169
}
119
170
}
120
171
172
+ @ RestController
173
+ private static class SingletonController {
121
174
122
- private static void assertRequestAttributes () {
123
- RequestAttributes attributes = RequestContextHolder .getRequestAttributes ();
124
- // TODO [SPR-13211] Assert that FOO is BAR, instead of NULL.
125
- // assertThat(attributes.getAttribute(FOO, SCOPE_REQUEST), is(BAR));
126
- assertThat (attributes .getAttribute (FOO , SCOPE_REQUEST ), is (nullValue ()));
127
- assertThat (attributes .getAttribute (ENIGMA , SCOPE_REQUEST ), is (PUZZLE ));
128
- assertThat (attributes .getAttribute (BAZ , SCOPE_REQUEST ), is (QUUX ));
175
+ @ RequestMapping ("/singletonController" )
176
+ public void handle () {
177
+ assertRequestAttributes ();
178
+ }
129
179
}
130
180
181
+ @ RestController
182
+ private static class RequestScopedController {
183
+
184
+ @ Autowired
185
+ private HttpServletRequest request ;
186
+
187
+
188
+ @ RequestMapping ("/requestScopedController" )
189
+ public void handle () {
190
+ assertRequestAttributes (request );
191
+ assertRequestAttributes ();
192
+ }
193
+ }
194
+
195
+ private static class RequestScopedService {
196
+
197
+ @ Autowired
198
+ private HttpServletRequest request ;
199
+
200
+
201
+ void process () {
202
+ assertRequestAttributes (request );
203
+ }
204
+ }
205
+
206
+ private static class SessionScopedService {
207
+
208
+ @ Autowired
209
+ private HttpServletRequest request ;
210
+
211
+
212
+ void process () {
213
+ assertRequestAttributes (this .request );
214
+ }
215
+ }
131
216
132
217
@ RestController
133
- private static class MyController {
218
+ private static class ControllerWithRequestScopedService {
219
+
220
+ @ Autowired
221
+ private RequestScopedService service ;
134
222
135
- @ RequestMapping ("/singleton" )
223
+
224
+ @ RequestMapping ("/requestScopedService" )
136
225
public void handle () {
226
+ this .service .process ();
137
227
assertRequestAttributes ();
138
228
}
139
229
}
140
230
141
231
@ RestController
142
- private static class MyScopedController {
232
+ private static class ControllerWithSessionScopedService {
143
233
144
234
@ Autowired
145
- private HttpServletRequest request ;
235
+ private SessionScopedService service ;
146
236
147
237
148
- @ RequestMapping ("/requestScoped " )
238
+ @ RequestMapping ("/sessionScopedService " )
149
239
public void handle () {
150
- // TODO [SPR-13211] Assert that FOO is BAR, instead of NULL.
151
- // assertThat(this.request.getAttribute(FOO), is(BAR));
152
- assertThat (this .request .getAttribute (FOO ), is (nullValue ()));
240
+ this .service .process ();
153
241
assertRequestAttributes ();
154
242
}
155
243
}
156
244
245
+ private static class AbcFilter extends GenericFilterBean {
246
+
247
+ @ Override
248
+ public void doFilter (ServletRequest request , ServletResponse response , FilterChain chain ) throws IOException , ServletException {
249
+ request .setAttribute (FROM_FILTER , ENIGMA );
250
+ chain .doFilter (request , response );
251
+ }
252
+ }
253
+
254
+
255
+ private static void assertRequestAttributes () {
256
+ RequestAttributes requestAttributes = RequestContextHolder .getRequestAttributes ();
257
+ assertThat (requestAttributes , instanceOf (ServletRequestAttributes .class ));
258
+ assertRequestAttributes (((ServletRequestAttributes ) requestAttributes ).getRequest ());
259
+ }
260
+
261
+ private static void assertRequestAttributes (HttpServletRequest request ) {
262
+ // TODO [SPR-13211] Assert that FOO is ENIGMA, instead of NULL.
263
+ // assertThat(this.request.getAttribute(FOO), is(ENIGMA));
264
+ assertThat (request .getAttribute (FROM_TCF_MOCK ), is (nullValue ()));
265
+ assertThat (request .getAttribute (FROM_MVC_TEST_DEFAULT ), is (ENIGMA ));
266
+ assertThat (request .getAttribute (FROM_MVC_TEST_MOCK ), is (ENIGMA ));
267
+ assertThat (request .getAttribute (FROM_FILTER ), is (ENIGMA ));
268
+ }
269
+
157
270
}
0 commit comments