Class ServletTester

All Implemented Interfaces:
Container, Destroyable, Dumpable, Dumpable.DumpableContainer, LifeCycle

public class ServletTester extends ContainerLifeCycle

ServletTester is not best practice and may be deprecated and eventually removed in future Jetty versions.

ServletTester is a just a wrapper around a ServletContextHandler, with a LocalConnector to accept HTTP/1.1 requests, so there is no value that this class adds to already existing classes.

Replace its usages with:

 Server server = new Server();
 LocalConnector connector = new LocalConnector(server);
 server.addConnector(connector);
 ServletContextHandler context = new ServletContextHandler(server, "/contextPath");
 // Configure the context here.
 server.start();
 

You can configure the context by adding Servlets and Filters, attributes, etc. even after it has been started. Use HttpTester and LocalConnector to make HTTP/1.1 requests, in this way:

 // Generate the request.
 HttpTester.Request request = HttpTester.newRequest();
 request.setMethod("GET");
 request.setURI("/contextPath/servletPath");
 request.put(HttpHeader.HOST, "localhost");
 ByteBuffer requestBuffer = request.generate();

 // Send the request buffer and get the response buffer.
 ByteBuffer responseBuffer = connector.getResponse(requestBuffer);

 // Parse the response buffer.
 HttpTester.Response response = HttpTester.parseResponse(responseBuffer);
 assert response.getStatus() == HttpStatus.OK_200;
 

Alternatively, you can use raw strings for requests and responses, but you must be sure the request strings are in the correct HTTP/1.1 format:

 String rawRequest = "" +
         "GET /contextPath/servletPath HTTP/1.1\r\n" +
         "Host: localhost\r\n" +
         "\r\n";
 String rawResponse = connector.getResponse(rawRequest);
 HttpTester.Response response = HttpTester.parseResponse(rawResponse);
 
  • Constructor Details

    • ServletTester

      public ServletTester()
    • ServletTester

      public ServletTester(String ctxPath)
    • ServletTester

      public ServletTester(String contextPath, int options)
  • Method Details