Skip to content

Commit

Permalink
Extra tests for #5378
Browse files Browse the repository at this point in the history
  • Loading branch information
gregw committed Oct 5, 2020
1 parent 85e257f commit 6d69e31
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,20 @@ public void addEventListener(EventListener listener)
@Override
public void setHandler(Handler handler)
{
if (handler != null)
LOG.warn("ServletContextHandler.setHandler should not be called directly. Use insertHandler or setSessionHandler etc.");
super.setHandler(handler);
if (handler instanceof SessionHandler)
setSessionHandler((SessionHandler)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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -897,18 +897,19 @@ public void destroy()
}

@Test
public void testAddServletFromSCL() throws Exception
public void testAddFilterServletFromSCL() throws Exception
{
//A servlet can be added from a ServletContextListener
ServletContextHandler context = new ServletContextHandler();
context.getServletHandler().setStartWithUnavailable(false);
context.setContextPath("/");
context.addEventListener(new ServletContextListener()
{

@Override
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);
rego.addMapping("/hello/*");
}
Expand All @@ -927,11 +928,13 @@ public void contextDestroyed(ServletContextEvent sce)
request.append("\n");

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
public void testAddServletFromSCI() throws Exception
public void testAddFilterServletFromSCI() throws Exception
{
//A servlet can be added from a ServletContainerInitializer
ContextHandlerCollection contexts = new ContextHandlerCollection();
Expand All @@ -943,6 +946,8 @@ class ServletAddingSCI implements ServletContainerInitializer
@Override
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);
rego.addMapping("/hello/*");
}
Expand All @@ -957,7 +962,9 @@ public void onStartup(Set<Class<?>> c, ServletContext ctx)
request.append("\n");

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
Expand Down Expand Up @@ -1430,7 +1437,6 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)

public static class MyFilter implements Filter
{

@Override
public void init(FilterConfig filterConfig) throws ServletException
{
Expand All @@ -1440,6 +1446,7 @@ public void init(FilterConfig filterConfig) throws ServletException
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException
{
((HttpServletResponse)response).addHeader("filter", "filter");
request.getServletContext().setAttribute("filter", "filter");
chain.doFilter(request, response);
}
Expand Down Expand Up @@ -1636,4 +1643,39 @@ else if ("delete".equalsIgnoreCase(action))
}
}
}
}

@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"));
}
}

0 comments on commit 6d69e31

Please sign in to comment.