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);
     ServletContextHandler context = new ServletContextHandler(server, "/contextPath");
     // Configure the context here.

    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.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" +
     String rawResponse = connector.getResponse(rawRequest);
     HttpTester.Response response = HttpTester.parseResponse(rawResponse);
    • Constructor Detail

      • ServletTester

        public ServletTester()
      • ServletTester

        public ServletTester​(java.lang.String ctxPath)
      • ServletTester

        public ServletTester​(java.lang.String contextPath,
                             int options)
    • Method Detail

      • getServer

        public Server getServer()
      • setVirtualHosts

        public void setVirtualHosts​(java.lang.String[] vhosts)
      • addVirtualHosts

        public void addVirtualHosts​(java.lang.String[] virtualHosts)
      • addServlet

        public ServletHolder addServlet​(java.lang.String className,
                                        java.lang.String pathSpec)
      • addServlet

        public ServletHolder addServlet​(java.lang.Class<? extends javax.servlet.Servlet> servlet,
                                        java.lang.String pathSpec)
      • addServlet

        public void addServlet​(ServletHolder servlet,
                               java.lang.String pathSpec)
      • addFilter

        public void addFilter​(FilterHolder holder,
                              java.lang.String pathSpec,
                              java.util.EnumSet<javax.servlet.DispatcherType> dispatches)
      • addFilter

        public FilterHolder addFilter​(java.lang.Class<? extends javax.servlet.Filter> filterClass,
                                      java.lang.String pathSpec,
                                      java.util.EnumSet<javax.servlet.DispatcherType> dispatches)
      • addFilter

        public FilterHolder addFilter​(java.lang.String filterClass,
                                      java.lang.String pathSpec,
                                      java.util.EnumSet<javax.servlet.DispatcherType> dispatches)
      • getAttribute

        public java.lang.Object getAttribute​(java.lang.String name)
      • getAttributeNames

        public java.util.Enumeration<java.lang.String> getAttributeNames()
      • getAttributes

        public Attributes getAttributes()
      • getContextPath

        public java.lang.String getContextPath()
      • getInitParameter

        public java.lang.String getInitParameter​(java.lang.String name)
      • setInitParameter

        public java.lang.String setInitParameter​(java.lang.String name,
                                                 java.lang.String value)
      • getInitParameterNames

        public java.util.Enumeration<java.lang.String> getInitParameterNames()
      • getInitParams

        public java.util.Map<java.lang.String,​java.lang.String> getInitParams()
      • removeAttribute

        public void removeAttribute​(java.lang.String name)
      • setAttribute

        public void setAttribute​(java.lang.String name,
                                 java.lang.Object value)
      • setContextPath

        public void setContextPath​(java.lang.String contextPath)
      • getBaseResource

        public Resource getBaseResource()
      • setBaseResource

        public void setBaseResource​(Resource resource)
      • getResourceBase

        public java.lang.String getResourceBase()
      • setResourceBase

        public void setResourceBase​(java.lang.String resourceBase)
      • getResponses

        public java.lang.String getResponses​(java.lang.String request)
                                      throws java.lang.Exception
      • getResponses

        public java.lang.String getResponses​(java.lang.String request,
                                             long idleFor,
                                             java.util.concurrent.TimeUnit units)
                                      throws java.lang.Exception
      • getResponses

        public java.nio.ByteBuffer getResponses​(java.nio.ByteBuffer request)
                                         throws java.lang.Exception
      • getResponses

        public java.nio.ByteBuffer getResponses​(java.nio.ByteBuffer requestsBuffer,
                                                long idleFor,
                                                java.util.concurrent.TimeUnit units)
                                         throws java.lang.Exception
      • createConnector

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