Class MockHttpTransport

java.lang.Object
org.apache.juneau.rest.mock.MockHttpTransport
All Implemented Interfaces:
Closeable, AutoCloseable, HttpTransport

public final class MockHttpTransport extends Object implements HttpTransport
A programmable in-process HttpTransport for unit testing.

Register one or more route handlers, or a catch-all handler, and the transport will dispatch each TransportRequest to the appropriate handler without any real network activity.

Example:

transport = MockHttpTransport.builder() .on("GET", "/hello", req -> TransportResponse.builder() .statusCode(200) .reasonPhrase("OK") .header("Content-Type", "text/plain") .body(new ByteArrayInputStream("Hello, World!".getBytes(StandardCharsets.UTF_8))) .build()) .build(); RestClient client = RestClient.builder() .transport(transport) .build();

See Also:
Since:
9.2.1
  • Method Details

    • builder

    • of

      public static MockHttpTransport of(int statusCode, String body)
      Creates a simple mock transport that always returns the given status code and body string.
      Parameters:
      statusCode - The HTTP status code to return.
      body - The body string to return (UTF-8 encoded). May be null.
      Returns:
      A new instance. Never null.
    • execute

      Description copied from interface: HttpTransport
      Executes the given request and returns the response.

      The caller is responsible for closing the returned TransportResponse after consuming the body.

      Specified by:
      execute in interface HttpTransport
      Parameters:
      request - The fully-resolved request. Must not be null.
      Returns:
      The response. Never null.
      Throws:
      TransportException - If a network-level error occurs.
    • supportsUrlPolicy

      public boolean supportsUrlPolicy()
      Description copied from interface: HttpTransport
      Returns true if this transport can honor the SSRF guardrail's connect-time contract for a policy-covered request (TransportRequest.isSsrfGuardActive()): pin-on-connect to a resolved address that has passed the deny-private policy, and Juneau-controlled redirect revalidation (no fallback to the transport's own auto-follow).

      The default implementation returns false (fail closed): an unknown/caller-supplied transport that has not affirmatively opted in is presumed unable to honor the contract, so RestRequest#run() rejects policy-covered requests against it rather than silently connecting without the guardrail. First-party transports that construct and fully control their own underlying HTTP client override this to return true.

      Specified by:
      supportsUrlPolicy in interface HttpTransport
      Returns:
      true if this transport supports pin-on-connect + redirect revalidation for policy-covered requests.
    • getRecordedRequests

      Returns an unmodifiable view of all requests received since creation (only available when recording is enabled).
      Returns:
      The recorded requests. Empty if recording is disabled.
    • clearRecordedRequests

      public void clearRecordedRequests()
      Clears the list of recorded requests.