11/* 
2-  * Copyright 2002-2014  the original author or authors. 
2+  * Copyright 2002-2015  the original author or authors. 
33 * 
44 * Licensed under the Apache License, Version 2.0 (the "License"); 
55 * you may not use this file except in compliance with the License. 
1616
1717package  org .springframework .test .web .servlet .samples .spr ;
1818
19+ import  javax .servlet .http .HttpServletRequest ;
20+ 
1921import  org .junit .Before ;
2022import  org .junit .Test ;
2123import  org .junit .runner .RunWith ;
2224
25+ import  org .springframework .aop .support .AopUtils ;
2326import  org .springframework .beans .factory .annotation .Autowired ;
2427import  org .springframework .context .annotation .Bean ;
2528import  org .springframework .context .annotation .Configuration ;
29+ import  org .springframework .context .annotation .Scope ;
30+ import  org .springframework .context .annotation .ScopedProxyMode ;
2631import  org .springframework .mock .web .MockHttpServletRequest ;
27- import  org .springframework .stereotype . Controller ;
32+ import  org .springframework .test . annotation . DirtiesContext ;
2833import  org .springframework .test .context .ContextConfiguration ;
2934import  org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
3035import  org .springframework .test .context .web .WebAppConfiguration ;
3136import  org .springframework .test .web .servlet .MockMvc ;
3237import  org .springframework .web .bind .annotation .RequestMapping ;
33- import  org .springframework .web .bind .annotation .ResponseBody ;
38+ import  org .springframework .web .bind .annotation .RestController ;
3439import  org .springframework .web .context .WebApplicationContext ;
3540import  org .springframework .web .context .request .RequestAttributes ;
3641import  org .springframework .web .context .request .RequestContextHolder ;
3742import  org .springframework .web .servlet .config .annotation .EnableWebMvc ;
3843import  org .springframework .web .servlet .config .annotation .WebMvcConfigurerAdapter ;
3944
45+ import  static  org .hamcrest .CoreMatchers .*;
4046import  static  org .junit .Assert .*;
4147import  static  org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
4248import  static  org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
4349import  static  org .springframework .test .web .servlet .setup .MockMvcBuilders .*;
50+ import  static  org .springframework .web .context .request .RequestAttributes .*;
4451
4552/** 
4653 * Test for SPR-10025 (access to request attributes via RequestContextHolder). 
4754 * 
4855 * @author Rossen Stoyanchev 
56+  * @author Sam Brannen 
4957 */ 
5058@ RunWith (SpringJUnit4ClassRunner .class )
5159@ WebAppConfiguration 
5260@ ContextConfiguration 
61+ @ DirtiesContext 
5362public  class  RequestContextHolderTests  {
5463
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+ 
5571	@ Autowired 
5672	private  WebApplicationContext  wac ;
5773
5874	@ Autowired 
59- 	private  MockHttpServletRequest  servletRequest ;
75+ 	private  MockHttpServletRequest  mockRequest ;
76+ 
77+ 	@ Autowired 
78+ 	private  MyScopedController  myScopedController ;
6079
6180	private  MockMvc  mockMvc ;
6281
6382
6483	@ Before 
6584	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 ();
6791	}
6892
6993	@ 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 ));
73102	}
74103
75104
@@ -81,17 +110,47 @@ static class WebConfig extends WebMvcConfigurerAdapter {
81110		public  MyController  myController () {
82111			return  new  MyController ();
83112		}
113+ 
114+ 		@ Bean 
115+ 		@ Scope (name  = "request" , proxyMode  = ScopedProxyMode .TARGET_CLASS )
116+ 		public  MyScopedController  myScopedController () {
117+ 			return  new  MyScopedController ();
118+ 		}
84119	}
85120
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 
87133	private  static  class  MyController  {
88134
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" )
91149		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 ();
95154		}
96155	}
97156
0 commit comments