ServletTester is not best practice and may be deprecated and eventually + * removed in future Jetty versions.
+ *ServletTester is a just a wrapper around a {@link ServletContextHandler}, + * with a {@link 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 {@link HttpTester} and {@link 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); + *+ */ public class ServletTester extends ContainerLifeCycle { private static final Logger LOG = LoggerFactory.getLogger(ServletTester.class); diff --git a/jetty-servlets/pom.xml b/jetty-servlets/pom.xml index c579ba758ec..be3d97a32f1 100644 --- a/jetty-servlets/pom.xml +++ b/jetty-servlets/pom.xml @@ -84,13 +84,6 @@