Extra tests for #5378
This commit is contained in:
parent
85e257fc98
commit
6d69e31439
|
@ -190,9 +190,20 @@ public class ServletContextHandler extends ContextHandler
|
|||
@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)
|
||||
|
|
|
@ -897,7 +897,7 @@ public class ServletContextHandlerTest
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testAddServletFromSCL() throws Exception
|
||||
public void testAddFilterServletFromSCL() throws Exception
|
||||
{
|
||||
//A servlet can be added from a ServletContextListener
|
||||
ServletContextHandler context = new ServletContextHandler();
|
||||
|
@ -905,10 +905,11 @@ public class ServletContextHandlerTest
|
|||
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/*");
|
||||
}
|
||||
|
@ -927,11 +928,13 @@ public class ServletContextHandlerTest
|
|||
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();
|
||||
|
@ -943,6 +946,8 @@ public class ServletContextHandlerTest
|
|||
@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/*");
|
||||
}
|
||||
|
@ -957,7 +962,9 @@ public class ServletContextHandlerTest
|
|||
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
|
||||
|
@ -1430,7 +1437,6 @@ public class ServletContextHandlerTest
|
|||
|
||||
public static class MyFilter implements Filter
|
||||
{
|
||||
|
||||
@Override
|
||||
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,
|
||||
ServletException
|
||||
{
|
||||
((HttpServletResponse)response).addHeader("filter", "filter");
|
||||
request.getServletContext().setAttribute("filter", "filter");
|
||||
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"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue