## org.mortbay.jetty.testing
Class ServletTester

    java.lang.Object
      org.mortbay.jetty.testing.ServletTester

    public class ServletTester

extends java.lang.Object

Testing support for servlets and filters. Allows a programatic setup of a context with servlets and filters for testing. Raw HTTP requests may be sent to the context and responses received. To avoid handling raw HTTP see `HttpTester`.

          ServletTester tester=new ServletTester();
          tester.setContextPath("/context");
          tester.addServlet(TestServlet.class, "/servlet/*");
          tester.addServlet("org.mortbay.jetty.servlet.DefaultServlet", "/");
          tester.start();
          String response = tester.getResponses("GET /context/servlet/info HTTP/1.0\r\n\r\n");

Author:
  gregw
See Also:
  `HttpTester`

Constructor Summary
`ServletTester()`

Method Summary
`void` `addEventListener(java.util.EventListener listener)`
`FilterHolder` `addFilter(java.lang.Class filterClass, java.lang.String pathSpec, int dispatches)`
`FilterHolder` `addFilter(java.lang.String filterClass, java.lang.String pathSpec, int dispatches)`
`ServletHolder` `addServlet(java.lang.Class servlet, java.lang.String pathSpec)`
`ServletHolder` `addServlet(java.lang.String className, java.lang.String pathSpec)`
`LocalConnector` `createLocalConnector()` Create a Socket connector.
`java.lang.String` `createSocketConnector(boolean localhost)` Create a Socket connector.
`java.lang.Object` `getAttribute(java.lang.String name)`
`java.util.Enumeration` `getAttributeNames()`
`Attributes` `getAttributes()`
`Context` `getContext()`
`java.lang.String` `getResourceBase()`
`ByteArrayBuffer` `getResponses(ByteArrayBuffer rawRequests)` Get raw HTTP responses from raw HTTP requests.
`ByteArrayBuffer` `getResponses(ByteArrayBuffer rawRequests, LocalConnector connector)` Get raw HTTP responses from raw HTTP requests.
`java.lang.String` `getResponses(java.lang.String rawRequests)` Get raw HTTP responses from raw HTTP requests.
`java.lang.String` `getResponses(java.lang.String rawRequests, LocalConnector connector)` Get raw HTTP responses from raw HTTP requests.
`void` `setAttribute(java.lang.String name, java.lang.Object value)`
`void` `setClassLoader(java.lang.ClassLoader classLoader)`
`void` `setContextPath(java.lang.String contextPath)`
`void` `setEventListeners(java.util.EventListener[] eventListeners)`
`void` `setMaxIdleTime(int maxIdleTime)`
`void` `setResourceBase(java.lang.String resourceBase)`
`void` `start()`
`void` `stop()`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### ServletTester

    public ServletTester()

Method Detail

### start

    public void start()
               throws java.lang.Exception

  Throws:
    `java.lang.Exception`

### stop

    public void stop()
              throws java.lang.Exception

  Throws:
    `java.lang.Exception`

### getContext

    public Context getContext()

### getResponses

    public java.lang.String getResponses(java.lang.String rawRequests)
                                  throws java.lang.Exception

  Get raw HTTP responses from raw HTTP requests. Multiple requests and responses may be handled, but only if persistent connections conditions apply.

  Parameters:
    `rawRequests` - String of raw HTTP requests
  Returns:
    String of raw HTTP responses
  Throws:
    `java.lang.Exception`

### getResponses

    public java.lang.String getResponses(java.lang.String rawRequests,
                                         LocalConnector connector)
                                  throws java.lang.Exception

  Get raw HTTP responses from raw HTTP requests. Multiple requests and responses may be handled, but only if persistent connections conditions apply.

  Parameters:
    `rawRequests` - String of raw HTTP requests
    `connector` - The connector to handle the responses
  Returns:
    String of raw HTTP responses
  Throws:
    `java.lang.Exception`

### getResponses

    public ByteArrayBuffer getResponses(ByteArrayBuffer rawRequests)
                                 throws java.lang.Exception

  Get raw HTTP responses from raw HTTP requests. Multiple requests and responses may be handled, but only if persistent connections conditions apply.

  Parameters:
    `rawRequests` - String of raw HTTP requests
  Returns:
    String of raw HTTP responses
  Throws:
    `java.lang.Exception`

### getResponses

    public ByteArrayBuffer getResponses(ByteArrayBuffer rawRequests,
                                        LocalConnector connector)
                                 throws java.lang.Exception

  Get raw HTTP responses from raw HTTP requests. Multiple requests and responses may be handled, but only if persistent connections conditions apply.

  Parameters:
    `rawRequests` - String of raw HTTP requests
    `connector` - The connector to handle the responses
  Returns:
    String of raw HTTP responses
  Throws:
    `java.lang.Exception`

### createSocketConnector

    public java.lang.String createSocketConnector(boolean localhost)
                                           throws java.lang.Exception

  Create a Socket connector. This methods adds a socket connector to the server

  Parameters:
    `locahost` - if true, only listen on local host, else listen on all interfaces.
  Returns:
    A URL to access the server via the socket connector.
  Throws:
    `java.lang.Exception`

### createLocalConnector

    public LocalConnector createLocalConnector()
                                        throws java.lang.Exception

  Create a Socket connector. This methods adds a socket connector to the server

  Parameters:
    `locahost` - if true, only listen on local host, else listen on all interfaces.
  Returns:
    A URL to access the server via the socket connector.
  Throws:
    `java.lang.Exception`

### addEventListener

    public void addEventListener(java.util.EventListener listener)

  Parameters:
    `listener` -
  See Also:
    `ContextHandler.addEventListener(java.util.EventListener)`

### addFilter

    public FilterHolder addFilter(java.lang.Class filterClass,
                                  java.lang.String pathSpec,
                                  int dispatches)

  Parameters:
    `filterClass` -
    `pathSpec` -
    `dispatches` -
  Returns:
  See Also:
    `Context.addFilter(java.lang.Class, java.lang.String, int)`

### addFilter

    public FilterHolder addFilter(java.lang.String filterClass,
                                  java.lang.String pathSpec,
                                  int dispatches)

  Parameters:
    `filterClass` -
    `pathSpec` -
    `dispatches` -
  Returns:
  See Also:
    `Context.addFilter(java.lang.String, java.lang.String, int)`

### addServlet

    public ServletHolder addServlet(java.lang.Class servlet,
                                    java.lang.String pathSpec)

  Parameters:
    `servlet` -
    `pathSpec` -
  Returns:
  See Also:
    `Context.addServlet(java.lang.Class, java.lang.String)`

### addServlet

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

  Parameters:
    `className` -
    `pathSpec` -
  Returns:
  See Also:
    `Context.addServlet(java.lang.String, java.lang.String)`

### getAttribute

    public java.lang.Object getAttribute(java.lang.String name)

  Parameters:
    `name` -
  Returns:
  See Also:
    `ContextHandler.getAttribute(java.lang.String)`

### getAttributeNames

    public java.util.Enumeration getAttributeNames()

  Returns:
  See Also:
    `ContextHandler.getAttributeNames()`

### getAttributes

    public Attributes getAttributes()

  Returns:
  See Also:
    `ContextHandler.getAttributes()`

### getResourceBase

    public java.lang.String getResourceBase()

  Returns:
  See Also:
    `ContextHandler.getResourceBase()`

### setAttribute

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

  Parameters:
    `name` -
    `value` -
  See Also:
    `ContextHandler.setAttribute(java.lang.String, java.lang.Object)`

### setClassLoader

    public void setClassLoader(java.lang.ClassLoader classLoader)

  Parameters:
    `classLoader` -
  See Also:
    `ContextHandler.setClassLoader(java.lang.ClassLoader)`

### setContextPath

    public void setContextPath(java.lang.String contextPath)

  Parameters:
    `contextPath` -
  See Also:
    `ContextHandler.setContextPath(java.lang.String)`

### setEventListeners

    public void setEventListeners(java.util.EventListener[] eventListeners)

  Parameters:
    `eventListeners` -
  See Also:
    `ContextHandler.setEventListeners(java.util.EventListener[])`

### setResourceBase

    public void setResourceBase(java.lang.String resourceBase)

  Parameters:
    `resourceBase` -
  See Also:
    `ContextHandler.setResourceBase(java.lang.String)`

### setMaxIdleTime

    public void setMaxIdleTime(int maxIdleTime)

Copyright © 1995-2010 Mort Bay Consulting. All Rights Reserved.