Permalink
Browse files

Added JASPIC test for request/response wrapping in filters

  • Loading branch information...
arjantijms committed Oct 18, 2016
1 parent dd025f3 commit fe85e711807c194e22d3414f55fb1a3d92d05626
@@ -1,10 +1,14 @@
package org.javaee7.jaspic.wrapping.sam;
import static java.util.EnumSet.allOf;
import javax.servlet.DispatcherType;
import javax.servlet.ServletContextEvent;
import javax.servlet.annotation.WebListener;
import org.javaee7.jaspic.common.BaseServletContextListener;
import org.javaee7.jaspic.common.JaspicUtils;
import org.javaee7.jaspic.wrapping.servlet.ProgrammaticFilter;
/**
*
@@ -17,6 +21,10 @@
@Override
public void contextInitialized(ServletContextEvent sce) {
JaspicUtils.registerSAM(sce.getServletContext(), new TestWrappingServerAuthModule());
sce.getServletContext()
.addFilter("Programmatic filter", ProgrammaticFilter.class)
.addMappingForUrlPatterns(allOf(DispatcherType.class), false, "/*");
}
}
@@ -0,0 +1,43 @@
package org.javaee7.jaspic.wrapping.servlet;
import java.io.IOException;
import java.io.Writer;
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 javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
/**
* This Filter tests that the request and response objects it receives are the ones marked as wrapped by the SAM that executed
* before the Servlet was called.
*
* @author Arjan Tijms
*
*/
@WebFilter(urlPatterns="/*")
public class DeclaredFilter implements Filter {
public void init(FilterConfig fConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
Writer writer = response.getWriter();
writer.write("declared filter request isWrapped: " + request.getAttribute("isWrapped"));
writer.write("\n");
writer.write("declared filter response isWrapped: " + ((HttpServletResponse)response).getHeader("isWrapped"));
writer.write("\n");
chain.doFilter(request, response);
}
public void destroy() {
}
}
@@ -0,0 +1,41 @@
package org.javaee7.jaspic.wrapping.servlet;
import java.io.IOException;
import java.io.Writer;
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 javax.servlet.http.HttpServletResponse;
/**
* This Filter tests that the request and response objects it receives are the ones marked as wrapped by the SAM that executed
* before the Servlet was called.
*
* @author Arjan Tijms
*
*/
public class ProgrammaticFilter implements Filter {
public void init(FilterConfig fConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
Writer writer = response.getWriter();
writer.write("programmatic filter request isWrapped: " + request.getAttribute("isWrapped"));
writer.write("\n");
writer.write("programmatic filter response isWrapped: " + ((HttpServletResponse)response).getHeader("isWrapped"));
writer.write("\n");
chain.doFilter(request, response);
}
public void destroy() {
}
}
@@ -26,9 +26,9 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
Writer writer = response.getWriter();
writer.write("request isWrapped: " + request.getAttribute("isWrapped"));
writer.write("servlet request isWrapped: " + request.getAttribute("isWrapped"));
writer.write("\n");
writer.write("response isWrapped: " + response.getHeader("isWrapped"));
writer.write("servlet response isWrapped: " + response.getHeader("isWrapped"));
}
}
@@ -14,7 +14,7 @@
/**
* This tests that the wrapped request and response a SAM puts into the MessageInfo structure reaches the Servlet that's
* invoked.
* invoked as well as all filters executed before that.
*
* @author Arjan Tijms
*
@@ -26,6 +26,46 @@
public static Archive<?> createDeployment() {
return defaultArchive();
}
@Test
public void testProgrammaticFilterRequestWrapping() throws IOException, SAXException {
String response = getFromServerPath("protected/servlet");
// The SAM wrapped a request so that it always contains the request attribute "isWrapped" with value true.
assertTrue("Request wrapped by SAM did not arrive in programmatic Filter.",
response.contains("programmatic filter request isWrapped: true"));
}
@Test
public void testProgrammaticFilterResponseWrapping() throws IOException, SAXException {
String response = getFromServerPath("protected/servlet");
// The SAM wrapped a response so that it always contains the header "isWrapped" with value true.
assertTrue("Response wrapped by SAM did not arrive in programmatic Filter.",
response.contains("programmatic filter response isWrapped: true"));
}
@Test
public void testDeclaredFilterRequestWrapping() throws IOException, SAXException {
String response = getFromServerPath("protected/servlet");
// The SAM wrapped a request so that it always contains the request attribute "isWrapped" with value true.
assertTrue("Request wrapped by SAM did not arrive in declared Filter.",
response.contains("declared filter request isWrapped: true"));
}
@Test
public void testDeclaredFilterResponseWrapping() throws IOException, SAXException {
String response = getFromServerPath("protected/servlet");
// The SAM wrapped a response so that it always contains the header "isWrapped" with value true.
assertTrue("Response wrapped by SAM did not arrive in declared Filter.",
response.contains("declared filter response isWrapped: true"));
}
@Test
public void testRequestWrapping() throws IOException, SAXException {
@@ -34,7 +74,7 @@ public void testRequestWrapping() throws IOException, SAXException {
// The SAM wrapped a request so that it always contains the request attribute "isWrapped" with value true.
assertTrue("Request wrapped by SAM did not arrive in Servlet.",
response.contains("request isWrapped: true"));
response.contains("servlet request isWrapped: true"));
}
@Test
@@ -44,7 +84,7 @@ public void testResponseWrapping() throws IOException, SAXException {
// The SAM wrapped a response so that it always contains the header "isWrapped" with value true.
assertTrue("Response wrapped by SAM did not arrive in Servlet.",
response.contains("response isWrapped: true"));
response.contains("servlet response isWrapped: true"));
}
}

0 comments on commit fe85e71

Please sign in to comment.