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);
| Modifier and Type | Field and Description |
|---|---|
| private final LocalConnector | |
| private final ServletContextHandler | |
| private final Server | |
| private static final Logger |
| Access | Constructor and Description |
|---|---|
| public | |
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public FilterHolder | |
| public FilterHolder | |
| public ServletHolder | |
| public ServletHolder | |
| public void | |
| public void | |
| public String | Returns: A URL to access the server via the connector.true if connector should use localhost, false for default host behavior. localhost)Create a port based connector. |
| public LocalConnector | |
| public Object | |
| public Enumeration | |
| public Attributes | |
| public LocalConnector | |
| public ServletContextHandler | |
| public String | |
| public String | |
| public Enumeration | |
| public Map | |
| public Resource | |
| public String | |
| public String | |
| public ByteBuffer | |
| public ByteBuffer | |
| public Server | |
| public void | |
| public void | |
| public void | |
| public String | |
| public void | |
| public void |
| _connector | back to summary |
|---|---|
| private final LocalConnector _connector | |
| _context | back to summary |
|---|---|
| private final ServletContextHandler _context | |
| _server | back to summary |
|---|---|
| private final Server _server | |
| LOG | back to summary |
|---|---|
| private static final Logger LOG Hides org. | |
| ServletTester | back to summary |
|---|---|
| public ServletTester() | |
| ServletTester | back to summary |
|---|---|
| public ServletTester(String ctxPath) | |
| ServletTester | back to summary |
|---|---|
| public ServletTester(String contextPath, int options) | |
| addFilter | back to summary |
|---|---|
| public void addFilter(FilterHolder holder, String pathSpec, EnumSet<DispatcherType> dispatches) | |
| addFilter | back to summary |
|---|---|
| public FilterHolder addFilter(Class<? extends Filter> filterClass, String pathSpec, EnumSet<DispatcherType> dispatches) | |
| addFilter | back to summary |
|---|---|
| public FilterHolder addFilter(String filterClass, String pathSpec, EnumSet<DispatcherType> dispatches) | |
| addServlet | back to summary |
|---|---|
| public ServletHolder addServlet(String className, String pathSpec) | |
| addServlet | back to summary |
|---|---|
| public ServletHolder addServlet(Class<? extends Servlet> servlet, String pathSpec) | |
| addServlet | back to summary |
|---|---|
| public void addServlet(ServletHolder servlet, String pathSpec) | |
| addVirtualHosts | back to summary |
|---|---|
| public void addVirtualHosts(String[] virtualHosts) | |
| createConnector | back to summary |
|---|---|
| public String createConnector(boolean localhost) throws Exception Create a port based connector. This methods adds a port connector to the server | |
| createLocalConnector | back to summary |
|---|---|
| public LocalConnector createLocalConnector() | |
| getAttribute | back to summary |
|---|---|
| public Object getAttribute(String name) | |
| getAttributeNames | back to summary |
|---|---|
| public Enumeration | |
| getAttributes | back to summary |
|---|---|
| public Attributes getAttributes() | |
| getConnector | back to summary |
|---|---|
| public LocalConnector getConnector() | |
| getContext | back to summary |
|---|---|
| public ServletContextHandler getContext() | |
| getContextPath | back to summary |
|---|---|
| public String getContextPath() | |
| getInitParameter | back to summary |
|---|---|
| public String getInitParameter(String name) | |
| getInitParameterNames | back to summary |
|---|---|
| public Enumeration | |
| getInitParams | back to summary |
|---|---|
| public Map | |
| getResourceBase | back to summary |
|---|---|
| public Resource getResourceBase() | |
| getResponses | back to summary |
|---|---|
| public String getResponses(String request) throws Exception | |
| getResponses | back to summary |
|---|---|
| public String getResponses(String request, long idleFor, TimeUnit units) throws Exception | |
| getResponses | back to summary |
|---|---|
| public ByteBuffer getResponses(ByteBuffer request) throws Exception | |
| getResponses | back to summary |
|---|---|
| public ByteBuffer getResponses(ByteBuffer requestsBuffer, long idleFor, TimeUnit units) throws Exception | |
| getServer | back to summary |
|---|---|
| public Server getServer() | |
| removeAttribute | back to summary |
|---|---|
| public void removeAttribute(String name) | |
| setAttribute | back to summary |
|---|---|
| public void setAttribute(String name, Object value) | |
| setContextPath | back to summary |
|---|---|
| public void setContextPath(String contextPath) | |
| setInitParameter | back to summary |
|---|---|
| public String setInitParameter(String name, String value) | |
| setResourceBase | back to summary |
|---|---|
| public void setResourceBase(Resource base) | |
| setVirtualHosts | back to summary |
|---|---|
| public void setVirtualHosts(List<String> vhosts) | |