public void endServer() {
   if (updateTask != null) {
     updateTask.cancel();
   }
   if (context != null) {
     try {
       context.stop();
       if (!context.isStopped()) {
         hcw.getDataBukkit().writeError("Context failed to stop.");
       }
     } catch (Exception e) {
       hcw.getDataBukkit().writeError(e);
     }
   }
   if (server != null) {
     try {
       server.stop();
       if (!server.isStopped()) {
         hcw.getDataBukkit().writeError("Server failed to stop.");
       }
     } catch (Exception e) {
       hcw.getDataBukkit().writeError(e);
     }
   }
 }
  @Test
  public void testReplaceServletHandlerWithoutServlet() throws Exception {
    ServletContextHandler context = new ServletContextHandler();
    context.addServlet(TestServlet.class, "/test");
    context.setContextPath("/");
    _server.setHandler(context);
    _server.start();

    StringBuffer request = new StringBuffer();
    request.append("GET /test HTTP/1.0\n");
    request.append("Host: localhost\n");
    request.append("\n");

    String response = _connector.getResponses(request.toString());
    assertResponseContains("Test", response);

    context.stop();
    ServletHandler srvHnd = new ServletHandler();
    context.setServletHandler(srvHnd);
    context.start();

    context.addServlet(HelloServlet.class, "/hello");

    request = new StringBuffer();
    request.append("GET /hello HTTP/1.0\n");
    request.append("Host: localhost\n");
    request.append("\n");

    response = _connector.getResponses(request.toString());
    assertResponseContains("Hello World", response);
  }
  @Test
  public void testResponseHeadersAreNotRemoved() throws Exception {
    prepareProxy();
    proxyContext.stop();
    final String headerName = "X-Test";
    final String headerValue = "test-value";
    proxyContext.addFilter(
        new FilterHolder(
            new Filter() {
              @Override
              public void init(FilterConfig filterConfig) throws ServletException {}

              @Override
              public void doFilter(
                  ServletRequest request, ServletResponse response, FilterChain chain)
                  throws IOException, ServletException {
                ((HttpServletResponse) response).addHeader(headerName, headerValue);
                chain.doFilter(request, response);
              }

              @Override
              public void destroy() {}
            }),
        "/*",
        EnumSet.of(DispatcherType.REQUEST));
    proxyContext.start();
    prepareServer(new EmptyHttpServlet());

    HttpClient client = prepareClient();
    ContentResponse response =
        client.newRequest("localhost", serverConnector.getLocalPort()).send();

    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(headerValue, response.getHeaders().get(headerName));
  }