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

    • getServer

      public Server getServer()
    • getConnector

      public LocalConnector getConnector()
    • setVirtualHosts

      public void setVirtualHosts(String[] vhosts)
    • addVirtualHosts

      public void addVirtualHosts(String[] virtualHosts)
    • addServlet

      public ServletHolder addServlet(String className, String pathSpec)
    • addServlet

      public ServletHolder addServlet(Class<? extends jakarta.servlet.Servlet> servlet, String pathSpec)
    • addServlet

      public void addServlet(ServletHolder servlet, String pathSpec)
    • addFilter

      public void addFilter(FilterHolder holder, String pathSpec, EnumSet<jakarta.servlet.DispatcherType> dispatches)
    • addFilter

      public FilterHolder addFilter(Class<? extends jakarta.servlet.Filter> filterClass, String pathSpec, EnumSet<jakarta.servlet.DispatcherType> dispatches)
    • addFilter

      public FilterHolder addFilter(String filterClass, String pathSpec, EnumSet<jakarta.servlet.DispatcherType> dispatches)
    • getAttribute

      public Object getAttribute(String name)
    • getAttributeNames

      public Enumeration<String> getAttributeNames()
    • getAttributes

      public Attributes getAttributes()
    • getContextPath

      public String getContextPath()
    • getInitParameter

      public String getInitParameter(String name)
    • setInitParameter

      public String setInitParameter(String name, String value)
    • getInitParameterNames

      public Enumeration<String> getInitParameterNames()
    • getInitParams

      public Map<String,String> getInitParams()
    • removeAttribute

      public void removeAttribute(String name)
    • setAttribute

      public void setAttribute(String name, Object value)
    • setContextPath

      public void setContextPath(String contextPath)
    • getBaseResource

      public Resource getBaseResource()
    • setBaseResource

      public void setBaseResource(Resource resource)
    • getResourceBase

      public String getResourceBase()
    • setResourceBase

      public void setResourceBase(String resourceBase)
    • getContext

      public ServletContextHandler getContext()
    • getResponses

      public String getResponses(String request) throws Exception
      Throws:
      Exception
    • getResponses

      public String getResponses(String request, long idleFor, TimeUnit units) throws Exception
      Throws:
      Exception
    • getResponses

      public ByteBuffer getResponses(ByteBuffer request) throws Exception
      Throws:
      Exception
    • getResponses

      public ByteBuffer getResponses(ByteBuffer requestsBuffer, long idleFor, TimeUnit units) throws Exception
      Throws:
      Exception
    • createConnector

      public String createConnector(boolean localhost) throws Exception
      Create a port based connector. This methods adds a port connector to the server
      Parameters:
      localhost - true if connector should use localhost, false for default host behavior.
      Returns:
      A URL to access the server via the connector.
      Throws:
      Exception - on test failure
    • createLocalConnector

      public LocalConnector createLocalConnector()