2525import org .apache .struts2 .components .template .Template ;
2626import org .apache .struts2 .components .template .TemplateEngine ;
2727import org .apache .struts2 .components .template .TemplateEngineManager ;
28+ import org .apache .struts2 .dispatcher .SessionMap ;
2829import org .apache .struts2 .dispatcher .StaticContentLoader ;
2930import org .springframework .mock .web .MockHttpServletRequest ;
3031import org .springframework .mock .web .MockHttpServletResponse ;
32+ import org .springframework .mock .web .MockHttpSession ;
3133
3234import java .util .Collections ;
33- import java .util .HashMap ;
3435import java .util .Map ;
3536
3637import static com .opensymphony .xwork2 .security .DefaultNotExcludedAcceptedPatternsCheckerTest .NO_EXCLUSION_ACCEPT_ALL_PATTERNS_CHECKER ;
@@ -160,7 +161,7 @@ public TemplateEngine getTemplateEngine(Template template, String templateTypeOv
160161 try {
161162 txtFld .mergeTemplate (null , new Template (null , null , null ));
162163 fail ("Exception not thrown" );
163- } catch (final Exception e ){
164+ } catch (final Exception e ) {
164165 assertTrue (e instanceof ConfigurationException );
165166 }
166167 }
@@ -225,6 +226,7 @@ public void testSetAccesskey() {
225226 ValueStack stack = ActionContext .getContext ().getValueStack ();
226227 MockHttpServletRequest req = new MockHttpServletRequest ();
227228 MockHttpServletResponse res = new MockHttpServletResponse ();
229+ ActionContext .getContext ().withServletRequest (req );
228230
229231 TextField txtFld = new TextField (stack , req , res );
230232 txtFld .setAccesskey (accesskeyValue );
@@ -238,6 +240,7 @@ public void testValueParameterEvaluation() {
238240 ValueStack stack = ActionContext .getContext ().getValueStack ();
239241 MockHttpServletRequest req = new MockHttpServletRequest ();
240242 MockHttpServletResponse res = new MockHttpServletResponse ();
243+ ActionContext .getContext ().withServletRequest (req );
241244
242245 TextField txtFld = new TextField (stack , req , res );
243246 txtFld .addParameter ("value" , value );
@@ -250,11 +253,13 @@ public void testValueParameterRecursion() {
250253 ValueStack stack = ActionContext .getContext ().getValueStack ();
251254 MockHttpServletRequest req = new MockHttpServletRequest ();
252255 MockHttpServletResponse res = new MockHttpServletResponse ();
256+ ActionContext .getContext ().withServletRequest (req );
253257
254258 stack .push (new Object () {
255259 public String getMyValue () {
256260 return "%{myBad}" ;
257261 }
262+
258263 public String getMyBad () {
259264 throw new IllegalStateException ("Recursion detected!" );
260265 }
@@ -273,11 +278,13 @@ public void testValueNameParameterNotAccepted() {
273278 ValueStack stack = ActionContext .getContext ().getValueStack ();
274279 MockHttpServletRequest req = new MockHttpServletRequest ();
275280 MockHttpServletResponse res = new MockHttpServletResponse ();
281+ ActionContext .getContext ().withServletRequest (req );
276282
277283 stack .push (new Object () {
278284 public String getMyValueName () {
279285 return "getMyValue()" ;
280286 }
287+
281288 public String getMyValue () {
282289 return "value" ;
283290 }
@@ -300,6 +307,7 @@ public void testValueNameParameterGetterAccepted() {
300307 ValueStack stack = ActionContext .getContext ().getValueStack ();
301308 MockHttpServletRequest req = new MockHttpServletRequest ();
302309 MockHttpServletResponse res = new MockHttpServletResponse ();
310+ ActionContext .getContext ().withServletRequest (req );
303311
304312 stack .push (new Object () {
305313 public String getMyValue () {
@@ -320,6 +328,7 @@ public void testSetClass() {
320328 ValueStack stack = ActionContext .getContext ().getValueStack ();
321329 MockHttpServletRequest req = new MockHttpServletRequest ();
322330 MockHttpServletResponse res = new MockHttpServletResponse ();
331+ ActionContext .getContext ().withServletRequest (req );
323332
324333 TextField txtFld = new TextField (stack , req , res );
325334 txtFld .setCssClass (cssClass );
@@ -333,6 +342,7 @@ public void testSetStyle() {
333342 ValueStack stack = ActionContext .getContext ().getValueStack ();
334343 MockHttpServletRequest req = new MockHttpServletRequest ();
335344 MockHttpServletResponse res = new MockHttpServletResponse ();
345+ ActionContext .getContext ().withServletRequest (req );
336346
337347 TextField txtFld = new TextField (stack , req , res );
338348 txtFld .setStyle (cssStyle );
@@ -347,16 +357,39 @@ public void testNonce() {
347357 MockHttpServletRequest req = new MockHttpServletRequest ();
348358 MockHttpServletResponse res = new MockHttpServletResponse ();
349359 ActionContext actionContext = stack .getActionContext ();
350- Map <String , Object > session = new HashMap <>();
351- session .put ("nonce" , nonceVal );
352- actionContext .withSession (session );
360+ actionContext .withServletRequest (req );
361+ MockHttpSession session = new MockHttpSession ();
362+ session .putValue ("nonce" , nonceVal );
363+ req .setSession (session );
364+
365+ actionContext .withSession (new SessionMap (req ));
353366
354367 DoubleSelect dblSelect = new DoubleSelect (stack , req , res );
355368 dblSelect .evaluateParams ();
356369
357370 assertEquals (nonceVal , dblSelect .getParameters ().get ("nonce" ));
358371 }
359372
373+ public void testNonceOfInvalidSession () {
374+ String nonceVal = "r4nd0m" ;
375+ ValueStack stack = ActionContext .getContext ().getValueStack ();
376+ MockHttpServletRequest req = new MockHttpServletRequest ();
377+ MockHttpServletResponse res = new MockHttpServletResponse ();
378+ ActionContext actionContext = stack .getActionContext ();
379+ actionContext .withServletRequest (req );
380+ MockHttpSession session = new MockHttpSession ();
381+ session .putValue ("nonce" , nonceVal );
382+ req .setSession (session );
383+ actionContext .withSession (new SessionMap (req ));
384+
385+ session .invalidate ();
386+
387+ DoubleSelect dblSelect = new DoubleSelect (stack , req , res );
388+ dblSelect .evaluateParams ();
389+
390+ assertNull (dblSelect .getParameters ().get ("nonce" ));
391+ }
392+
360393 public void testSetNullUiStaticContentPath () {
361394 // given
362395 ValueStack stack = ActionContext .getContext ().getValueStack ();
0 commit comments