1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .test .web .servlet .samples .spr ;
18
18
19
+ import javax .servlet .http .HttpServletRequest ;
20
+
19
21
import org .junit .Before ;
20
22
import org .junit .Test ;
21
23
import org .junit .runner .RunWith ;
22
24
25
+ import org .springframework .aop .support .AopUtils ;
23
26
import org .springframework .beans .factory .annotation .Autowired ;
24
27
import org .springframework .context .annotation .Bean ;
25
28
import org .springframework .context .annotation .Configuration ;
29
+ import org .springframework .context .annotation .Scope ;
30
+ import org .springframework .context .annotation .ScopedProxyMode ;
26
31
import org .springframework .mock .web .MockHttpServletRequest ;
27
- import org .springframework .stereotype . Controller ;
32
+ import org .springframework .test . annotation . DirtiesContext ;
28
33
import org .springframework .test .context .ContextConfiguration ;
29
34
import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
30
35
import org .springframework .test .context .web .WebAppConfiguration ;
31
36
import org .springframework .test .web .servlet .MockMvc ;
32
37
import org .springframework .web .bind .annotation .RequestMapping ;
33
- import org .springframework .web .bind .annotation .ResponseBody ;
38
+ import org .springframework .web .bind .annotation .RestController ;
34
39
import org .springframework .web .context .WebApplicationContext ;
35
40
import org .springframework .web .context .request .RequestAttributes ;
36
41
import org .springframework .web .context .request .RequestContextHolder ;
37
42
import org .springframework .web .servlet .config .annotation .EnableWebMvc ;
38
43
import org .springframework .web .servlet .config .annotation .WebMvcConfigurerAdapter ;
39
44
45
+ import static org .hamcrest .CoreMatchers .*;
40
46
import static org .junit .Assert .*;
41
47
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
42
48
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
43
49
import static org .springframework .test .web .servlet .setup .MockMvcBuilders .*;
50
+ import static org .springframework .web .context .request .RequestAttributes .*;
44
51
45
52
/**
46
53
* Test for SPR-10025 (access to request attributes via RequestContextHolder).
47
54
*
48
55
* @author Rossen Stoyanchev
56
+ * @author Sam Brannen
49
57
*/
50
58
@ RunWith (SpringJUnit4ClassRunner .class )
51
59
@ WebAppConfiguration
52
60
@ ContextConfiguration
61
+ @ DirtiesContext
53
62
public class RequestContextHolderTests {
54
63
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" ;
70
+
55
71
@ Autowired
56
72
private WebApplicationContext wac ;
57
73
58
74
@ Autowired
59
- private MockHttpServletRequest servletRequest ;
75
+ private MockHttpServletRequest mockRequest ;
76
+
77
+ @ Autowired
78
+ private MyScopedController myScopedController ;
60
79
61
80
private MockMvc mockMvc ;
62
81
63
82
64
83
@ Before
65
84
public void setup () {
66
- this .mockMvc = webAppContextSetup (this .wac ).build ();
85
+ this .mockRequest .setAttribute (FOO , BAR );
86
+
87
+ this .mockMvc = webAppContextSetup (this .wac )
88
+ .defaultRequest (get ("/" ).requestAttr (ENIGMA , PUZZLE ))
89
+ .alwaysExpect (status ().isOk ())
90
+ .build ();
67
91
}
68
92
69
93
@ Test
70
- public void test () throws Exception {
71
- this .servletRequest .setAttribute ("foo1" , "bar" );
72
- this .mockMvc .perform (get ("/myUrl" ).requestAttr ("foo2" , "bar" )).andExpect (status ().isOk ());
94
+ public void singletonController () throws Exception {
95
+ this .mockMvc .perform (get ("/singleton" ).requestAttr (BAZ , QUUX ));
96
+ }
97
+
98
+ @ Test
99
+ 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 ));
73
102
}
74
103
75
104
@@ -81,17 +110,47 @@ static class WebConfig extends WebMvcConfigurerAdapter {
81
110
public MyController myController () {
82
111
return new MyController ();
83
112
}
113
+
114
+ @ Bean
115
+ @ Scope (name = "request" , proxyMode = ScopedProxyMode .TARGET_CLASS )
116
+ public MyScopedController myScopedController () {
117
+ return new MyScopedController ();
118
+ }
84
119
}
85
120
86
- @ Controller
121
+
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 ));
129
+ }
130
+
131
+
132
+ @ RestController
87
133
private static class MyController {
88
134
89
- @ RequestMapping ("/myUrl" )
90
- @ ResponseBody
135
+ @ RequestMapping ("/singleton" )
136
+ public void handle () {
137
+ assertRequestAttributes ();
138
+ }
139
+ }
140
+
141
+ @ RestController
142
+ private static class MyScopedController {
143
+
144
+ @ Autowired
145
+ private HttpServletRequest request ;
146
+
147
+
148
+ @ RequestMapping ("/requestScoped" )
91
149
public void handle () {
92
- RequestAttributes attributes = RequestContextHolder .getRequestAttributes ();
93
- assertNull (attributes .getAttribute ("foo1" , RequestAttributes .SCOPE_REQUEST ));
94
- assertNotNull (attributes .getAttribute ("foo2" , RequestAttributes .SCOPE_REQUEST ));
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 ()));
153
+ assertRequestAttributes ();
95
154
}
96
155
}
97
156
0 commit comments