Package org.eclipse.jetty.servlet
Class ServletTester
java.lang.Object
org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.component.ContainerLifeCycle
org.eclipse.jetty.servlet.ServletTester
- All Implemented Interfaces:
Container
,Destroyable
,Dumpable
,Dumpable.DumpableContainer
,LifeCycle
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);
-
Nested Class Summary
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener, AbstractLifeCycle.StopException
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Container
Container.InheritedListener, Container.Listener
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Dumpable
Dumpable.DumpableContainer
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle
LifeCycle.Listener
-
Field Summary
-
Constructor Summary
ConstructorDescriptionServletTester
(String ctxPath) ServletTester
(String contextPath, int options) -
Method Summary
Modifier and TypeMethodDescriptionaddFilter
(Class<? extends jakarta.servlet.Filter> filterClass, String pathSpec, EnumSet<jakarta.servlet.DispatcherType> dispatches) void
addFilter
(FilterHolder holder, String pathSpec, EnumSet<jakarta.servlet.DispatcherType> dispatches) addServlet
(Class<? extends jakarta.servlet.Servlet> servlet, String pathSpec) addServlet
(String className, String pathSpec) void
addServlet
(ServletHolder servlet, String pathSpec) void
addVirtualHosts
(String[] virtualHosts) createConnector
(boolean localhost) Create a port based connector.getAttribute
(String name) getInitParameter
(String name) getResponses
(String request) getResponses
(String request, long idleFor, TimeUnit units) getResponses
(ByteBuffer request) getResponses
(ByteBuffer requestsBuffer, long idleFor, TimeUnit units) void
removeAttribute
(String name) void
setAttribute
(String name, Object value) void
setBaseResource
(Resource resource) void
setContextPath
(String contextPath) setInitParameter
(String name, String value) void
setResourceBase
(String resourceBase) void
setVirtualHosts
(String[] vhosts) Methods inherited from class org.eclipse.jetty.util.component.ContainerLifeCycle
addBean, addBean, addEventListener, addManaged, contains, destroy, doStart, doStop, dump, dump, dump, dumpObjects, dumpStdErr, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, start, stop, unmanage, updateBean, updateBean, updateBeans, updateBeans
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
getEventListeners, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, setEventListeners, start, stop, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.eclipse.jetty.util.component.Container
getCachedBeans, getEventListeners
Methods inherited from interface org.eclipse.jetty.util.component.Dumpable.DumpableContainer
isDumpable
-
Constructor Details
-
ServletTester
public ServletTester() -
ServletTester
-
ServletTester
-
-
Method Details
-
getServer
-
getConnector
-
setVirtualHosts
-
addVirtualHosts
-
addServlet
-
addServlet
-
addServlet
-
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
-
getAttributeNames
-
getAttributes
-
getContextPath
-
getInitParameter
-
setInitParameter
-
getInitParameterNames
-
getInitParams
-
removeAttribute
-
setAttribute
-
setContextPath
-
getBaseResource
-
setBaseResource
-
getResourceBase
-
setResourceBase
-
getContext
-
getResponses
- Throws:
Exception
-
getResponses
- Throws:
Exception
-
getResponses
- Throws:
Exception
-
getResponses
public ByteBuffer getResponses(ByteBuffer requestsBuffer, long idleFor, TimeUnit units) throws Exception - Throws:
Exception
-
createConnector
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
-