Skip to content

Commit 3c2efee

Browse files
committed
Investigate claims raised in SPR-13211
This commit adds additional tests to RequestContextHolderTests that verify proper support for the MockHttpServletRequest managed by Spring MVC Test in the following scenarios: - request-scoped service invoked by controller - session-scoped service invoked by controller - custom filter that sets request attributes Issue: SPR-13211
1 parent 6679120 commit 3c2efee

File tree

1 file changed

+146
-33
lines changed

1 file changed

+146
-33
lines changed

spring-test/src/test/java/org/springframework/test/web/servlet/samples/spr/RequestContextHolderTests.java

Lines changed: 146 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

1717
package org.springframework.test.web.servlet.samples.spr;
1818

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;
1925
import javax.servlet.http.HttpServletRequest;
2026

2127
import org.junit.Before;
@@ -39,6 +45,8 @@
3945
import org.springframework.web.context.WebApplicationContext;
4046
import org.springframework.web.context.request.RequestAttributes;
4147
import org.springframework.web.context.request.RequestContextHolder;
48+
import org.springframework.web.context.request.ServletRequestAttributes;
49+
import org.springframework.web.filter.GenericFilterBean;
4250
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
4351
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
4452

@@ -47,10 +55,10 @@
4755
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
4856
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
4957
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
50-
import static org.springframework.web.context.request.RequestAttributes.*;
5158

5259
/**
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).
5462
*
5563
* @author Rossen Stoyanchev
5664
* @author Sam Brannen
@@ -61,12 +69,12 @@
6169
@DirtiesContext
6270
public class RequestContextHolderTests {
6371

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";
7078

7179
@Autowired
7280
private WebApplicationContext wac;
@@ -75,83 +83,188 @@ public class RequestContextHolderTests {
7583
private MockHttpServletRequest mockRequest;
7684

7785
@Autowired
78-
private MyScopedController myScopedController;
86+
private RequestScopedController requestScopedController;
87+
88+
@Autowired
89+
private RequestScopedService requestScopedService;
90+
91+
@Autowired
92+
private SessionScopedService sessionScopedService;
7993

8094
private MockMvc mockMvc;
8195

8296

8397
@Before
8498
public void setup() {
85-
this.mockRequest.setAttribute(FOO, BAR);
99+
this.mockRequest.setAttribute(FROM_TCF_MOCK, ENIGMA);
86100

87101
this.mockMvc = webAppContextSetup(this.wac)
88-
.defaultRequest(get("/").requestAttr(ENIGMA, PUZZLE))
102+
.addFilter(new AbcFilter())
103+
.defaultRequest(get("/").requestAttr(FROM_MVC_TEST_DEFAULT, ENIGMA))
89104
.alwaysExpect(status().isOk())
90105
.build();
91106
}
92107

93108
@Test
94109
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));
96111
}
97112

98113
@Test
99114
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));
102129
}
103130

104131

132+
// -------------------------------------------------------------------
133+
105134
@Configuration
106135
@EnableWebMvc
107136
static class WebConfig extends WebMvcConfigurerAdapter {
108137

109138
@Bean
110-
public MyController myController() {
111-
return new MyController();
139+
public SingletonController singletonController() {
140+
return new SingletonController();
112141
}
113142

114143
@Bean
115144
@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();
118169
}
119170
}
120171

172+
@RestController
173+
private static class SingletonController {
121174

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+
}
129179
}
130180

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+
}
131216

132217
@RestController
133-
private static class MyController {
218+
private static class ControllerWithRequestScopedService {
219+
220+
@Autowired
221+
private RequestScopedService service;
134222

135-
@RequestMapping("/singleton")
223+
224+
@RequestMapping("/requestScopedService")
136225
public void handle() {
226+
this.service.process();
137227
assertRequestAttributes();
138228
}
139229
}
140230

141231
@RestController
142-
private static class MyScopedController {
232+
private static class ControllerWithSessionScopedService {
143233

144234
@Autowired
145-
private HttpServletRequest request;
235+
private SessionScopedService service;
146236

147237

148-
@RequestMapping("/requestScoped")
238+
@RequestMapping("/sessionScopedService")
149239
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();
153241
assertRequestAttributes();
154242
}
155243
}
156244

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+
157270
}

0 commit comments

Comments
 (0)