Search in sources :

Example 71 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class CookieInterceptorTest method testInterceptAll2.

public void testInterceptAll2() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(new Cookie("cookie1", "cookie1value"), new Cookie("cookie2", "cookie2value"), new Cookie("cookie3", "cookie3value"));
    ServletActionContext.setRequest(request);
    MockActionWithCookieAware action = new MockActionWithCookieAware();
    ActionContext.getContext().getValueStack().push(action);
    ActionInvocation invocation = (ActionInvocation) createMock(ActionInvocation.class);
    expect(invocation.getAction()).andReturn(action);
    expect(invocation.invoke()).andReturn(Action.SUCCESS);
    replay(invocation);
    CookieInterceptor interceptor = new CookieInterceptor();
    interceptor.setExcludedPatternsChecker(new DefaultExcludedPatternsChecker());
    interceptor.setAcceptedPatternsChecker(new DefaultAcceptedPatternsChecker());
    interceptor.setCookiesName("cookie1, cookie2, cookie3");
    interceptor.setCookiesValue("cookie1value, cookie2value, cookie3value");
    interceptor.intercept(invocation);
    assertFalse(action.getCookiesMap().isEmpty());
    assertEquals(action.getCookiesMap().size(), 3);
    assertEquals(action.getCookiesMap().get("cookie1"), "cookie1value");
    assertEquals(action.getCookiesMap().get("cookie2"), "cookie2value");
    assertEquals(action.getCookiesMap().get("cookie3"), "cookie3value");
    assertEquals(action.getCookie1(), "cookie1value");
    assertEquals(action.getCookie2(), "cookie2value");
    assertEquals(action.getCookie3(), "cookie3value");
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie1"), "cookie1value");
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie2"), "cookie2value");
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie3"), "cookie3value");
    verify(invocation);
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) DefaultAcceptedPatternsChecker(com.opensymphony.xwork2.security.DefaultAcceptedPatternsChecker) DefaultExcludedPatternsChecker(com.opensymphony.xwork2.security.DefaultExcludedPatternsChecker)

Example 72 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class CookieInterceptorTest method testCookiesWithStrutsInternalsAccess.

public void testCookiesWithStrutsInternalsAccess() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    String sessionCookieName = "session.userId";
    String sessionCookieValue = "session.userId=1";
    String appCookieName = "application.userId";
    String appCookieValue = "application.userId=1";
    String reqCookieName = "request.userId";
    String reqCookieValue = "request.userId=1";
    request.setCookies(new Cookie(sessionCookieName, "1"), new Cookie("1", sessionCookieValue), new Cookie(appCookieName, "1"), new Cookie("1", appCookieValue), new Cookie(reqCookieName, "1"), new Cookie("1", reqCookieValue));
    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;
        }
    };
    interceptor.setExcludedPatternsChecker(new DefaultExcludedPatternsChecker());
    interceptor.setAcceptedPatternsChecker(new DefaultAcceptedPatternsChecker());
    interceptor.setCookiesName("*");
    MockActionInvocation invocation = new MockActionInvocation();
    invocation.setAction(new MockActionWithCookieAware());
    interceptor.intercept(invocation);
    assertFalse(excludedName.get(sessionCookieName));
    assertFalse(excludedName.get(appCookieName));
    assertFalse(excludedName.get(reqCookieName));
}
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)

Example 73 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class CookieInterceptorTest method testActionCookieAwareWithStrutsInternalsAccess.

public void testActionCookieAwareWithStrutsInternalsAccess() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    String sessionCookieName = "session.userId";
    String sessionCookieValue = "session.userId=1";
    String appCookieName = "application.userId";
    String appCookieValue = "application.userId=1";
    String reqCookieName = "request.userId";
    String reqCookieValue = "request.userId=1";
    request.setCookies(new Cookie(sessionCookieName, "1"), new Cookie("1", sessionCookieValue), new Cookie(appCookieName, "1"), new Cookie("1", appCookieValue), new Cookie(reqCookieName, "1"), new Cookie("1", reqCookieValue));
    ServletActionContext.setRequest(request);
    final Map<String, Boolean> excludedName = new HashMap<>();
    CookieInterceptor interceptor = new CookieInterceptor() {

        @Override
        protected boolean isAcceptableName(String name) {
            boolean accepted = super.isAcceptableName(name);
            excludedName.put(name, accepted);
            return accepted;
        }
    };
    interceptor.setExcludedPatternsChecker(new DefaultExcludedPatternsChecker());
    interceptor.setAcceptedPatternsChecker(new DefaultAcceptedPatternsChecker());
    interceptor.setCookiesName("*");
    MockActionInvocation invocation = new MockActionInvocation();
    invocation.setAction(new MockActionWithActionCookieAware());
    interceptor.intercept(invocation);
    assertFalse(excludedName.get(sessionCookieName));
    assertFalse(excludedName.get(appCookieName));
    assertFalse(excludedName.get(reqCookieName));
}
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)

Example 74 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class CoopInterceptorTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    container.inject(interceptor);
    interceptor.setExemptedPaths("/foo,/bar");
    ServletActionContext.setRequest(request);
    ServletActionContext.setResponse(response);
    ActionContext context = ServletActionContext.getActionContext();
    mai.setInvocationContext(context);
}
Also used : ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext)

Example 75 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class MessageStorePreResultListenerTest method testAutomatic.

public void testAutomatic() {
    MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
    interceptor.setOperationMode(MessageStoreInterceptor.AUTOMATIC_MODE);
    MessageStorePreResultListener listener = new MessageStorePreResultListener();
    listener.init(interceptor);
    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);
    HttpServletResponse mockedResponse = EasyMock.createControl().createMock(HttpServletResponse.class);
    mockedResponse.isCommitted();
    EasyMock.expectLastCall().andReturn(false);
    EasyMock.expectLastCall().once();
    ServletActionContext.setResponse(mockedResponse);
    EasyMock.replay(mockedResponse);
    // Mock (ActionInvocation)
    ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class);
    mockActionInvocation.getInvocationContext();
    EasyMock.expectLastCall().andReturn(actionContext);
    EasyMock.expectLastCall().anyTimes();
    mockActionInvocation.getAction();
    EasyMock.expectLastCall().andReturn(action);
    EasyMock.expectLastCall().anyTimes();
    mockActionInvocation.getProxy();
    MockActionProxy actionProxy = new MockActionProxy();
    ResultConfig resultConfig = new ResultConfig.Builder(Action.SUCCESS, ServletRedirectResult.class.getName()).build();
    ActionConfig actionConfig = new ActionConfig.Builder("", "test", action.getClass().getName()).addResultConfig(resultConfig).build();
    actionProxy.setConfig(actionConfig);
    EasyMock.expectLastCall().andReturn(actionProxy);
    EasyMock.expectLastCall().anyTimes();
    EasyMock.replay(mockActionInvocation);
    interceptor.init();
    listener.beforeResult(mockActionInvocation, Action.SUCCESS);
    List actionErrors = (List) sessionMap.get(MessageStoreInterceptor.actionErrorsSessionKey);
    List actionMessages = (List) sessionMap.get(MessageStoreInterceptor.actionMessagesSessionKey);
    Map fieldErrors = (Map) sessionMap.get(MessageStoreInterceptor.fieldErrorsSessionKey);
    assertEquals(actionErrors.size(), 2);
    assertEquals(actionMessages.size(), 2);
    assertEquals(fieldErrors.size(), 2);
    assertTrue(actionErrors.contains("some action error 1"));
    assertTrue(actionErrors.contains("some action error 2"));
    assertTrue(actionMessages.contains("some action message 1"));
    assertTrue(actionMessages.contains("some action message 2"));
    assertEquals(((List) fieldErrors.get("field1")).size(), 1);
    assertEquals(((List) fieldErrors.get("field2")).size(), 1);
    assertEquals(((List) fieldErrors.get("field1")).get(0), "some field error 1");
    assertEquals(((List) fieldErrors.get("field2")).get(0), "some field error 2");
    EasyMock.verify(mockActionInvocation);
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpSession(javax.servlet.http.HttpSession) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ActionSupport(com.opensymphony.xwork2.ActionSupport) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext) LinkedHashMap(java.util.LinkedHashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MockActionProxy(com.opensymphony.xwork2.mock.MockActionProxy)

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