Class AppServer


  • public class AppServer
    extends Object
    Tiny web application server for unit testing.

    Tests should start the server in their setUp() method and stop the server in their tearDown() method. Only while started the server's URL and/or port number can be obtained.

    • Constructor Detail

      • AppServer

        public AppServer()
        Constructor for AppServer.
      • AppServer

        public AppServer​(int port)
        Constructor for AppServer.
        Parameters:
        port - the http port number; may be zero to allocate a port dynamically
        Since:
        4.2
      • AppServer

        public AppServer​(int port,
                         int sslPort)
        Constructor for AppServer.
        Parameters:
        port - for http, may be zero to allocate a port dynamically
        sslPort - for https,may be zero to allocate a port dynamically. If negative, the server will be set up without https support.
        Since:
        4.9
    • Method Detail

      • addContext

        public org.eclipse.jetty.servlet.ServletContextHandler addContext​(String path)
        Create a new servlet context within the server.

        This method should be invoked before the server is started, once for each context the caller wants to register.

        Parameters:
        path - path of the context; use "/" for the root context if binding to the root is desired.
        Returns:
        the context to add servlets into.
      • authBasic

        public org.eclipse.jetty.servlet.ServletContextHandler authBasic​(org.eclipse.jetty.servlet.ServletContextHandler ctx,
                                                                         String... methods)
        Configure basic authentication.
        Parameters:
        ctx -
        methods -
        Returns:
        servlet context handler
      • setUp

        public void setUp()
                   throws Exception
        Start the server on a random local port.
        Throws:
        Exception - the server cannot be started, testing is not possible.
      • tearDown

        public void tearDown()
                      throws Exception
        Shutdown the server.
        Throws:
        Exception - the server refuses to halt, or wasn't running.
      • getURI

        public URI getURI()
        Get the URI to reference this server.

        The returned URI includes the proper host name and port number, but does not contain a path.

        Returns:
        URI to reference this server's root context.
      • getPort

        public int getPort()
        Get port.
        Returns:
        the local port number the server is listening on.
      • getSecurePort

        public int getSecurePort()
        Get secure port.
        Returns:
        the HTTPS port or -1 if not configured.
      • getRequests

        public List<AccessEvent> getRequests()
        Get requests.
        Returns:
        all requests since the server was started.
      • getRequests

        public List<AccessEvent> getRequests​(URIish base,
                                             String path)
        Get requests.
        Parameters:
        base - base URI used to access the server.
        path - the path to locate requests for, relative to base.
        Returns:
        all requests which match the given path.
      • getRequests

        public List<AccessEvent> getRequests​(String path)
        Get requests.
        Parameters:
        path - the path to locate requests for.
        Returns:
        all requests which match the given path.