Extra tests for #5378

This commit is contained in:
gregw 2020-10-05 12:28:10 +02:00
parent 85e257fc98
commit 6d69e31439
2 changed files with 63 additions and 10 deletions

View File

@ -190,9 +190,20 @@ public class ServletContextHandler extends ContextHandler
@Override @Override
public void setHandler(Handler handler) public void setHandler(Handler handler)
{ {
if (handler != null) if (handler instanceof SessionHandler)
LOG.warn("ServletContextHandler.setHandler should not be called directly. Use insertHandler or setSessionHandler etc."); setSessionHandler((SessionHandler)handler);
super.setHandler(handler); else if (handler instanceof SecurityHandler)
setSecurityHandler((SecurityHandler)handler);
else if (handler instanceof ServletHandler)
setServletHandler((ServletHandler)handler);
else if (handler instanceof GzipHandler)
setGzipHandler((GzipHandler)handler);
else
{
if (handler != null)
LOG.warn("ServletContextHandler.setHandler should not be called directly. Use insertHandler or setSessionHandler etc.");
super.setHandler(handler);
}
} }
private void doSetHandler(HandlerWrapper wrapper, Handler handler) private void doSetHandler(HandlerWrapper wrapper, Handler handler)

View File

@ -897,7 +897,7 @@ public class ServletContextHandlerTest
} }
@Test @Test
public void testAddServletFromSCL() throws Exception public void testAddFilterServletFromSCL() throws Exception
{ {
//A servlet can be added from a ServletContextListener //A servlet can be added from a ServletContextListener
ServletContextHandler context = new ServletContextHandler(); ServletContextHandler context = new ServletContextHandler();
@ -905,10 +905,11 @@ public class ServletContextHandlerTest
context.setContextPath("/"); context.setContextPath("/");
context.addEventListener(new ServletContextListener() context.addEventListener(new ServletContextListener()
{ {
@Override @Override
public void contextInitialized(ServletContextEvent sce) public void contextInitialized(ServletContextEvent sce)
{ {
sce.getServletContext().addFilter("filter", new MyFilter())
.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
ServletRegistration rego = sce.getServletContext().addServlet("hello", HelloServlet.class); ServletRegistration rego = sce.getServletContext().addServlet("hello", HelloServlet.class);
rego.addMapping("/hello/*"); rego.addMapping("/hello/*");
} }
@ -927,11 +928,13 @@ public class ServletContextHandlerTest
request.append("\n"); request.append("\n");
String response = _connector.getResponse(request.toString()); String response = _connector.getResponse(request.toString());
assertThat("Response", response, containsString("Hello World")); assertThat(response, containsString("200 OK"));
assertThat(response, containsString("filter: filter"));
assertThat(response, containsString("Hello World"));
} }
@Test @Test
public void testAddServletFromSCI() throws Exception public void testAddFilterServletFromSCI() throws Exception
{ {
//A servlet can be added from a ServletContainerInitializer //A servlet can be added from a ServletContainerInitializer
ContextHandlerCollection contexts = new ContextHandlerCollection(); ContextHandlerCollection contexts = new ContextHandlerCollection();
@ -943,6 +946,8 @@ public class ServletContextHandlerTest
@Override @Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) public void onStartup(Set<Class<?>> c, ServletContext ctx)
{ {
ctx.addFilter("filter", new MyFilter())
.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
ServletRegistration rego = ctx.addServlet("hello", HelloServlet.class); ServletRegistration rego = ctx.addServlet("hello", HelloServlet.class);
rego.addMapping("/hello/*"); rego.addMapping("/hello/*");
} }
@ -957,7 +962,9 @@ public class ServletContextHandlerTest
request.append("\n"); request.append("\n");
String response = _connector.getResponse(request.toString()); String response = _connector.getResponse(request.toString());
assertThat("Response", response, containsString("Hello World")); assertThat(response, containsString("200 OK"));
assertThat(response, containsString("filter: filter"));
assertThat(response, containsString("Hello World"));
} }
@Test @Test
@ -1430,7 +1437,6 @@ public class ServletContextHandlerTest
public static class MyFilter implements Filter public static class MyFilter implements Filter
{ {
@Override @Override
public void init(FilterConfig filterConfig) throws ServletException public void init(FilterConfig filterConfig) throws ServletException
{ {
@ -1440,6 +1446,7 @@ public class ServletContextHandlerTest
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException ServletException
{ {
((HttpServletResponse)response).addHeader("filter", "filter");
request.getServletContext().setAttribute("filter", "filter"); request.getServletContext().setAttribute("filter", "filter");
chain.doFilter(request, response); chain.doFilter(request, response);
} }
@ -1636,4 +1643,39 @@ public class ServletContextHandlerTest
} }
} }
} }
}
@Test
public void testProgrammaticFilterServlet() throws Exception
{
ServletContextHandler context = new ServletContextHandler();
ServletHandler handler = new ServletHandler();
_server.setHandler(context);
context.setHandler(handler);
handler.addServletWithMapping(new ServletHolder(new TestServlet()), "/");
_server.start();
String request =
"GET /hello HTTP/1.0\n" +
"Host: localhost\n" +
"\n";
String response = _connector.getResponse(request);
assertThat(response, containsString("200 OK"));
assertThat(response, containsString("Test"));
handler.addFilterWithMapping(new FilterHolder(new MyFilter()), "/*", EnumSet.of(DispatcherType.REQUEST));
handler.addServletWithMapping(new ServletHolder(new HelloServlet()), "/hello/*");
_server.dumpStdErr();
request =
"GET /hello HTTP/1.0\n" +
"Host: localhost\n" +
"\n";
response = _connector.getResponse(request);
assertThat(response, containsString("200 OK"));
assertThat(response, containsString("filter: filter"));
assertThat(response, containsString("Hello World"));
}
}