25
25
import org .apache .struts2 .components .template .Template ;
26
26
import org .apache .struts2 .components .template .TemplateEngine ;
27
27
import org .apache .struts2 .components .template .TemplateEngineManager ;
28
+ import org .apache .struts2 .dispatcher .SessionMap ;
28
29
import org .apache .struts2 .dispatcher .StaticContentLoader ;
29
30
import org .springframework .mock .web .MockHttpServletRequest ;
30
31
import org .springframework .mock .web .MockHttpServletResponse ;
32
+ import org .springframework .mock .web .MockHttpSession ;
31
33
32
34
import java .util .Collections ;
33
- import java .util .HashMap ;
34
35
import java .util .Map ;
35
36
36
37
import static com .opensymphony .xwork2 .security .DefaultNotExcludedAcceptedPatternsCheckerTest .NO_EXCLUSION_ACCEPT_ALL_PATTERNS_CHECKER ;
@@ -160,7 +161,7 @@ public TemplateEngine getTemplateEngine(Template template, String templateTypeOv
160
161
try {
161
162
txtFld .mergeTemplate (null , new Template (null , null , null ));
162
163
fail ("Exception not thrown" );
163
- } catch (final Exception e ){
164
+ } catch (final Exception e ) {
164
165
assertTrue (e instanceof ConfigurationException );
165
166
}
166
167
}
@@ -225,6 +226,7 @@ public void testSetAccesskey() {
225
226
ValueStack stack = ActionContext .getContext ().getValueStack ();
226
227
MockHttpServletRequest req = new MockHttpServletRequest ();
227
228
MockHttpServletResponse res = new MockHttpServletResponse ();
229
+ ActionContext .getContext ().withServletRequest (req );
228
230
229
231
TextField txtFld = new TextField (stack , req , res );
230
232
txtFld .setAccesskey (accesskeyValue );
@@ -238,6 +240,7 @@ public void testValueParameterEvaluation() {
238
240
ValueStack stack = ActionContext .getContext ().getValueStack ();
239
241
MockHttpServletRequest req = new MockHttpServletRequest ();
240
242
MockHttpServletResponse res = new MockHttpServletResponse ();
243
+ ActionContext .getContext ().withServletRequest (req );
241
244
242
245
TextField txtFld = new TextField (stack , req , res );
243
246
txtFld .addParameter ("value" , value );
@@ -250,11 +253,13 @@ public void testValueParameterRecursion() {
250
253
ValueStack stack = ActionContext .getContext ().getValueStack ();
251
254
MockHttpServletRequest req = new MockHttpServletRequest ();
252
255
MockHttpServletResponse res = new MockHttpServletResponse ();
256
+ ActionContext .getContext ().withServletRequest (req );
253
257
254
258
stack .push (new Object () {
255
259
public String getMyValue () {
256
260
return "%{myBad}" ;
257
261
}
262
+
258
263
public String getMyBad () {
259
264
throw new IllegalStateException ("Recursion detected!" );
260
265
}
@@ -273,11 +278,13 @@ public void testValueNameParameterNotAccepted() {
273
278
ValueStack stack = ActionContext .getContext ().getValueStack ();
274
279
MockHttpServletRequest req = new MockHttpServletRequest ();
275
280
MockHttpServletResponse res = new MockHttpServletResponse ();
281
+ ActionContext .getContext ().withServletRequest (req );
276
282
277
283
stack .push (new Object () {
278
284
public String getMyValueName () {
279
285
return "getMyValue()" ;
280
286
}
287
+
281
288
public String getMyValue () {
282
289
return "value" ;
283
290
}
@@ -300,6 +307,7 @@ public void testValueNameParameterGetterAccepted() {
300
307
ValueStack stack = ActionContext .getContext ().getValueStack ();
301
308
MockHttpServletRequest req = new MockHttpServletRequest ();
302
309
MockHttpServletResponse res = new MockHttpServletResponse ();
310
+ ActionContext .getContext ().withServletRequest (req );
303
311
304
312
stack .push (new Object () {
305
313
public String getMyValue () {
@@ -320,6 +328,7 @@ public void testSetClass() {
320
328
ValueStack stack = ActionContext .getContext ().getValueStack ();
321
329
MockHttpServletRequest req = new MockHttpServletRequest ();
322
330
MockHttpServletResponse res = new MockHttpServletResponse ();
331
+ ActionContext .getContext ().withServletRequest (req );
323
332
324
333
TextField txtFld = new TextField (stack , req , res );
325
334
txtFld .setCssClass (cssClass );
@@ -333,6 +342,7 @@ public void testSetStyle() {
333
342
ValueStack stack = ActionContext .getContext ().getValueStack ();
334
343
MockHttpServletRequest req = new MockHttpServletRequest ();
335
344
MockHttpServletResponse res = new MockHttpServletResponse ();
345
+ ActionContext .getContext ().withServletRequest (req );
336
346
337
347
TextField txtFld = new TextField (stack , req , res );
338
348
txtFld .setStyle (cssStyle );
@@ -347,16 +357,39 @@ public void testNonce() {
347
357
MockHttpServletRequest req = new MockHttpServletRequest ();
348
358
MockHttpServletResponse res = new MockHttpServletResponse ();
349
359
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 ));
353
366
354
367
DoubleSelect dblSelect = new DoubleSelect (stack , req , res );
355
368
dblSelect .evaluateParams ();
356
369
357
370
assertEquals (nonceVal , dblSelect .getParameters ().get ("nonce" ));
358
371
}
359
372
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
+
360
393
public void testSetNullUiStaticContentPath () {
361
394
// given
362
395
ValueStack stack = ActionContext .getContext ().getValueStack ();
0 commit comments