Class MockClient

  • All Implemented Interfaces:
    Client

    public class MockClient
    extends java.lang.Object
    implements Client
    A Mock client for unirest to make requests against This implements both sync and async clients
    • Field Detail

      • config

        private final java.util.function.Supplier<Config> config
      • routes

        private java.util.List<Routes> routes
      • remoteSocket

        private SocketSet remoteSocket
      • defaultResponse

        private Invocation defaultResponse
    • Constructor Detail

      • MockClient

        public MockClient​(java.util.function.Supplier<Config> config)
    • Method Detail

      • register

        public static MockClient register()
        Creates a new MockClient and registers it on the primary static UnirestInstance
        Returns:
        the Mock Client
      • register

        public static MockClient register​(UnirestInstance unirest)
        Creates a new MockClient and registers it on the Unirest instance
        Parameters:
        unirest - an instance of Unirest
        Returns:
        the Mock Client
      • clear

        public static void clear()
        Clears any MockClient from the primary instance
      • clear

        public static void clear​(UnirestInstance unirest)
        Clears any MockClient from the instance
        Parameters:
        unirest - the instance to clear the mocks from
      • request

        public <T> HttpResponse<T> request​(HttpRequest request,
                                           java.util.function.Function<RawResponse,​HttpResponse<T>> transformer,
                                           java.lang.Class<?> resultType)
        Description copied from interface: Client
        Make a request
        Specified by:
        request in interface Client
        Type Parameters:
        T - The type of the body
        Parameters:
        request - the prepared request object
        transformer - the function to transform the response
        resultType - the final body result type. This is a hint to downstream systems to make up for type erasure.
        Returns:
        a HttpResponse with a transformed body
      • request

        public <T> java.util.concurrent.CompletableFuture<HttpResponse<T>> request​(HttpRequest request,
                                                                                   java.util.function.Function<RawResponse,​HttpResponse<T>> transformer,
                                                                                   java.util.concurrent.CompletableFuture<HttpResponse<T>> callback,
                                                                                   java.lang.Class<?> resultTypes)
        Description copied from interface: Client
        Make a Async request
        Specified by:
        request in interface Client
        Type Parameters:
        T - The type of the body
        Parameters:
        request - the prepared request object
        transformer - the function to transform the response
        callback - the CompletableFuture that will handle the eventual response
        resultTypes - the final body result type. This is a hint to downstream systems to make up for type erasure.
        Returns:
        a CompletableFuture of a response
      • websocket

        public WebSocketResponse websocket​(WebSocketRequest request,
                                           java.net.http.WebSocket.Listener listener)
        Description copied from interface: Client
        Create a websocket connection
        Specified by:
        websocket in interface Client
        Parameters:
        request - the connection
        listener - (in the voice of Cicero) the listener
        Returns:
        a WebSocketResponse
      • getClient

        public java.lang.Object getClient()
        Specified by:
        getClient in interface Client
        Returns:
        the underlying client if this instance is wrapping another library.
      • expect

        public Expectation expect​(HttpMethod method,
                                  java.lang.String path)
        Start an expectation chain.
        Parameters:
        method - the Http method
        path - the base path
        Returns:
        an Expectation which can have additional criteria added to it.
      • expect

        public Expectation expect​(HttpMethod method)
        Expect ANY call to a path with this method
        Parameters:
        method - the Http Method
        Returns:
        this expectation builder
      • assertThat

        public Assert assertThat​(HttpMethod method,
                                 java.lang.String path)
        Assert a specific method and path were invoked
        Parameters:
        method - the Http method
        path - the base path
        Returns:
        an Assert object which can have additional criteria chained to it.
      • verifyAll

        public void verifyAll()
        Verify that all Expectations were invoked
      • reset

        public void reset()
        Reset all expectations
      • defaultResponse

        public ExpectedResponse defaultResponse()
        return this status for any request that doesn't match a expectation