Class MockClient

java.lang.Object
kong.unirest.core.MockClient
All Implemented Interfaces:
Client

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

  • Constructor Details

  • Method Details

    • 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, Function<RawResponse,HttpResponse<T>> transformer, 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
    • findExpecation

      private Routes findExpecation(HttpRequest request)
    • createNewPath

      private Routes createNewPath(HttpRequest request)
    • request

      public <T> CompletableFuture<HttpResponse<T>> request(HttpRequest request, Function<RawResponse,HttpResponse<T>> transformer, CompletableFuture<HttpResponse<T>> callback, 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, 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
    • serversSocket

      public SocketSet<MockWebSocket,MockListener> serversSocket()
    • getClient

      public 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, 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, 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.
    • findByPath

      private Optional<Routes> findByPath(HttpMethod get, Path path)
    • 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