use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class MessageStoreInterceptorTest method testAutomatic.
public void testAutomatic() throws Exception {
MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
interceptor.setAllowRequestParameterSwitch(true);
interceptor.setOperationMode(MessageStoreInterceptor.AUTOMATIC_MODE);
Map<String, Object> sessionMap = new LinkedHashMap<>();
ActionSupport action = new ActionSupport();
action.addActionError("some action error 1");
action.addActionError("some action error 2");
action.addActionMessage("some action message 1");
action.addActionMessage("some action message 2");
action.addFieldError("field1", "some field error 1");
action.addFieldError("field2", "some field error 2");
ActionContext actionContext = ActionContext.of(new HashMap<>()).bind();
actionContext.setParameters(HttpParameters.create().build());
actionContext.setSession(sessionMap);
HttpSession mockedSession = EasyMock.createControl().createMock(HttpSession.class);
HttpServletRequest mockedRequest = EasyMock.createControl().createMock(HttpServletRequest.class);
mockedRequest.getSession(false);
EasyMock.expectLastCall().andReturn(mockedSession);
EasyMock.expectLastCall().once();
ServletActionContext.setRequest(mockedRequest);
EasyMock.replay(mockedRequest);
// Mock (ActionInvocation)
ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class);
mockActionInvocation.getInvocationContext();
EasyMock.expectLastCall().andReturn(actionContext);
EasyMock.expectLastCall().anyTimes();
mockActionInvocation.addPreResultListener(EasyMock.anyObject());
EasyMock.expectLastCall();
mockActionInvocation.invoke();
EasyMock.expectLastCall().andReturn(Action.SUCCESS);
mockActionInvocation.getAction();
EasyMock.expectLastCall().andReturn(action);
EasyMock.expectLastCall().anyTimes();
EasyMock.replay(mockActionInvocation);
interceptor.init();
interceptor.intercept(mockActionInvocation);
interceptor.destroy();
EasyMock.verify(mockActionInvocation);
}
use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class DispatcherTest method testInterceptorDestroy.
public void testInterceptorDestroy() throws Exception {
Mock mockInterceptor = new Mock(Interceptor.class);
mockInterceptor.matchAndReturn("hashCode", 0);
mockInterceptor.expect("destroy");
InterceptorMapping interceptorMapping = new InterceptorMapping("test", (Interceptor) mockInterceptor.proxy());
InterceptorStackConfig isc = new InterceptorStackConfig.Builder("test").addInterceptor(interceptorMapping).build();
PackageConfig packageConfig = new PackageConfig.Builder("test").addInterceptorStackConfig(isc).build();
Map<String, PackageConfig> packageConfigs = new HashMap<String, PackageConfig>();
packageConfigs.put("test", packageConfig);
Mock mockContainer = new Mock(Container.class);
mockContainer.matchAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), new ObjectFactory());
String reloadConfigs = container.getInstance(String.class, StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD);
mockContainer.expectAndReturn("getInstance", C.args(C.eq(String.class), C.eq(StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD)), reloadConfigs);
Mock mockConfiguration = new Mock(Configuration.class);
mockConfiguration.matchAndReturn("getPackageConfigs", packageConfigs);
mockConfiguration.matchAndReturn("getContainer", mockContainer.proxy());
mockConfiguration.expect("destroy");
ConfigurationManager configurationManager = new ConfigurationManager(Container.DEFAULT_NAME);
configurationManager.setConfiguration((Configuration) mockConfiguration.proxy());
Dispatcher dispatcher = new MockDispatcher(new MockServletContext(), new HashMap<String, String>(), configurationManager);
dispatcher.init();
dispatcher.cleanup();
mockInterceptor.verify();
mockContainer.verify();
mockConfiguration.verify();
}
use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class ClearSessionInterceptorTest method testCreateSession.
public void testCreateSession() throws Exception {
ClearSessionInterceptor interceptor = new ClearSessionInterceptor();
MockActionInvocation invocation = new MockActionInvocation();
ActionContext context = ActionContext.of(new HashMap<>()).bind();
Map<String, Object> session = new HashMap<>();
session.put("Test1", "Test1");
session.put("Test2", "Test2");
session.put("Test3", "Test3");
context.setSession(session);
invocation.setInvocationContext(context);
interceptor.intercept(invocation);
assertEquals(0, session.size());
}
use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class CoepInterceptorTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
container.inject(interceptor);
interceptor.setExemptedPaths("/foo");
ServletActionContext.setRequest(request);
ServletActionContext.setResponse(response);
ActionContext context = ServletActionContext.getActionContext();
Map<String, Object> session = new HashMap<>();
context.withSession(session);
mai.setInvocationContext(context);
}
use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class CookieInterceptorTest method testCookiesWithClassPollution.
public void testCookiesWithClassPollution() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
String pollution1 = "model['class']['classLoader']['jarPath']";
String pollution2 = "model.class.classLoader.jarPath";
String pollution3 = "class.classLoader.jarPath";
String pollution4 = "class['classLoader']['jarPath']";
String pollution5 = "model[\"class\"]['classLoader']['jarPath']";
String pollution6 = "class[\"classLoader\"]['jarPath']";
request.setCookies(new Cookie(pollution1, "pollution1"), new Cookie("pollution1", pollution1), new Cookie(pollution2, "pollution2"), new Cookie("pollution2", pollution2), new Cookie(pollution3, "pollution3"), new Cookie("pollution3", pollution3), new Cookie(pollution4, "pollution4"), new Cookie("pollution4", pollution4), new Cookie(pollution5, "pollution5"), new Cookie("pollution5", pollution5), new Cookie(pollution6, "pollution6"), new Cookie("pollution6", pollution6));
ServletActionContext.setRequest(request);
final Map<String, Boolean> excludedName = new HashMap<String, Boolean>();
CookieInterceptor interceptor = new CookieInterceptor() {
@Override
protected boolean isAcceptableName(String name) {
boolean accepted = super.isAcceptableName(name);
excludedName.put(name, accepted);
return accepted;
}
};
DefaultExcludedPatternsChecker excludedPatternsChecker = new DefaultExcludedPatternsChecker();
excludedPatternsChecker.setAdditionalExcludePatterns(".*(^|\\.|\\[|'|\")class(\\.|\\[|'|\").*");
interceptor.setExcludedPatternsChecker(excludedPatternsChecker);
interceptor.setAcceptedPatternsChecker(new DefaultAcceptedPatternsChecker());
interceptor.setCookiesName("*");
MockActionInvocation invocation = new MockActionInvocation();
invocation.setAction(new MockActionWithCookieAware());
interceptor.intercept(invocation);
assertFalse(excludedName.get(pollution1));
assertFalse(excludedName.get(pollution2));
assertFalse(excludedName.get(pollution3));
assertFalse(excludedName.get(pollution4));
assertFalse(excludedName.get(pollution5));
assertFalse(excludedName.get(pollution6));
}
Aggregations