Search in sources :

Example 31 with Interceptor

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpSession(javax.servlet.http.HttpSession) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ActionSupport(com.opensymphony.xwork2.ActionSupport) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext) LinkedHashMap(java.util.LinkedHashMap)

Example 32 with Interceptor

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();
}
Also used : HashMap(java.util.HashMap) Mock(com.mockobjects.dynamic.Mock) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) MockServletContext(org.springframework.mock.web.MockServletContext) InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) ObjectFactory(com.opensymphony.xwork2.ObjectFactory) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) ConfigurationManager(com.opensymphony.xwork2.config.ConfigurationManager)

Example 33 with Interceptor

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());
}
Also used : HashMap(java.util.HashMap) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) ActionContext(com.opensymphony.xwork2.ActionContext)

Example 34 with Interceptor

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);
}
Also used : HashMap(java.util.HashMap) ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext)

Example 35 with Interceptor

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));
}
Also used : Cookie(javax.servlet.http.Cookie) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultAcceptedPatternsChecker(com.opensymphony.xwork2.security.DefaultAcceptedPatternsChecker) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) DefaultExcludedPatternsChecker(com.opensymphony.xwork2.security.DefaultExcludedPatternsChecker)

Aggregations

ActionInvocation (com.opensymphony.xwork2.ActionInvocation)32 HashMap (java.util.HashMap)28 ActionContext (com.opensymphony.xwork2.ActionContext)20 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)19 MockActionInvocation (com.opensymphony.xwork2.mock.MockActionInvocation)19 ServletActionContext (org.apache.struts2.ServletActionContext)14 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)13 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)12 LinkedHashMap (java.util.LinkedHashMap)12 Map (java.util.Map)11 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)11 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)10 InterceptorConfig (com.opensymphony.xwork2.config.entities.InterceptorConfig)10 DefaultAcceptedPatternsChecker (com.opensymphony.xwork2.security.DefaultAcceptedPatternsChecker)10 DefaultExcludedPatternsChecker (com.opensymphony.xwork2.security.DefaultExcludedPatternsChecker)10 Cookie (javax.servlet.http.Cookie)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 Mock (com.mockobjects.dynamic.Mock)8 ActionSupport (com.opensymphony.xwork2.ActionSupport)8