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);
}
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));
}
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));
}
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);
}
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);
}
Aggregations