Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WW-5486 Fixes exposing params added by ServletDispatcherResult #1123

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/src/main/java/org/apache/struts2/ActionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import jakarta.servlet.jsp.PageContext;
import org.apache.struts2.action.Action;
import org.apache.struts2.conversion.impl.ConversionData;
import org.apache.struts2.dispatcher.DispatcherConstants;
import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.inject.Container;
Expand Down Expand Up @@ -316,6 +317,7 @@ public String getActionName() {
*/
public ActionContext withParameters(HttpParameters parameters) {
put(PARAMETERS, parameters);
put(DispatcherConstants.PARAMETERS, parameters);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,6 @@ public Map<String, Object> createContextMap(Map<String, Object> requestMap,
.with(DispatcherConstants.REQUEST, requestMap)
.with(DispatcherConstants.SESSION, sessionMap)
.with(DispatcherConstants.APPLICATION, applicationMap)
.with(DispatcherConstants.PARAMETERS, parameters)
.getContextMap();

AttributeMap attrMap = new AttributeMap(extraContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,119 +18,80 @@
*/
package org.apache.struts2.result;

import com.mockobjects.dynamic.C;
import com.mockobjects.dynamic.Mock;
import org.apache.struts2.ActionContext;
import org.apache.struts2.mock.MockActionInvocation;
import org.apache.struts2.util.ValueStackFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsInternalTestCase;
import org.apache.struts2.StrutsStatics;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.struts2.mock.MockActionInvocation;
import org.apache.struts2.util.ValueStack;
import org.apache.struts2.util.ValueStackFactory;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

public class ServletDispatcherResultTest extends StrutsInternalTestCase implements StrutsStatics {

public void testInclude() {
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private MockActionInvocation invocation;
private ValueStack stack;

public void testForward() throws Exception {
ServletDispatcherResult view = new ServletDispatcherResult();
view.setLocation("foo.jsp");

Mock dispatcherMock = new Mock(RequestDispatcher.class);
dispatcherMock.expect("include", C.ANY_ARGS);

Mock requestMock = new Mock(HttpServletRequest.class);
requestMock.expectAndReturn("getAttribute", "struts.actiontag.invocation", null);
requestMock.expectAndReturn("getRequestDispatcher", C.args(C.eq("foo.jsp")), dispatcherMock.proxy());

Mock responseMock = new Mock(HttpServletResponse.class);
responseMock.expectAndReturn("isCommitted", Boolean.TRUE);
request.setAttribute("struts.actiontag.invocation", null);
request.setAttribute("jakarta.servlet.include.servlet_path", null);
request.setRequestURI("foo.jsp");

ServletActionContext.setRequest((HttpServletRequest) requestMock.proxy());
ServletActionContext.setResponse((HttpServletResponse) responseMock.proxy());
response.setCommitted(Boolean.FALSE);

try {
view.execute(null);
} catch (Exception e) {
e.printStackTrace();
fail();
}
view.execute(invocation);

dispatcherMock.verify();
requestMock.verify();
dispatcherMock.verify();
assertEquals("foo.jsp", response.getForwardedUrl());
}

public void testSimple() {
public void testInclude() throws Exception {
ServletDispatcherResult view = new ServletDispatcherResult();
view.setLocation("foo.jsp");

Mock dispatcherMock = new Mock(RequestDispatcher.class);
dispatcherMock.expect("forward", C.ANY_ARGS);

Mock requestMock = new Mock(HttpServletRequest.class);
requestMock.expectAndReturn("getAttribute", "struts.actiontag.invocation", null);
requestMock.expectAndReturn("getAttribute", "jakarta.servlet.include.servlet_path", null);
requestMock.expectAndReturn("getRequestDispatcher", C.args(C.eq("foo.jsp")), dispatcherMock.proxy());
requestMock.expect("setAttribute", C.ANY_ARGS); // this is a bad mock, but it works
requestMock.expect("setAttribute", C.ANY_ARGS); // this is a bad mock, but it works
requestMock.matchAndReturn("getRequestURI", "foo.jsp");

Mock responseMock = new Mock(HttpServletResponse.class);
responseMock.expectAndReturn("isCommitted", Boolean.FALSE);

ServletActionContext.setRequest((HttpServletRequest) requestMock.proxy());
ServletActionContext.setResponse((HttpServletResponse) responseMock.proxy());

try {
view.execute(null);
} catch (Exception e) {
e.printStackTrace();
fail();
}

dispatcherMock.verify();
requestMock.verify();
dispatcherMock.verify();
request.setAttribute("struts.actiontag.invocation", null);
response.setCommitted(Boolean.TRUE);
request.setRequestURI("foo.jsp");

view.execute(invocation);

assertEquals("foo.jsp", response.getIncludedUrl());
}

public void testWithParameter() {
public void testWithParameter() throws Exception {
ServletDispatcherResult view = container.inject(ServletDispatcherResult.class);
view.setLocation("foo.jsp?bar=1");

Mock dispatcherMock = new Mock(RequestDispatcher.class);
dispatcherMock.expect("forward", C.ANY_ARGS);

Mock requestMock = new Mock(HttpServletRequest.class);
requestMock.expectAndReturn("getAttribute", "struts.actiontag.invocation", null);
requestMock.expectAndReturn("getAttribute", "jakarta.servlet.include.servlet_path", null);
requestMock.expectAndReturn("getRequestDispatcher", C.args(C.eq("foo.jsp?bar=1")), dispatcherMock.proxy());
requestMock.expect("setAttribute", C.ANY_ARGS); // this is a bad mock, but it works
requestMock.expect("setAttribute", C.ANY_ARGS); // this is a bad mock, but it works
requestMock.matchAndReturn("getRequestURI", "foo.jsp");

Mock responseMock = new Mock(HttpServletResponse.class);
responseMock.expectAndReturn("isCommitted", Boolean.FALSE);

ServletActionContext.setRequest((HttpServletRequest) requestMock.proxy());
ServletActionContext.setResponse((HttpServletResponse) responseMock.proxy());

MockActionInvocation mockActionInvocation = new MockActionInvocation();
mockActionInvocation.setInvocationContext(ActionContext.getContext());
mockActionInvocation.setStack(container.getInstance(ValueStackFactory.class).createValueStack());

try {
view.execute(mockActionInvocation);
} catch (Exception e) {
e.printStackTrace();
fail();
}

assertTrue(mockActionInvocation.getInvocationContext().getParameters().contains("bar"));
assertEquals("1", mockActionInvocation.getInvocationContext().getParameters().get("bar").getValue());
dispatcherMock.verify();
requestMock.verify();
dispatcherMock.verify();
view.execute(invocation);

assertTrue(invocation.getInvocationContext().getParameters().contains("bar"));
assertEquals("1", invocation.getInvocationContext().getParameters().get("bar").getValue());

// See https://issues.apache.org/jira/browse/WW-5486
assertEquals("1", stack.findString("#parameters.bar"));
}

@Override
public void setUp() throws Exception {
super.setUp();
invocation = new MockActionInvocation();
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
stack = container.getInstance(ValueStackFactory.class).createValueStack();
invocation.setStack(stack);

stack.getActionContext()
.withServletRequest(request)
.withServletResponse(response)
.withActionInvocation(invocation)
.withValueStack(stack)
.bind();

invocation.setInvocationContext(ActionContext.getContext());
}

}
Loading