Class LoopbackRestServer

All Implemented Interfaces:
Disposable, Observable<RestEndpoint>, Observers<RestEndpoint, RestServer>, RestServer, BaseLocatorAccessor, MediaTypeFactoryLookup, MediaTypeFactoryLookup.MutableMediaTypeFactoryLookup, RealmAccessor
Direct Known Subclasses:
LoopbackRestServerSingleton

public class LoopbackRestServer extends AbstractRestServer

Lightweight in memory implementation of a RestServer used for loopback style REST request processing without opening physical network connections.

The LoopbackRestServer is primarily intended for:

  • testing RestEndpoint implementations
  • testing RestRequestConsumer implementations
  • integration testing of REST request processing pipelines
  • simulating HTTP request dispatching in memory
  • testing request/response marshaling behavior
  • testing interceptor and observer integration

Instead of opening a physical HTTP or HTTPS server socket, requests are directly dispatched to the registered REST infrastructure through the onHttpRequest(InetSocketAddress, InetSocketAddress, HttpMethod, Url, RequestHeaderFields, InputStream, HttpServerResponse) methods.

This implementation internally reuses the request processing logic provided by AbstractRestServer, thereby enabling realistic simulation of:

  • REST endpoint resolution
  • HTTP method dispatching
  • media type negotiation
  • request unmarshaling
  • response marshaling
  • observer and interceptor execution

The server supports both textual request bodies as well as arbitrary InputStream based request payloads.

Unlike JdkHttpRestServer, this implementation performs no actual network communication and therefore avoids:

  • socket allocation
  • HTTP protocol overhead
  • TLS/SSL setup
  • port binding

This makes the LoopbackRestServer especially suitable for fast unit and integration tests requiring deterministic in process REST request handling.

See Also:
  • Constructor Details

    • LoopbackRestServer

      public LoopbackRestServer()
      Instantiates a new loopback rest server.
    • LoopbackRestServer

      public LoopbackRestServer(String aBaseLocator)
      Instantiates a new loopback rest server.
      Parameters:
      aBaseLocator - The base locator to be used.
  • Method Details

    • onHttpRequest

      public void onHttpRequest(InetSocketAddress aLocalAddress, InetSocketAddress aClientAddress, HttpMethod aHttpMethod, Url aUrl, RequestHeaderFields aRequestHeaderFields, String aHttpBody, HttpServerResponse aHttpServerResponse) throws HttpStatusException
      Invoke this method to simulate a REST request to be handled by the according registered RestEndpoint instances:.
      Parameters:
      aLocalAddress - the local address
      aClientAddress - the client address
      aHttpMethod - The method for the simulated REST request to be used
      aUrl - The query string part of the request.
      aRequestHeaderFields - The simulated REST request's Header-Fields to be used
      aHttpBody - The raw body for the simulated REST request to be used.
      aHttpServerResponse - The HttpServerResponse to be filled with the complete result of processing the request.
      Throws:
      HttpStatusException - thrown in case of HTTP status code related exceptions (e.g. as of a HTTP response was of an erroneous status).
    • onHttpRequest

      public void onHttpRequest(InetSocketAddress aLocalAddress, InetSocketAddress aClientAddress, HttpMethod aHttpMethod, Url aUrl, RequestHeaderFields aRequestHeaderFields, InputStream aHttpInputStream, HttpServerResponse aHttpServerResponse) throws HttpStatusException
      Invoke this method to simulate a REST request to be handled by the according registered RestEndpoint instances:.
      Overrides:
      onHttpRequest in class AbstractRestServer
      Parameters:
      aLocalAddress - the local address
      aClientAddress - the client address
      aHttpMethod - The method for the simulated REST request to be used
      aUrl - The query string part of the request.
      aRequestHeaderFields - The simulated REST request's Header-Fields to be used
      aHttpInputStream - The HTTP InputStream representing the body for the simulated REST request to be used.
      aHttpServerResponse - The HttpServerResponse to be filled with the complete result of processing the request.
      Throws:
      HttpStatusException - thrown in case of HTTP status code related exceptions (e.g. as of a HTTP response was of an erroneous status).
    • withObserversActive

      public LoopbackRestServer withObserversActive(boolean isActive)
    • withEnableObservers

      public LoopbackRestServer withEnableObservers()
    • withDisableObservers

      public LoopbackRestServer withDisableObservers()