[
https://issues.apache.org/jira/browse/WW-5267?focusedWorklogId=848267&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-848267
]
ASF GitHub Bot logged work on WW-5267:
--------------------------------------
Author: ASF GitHub Bot
Created on: 01/Mar/23 10:08
Start Date: 01/Mar/23 10:08
Worklog Time Spent: 10m
Work Description: kusalk commented on code in PR #663:
URL: https://github.com/apache/struts/pull/663#discussion_r1121457278
##########
core/src/test/java/org/apache/struts2/dispatcher/TwoFilterIntegrationTest.java:
##########
@@ -19,87 +19,133 @@
package org.apache.struts2.dispatcher;
import com.opensymphony.xwork2.ActionContext;
-import junit.framework.TestCase;
-import org.apache.struts2.dispatcher.Dispatcher;
-import org.apache.struts2.dispatcher.PrepareOperations;
import org.apache.struts2.dispatcher.filter.StrutsExecuteFilter;
import org.apache.struts2.dispatcher.filter.StrutsPrepareFilter;
-import org.springframework.mock.web.*;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.mock.web.MockFilterChain;
+import org.springframework.mock.web.MockFilterConfig;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
-import javax.servlet.*;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
import java.io.IOException;
-import java.util.LinkedList;
import java.util.Arrays;
+import java.util.LinkedList;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
/**
* Integration tests for the filter
*/
-public class TwoFilterIntegrationTest extends TestCase {
- StrutsExecuteFilter filterExecute;
- StrutsPrepareFilter filterPrepare;
- Filter failFilter;
+public class TwoFilterIntegrationTest {
+ private StrutsExecuteFilter filterExecute;
+ private StrutsPrepareFilter filterPrepare;
+ private Filter failFilter;
private Filter stringFilter;
+ @Before
public void setUp() {
filterPrepare = new StrutsPrepareFilter();
filterExecute = new StrutsExecuteFilter();
- failFilter = new Filter() {
- public void init(FilterConfig filterConfig) throws
ServletException {}
- public void doFilter(ServletRequest request, ServletResponse
response, FilterChain chain) throws IOException, ServletException {
- fail("Should never get here");
- }
- public void destroy() {}
- };
- stringFilter = new Filter() {
- public void init(FilterConfig filterConfig) throws
ServletException {}
- public void doFilter(ServletRequest request, ServletResponse
response, FilterChain chain) throws IOException, ServletException {
- response.getWriter().write("content");
- assertNotNull(ActionContext.getContext());
- assertNotNull(Dispatcher.getInstance());
- }
- public void destroy() {}
- };
+ failFilter = newFilter((req, res, chain) -> fail("Should never get
here"));
+ stringFilter = newFilter((req, res, chain) -> {
+ res.getWriter().write("content");
+ assertNotNull(ActionContext.getContext());
+ assertNotNull(Dispatcher.getInstance());
+ });
}
+ @Test
public void test404() throws ServletException, IOException {
MockHttpServletResponse response = run("/foo.action", filterPrepare,
filterExecute, failFilter);
assertEquals(404, response.getStatus());
}
+ @Test
public void test200() throws ServletException, IOException {
MockHttpServletResponse response = run("/hello.action", filterPrepare,
filterExecute, failFilter);
assertEquals(200, response.getStatus());
}
+ @Test
public void testStaticFallthrough() throws ServletException, IOException {
MockHttpServletResponse response = run("/foo.txt", filterPrepare,
filterExecute, stringFilter);
assertEquals(200, response.getStatus());
assertEquals("content", response.getContentAsString());
}
+ @Test
public void testStaticExecute() throws ServletException, IOException {
MockHttpServletResponse response = run("/static/utils.js",
filterPrepare, filterExecute, failFilter);
assertEquals(200, response.getStatus());
assertTrue(response.getContentAsString().contains("StrutsUtils"));
}
+ @Test
public void testFilterInMiddle() throws ServletException, IOException {
- Filter middle = new Filter() {
- public void init(FilterConfig filterConfig) throws
ServletException {}
- public void doFilter(ServletRequest request, ServletResponse
response, FilterChain chain) throws IOException, ServletException {
- assertNotNull(ActionContext.getContext());
- assertNotNull(Dispatcher.getInstance());
- assertNull(ActionContext.getContext().getActionInvocation());
- chain.doFilter(request, response);
- assertEquals("hello",
ActionContext.getContext().getActionInvocation().getProxy().getActionName());
- }
- public void destroy() {}
- };
+ Filter middle = newFilter((req, res, chain) -> {
+ assertNotNull(ActionContext.getContext());
+ assertNotNull(Dispatcher.getInstance());
+ assertNull(ActionContext.getContext().getActionInvocation());
+ chain.doFilter(req, res);
+ assertEquals("hello",
ActionContext.getContext().getActionInvocation().getProxy().getActionName());
+ });
MockHttpServletResponse response = run("/hello.action", filterPrepare,
middle, filterExecute, failFilter);
assertEquals(200, response.getStatus());
}
+ /**
+ * It is possible for a Struts excluded URL to be forwarded to a Struts
URL. If this happens, the ActionContext
+ * should not be cleared until the very first execution of the
StrutsPrepareFilter, otherwise SiteMesh will malfunction.
+ */
+ @Test
+ public void
testActionContextNotClearedUntilEndWhenForwardedFromExcludedUrl() throws
ServletException, IOException {
Review Comment:
This integration test is actually for
[WW-5270](https://issues.apache.org/jira/browse/WW-5270). (We already have
acceptance test coverage for that fix.)
Issue Time Tracking
-------------------
Worklog Id: (was: 848267)
Time Spent: 20m (was: 10m)
> Add configuration option to generate ActionContext even for excluded urls
> -------------------------------------------------------------------------
>
> Key: WW-5267
> URL: https://issues.apache.org/jira/browse/WW-5267
> Project: Struts 2
> Issue Type: Improvement
> Components: Core
> Reporter: Kusal Kithul-Godage
> Priority: Minor
> Fix For: 6.2.0
>
> Time Spent: 20m
> Remaining Estimate: 0h
>
> There are scenarios where you may want to except a request from Struts
> filtering/processing using `struts.action.excludePattern`, however you may
> still want that request to undergo filtering such as SiteMesh, which requires
> the ActionContext to be present.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)