Class MockResponse<T>

java.lang.Object
kong.unirest.core.MockResponse<T>
Type Parameters:
T - the body type
All Implemented Interfaces:
HttpResponse<T>

public class MockResponse<T> extends Object
A Mock Response that can be used in testing.
  • Constructor Details

    • MockResponse

      public MockResponse(int status, String statusText, T body)
      Construct a mock Response
      Parameters:
      status - the status of the response
      statusText - the status text
      body - the body
    • MockResponse

      public MockResponse(int status, String statusText, T body, MockConfig config)
      Construct a mock Response
      Parameters:
      status - the status of the response
      statusText - the status text
      body - the body
      config - a mockConfig for post-processing options
  • Method Details

    • ok

      public static <T> MockResponse<T> ok(T body)
      Construct a simple successful (200 ok) response with a body
      Type Parameters:
      T - the type of body
      Parameters:
      body - the body
      Returns:
      a MockResponse;
    • bad

      public static <T> MockResponse<T> bad(T body)
      Construct a simple failed (400 bad request) response with a body
      Type Parameters:
      T - the type of body
      Parameters:
      body - the body
      Returns:
      a MockResponse;
    • of

      public static <T> MockResponse<T> of(int status, T body)
      Construct a response with a status and body. The status text is just the string of the status
      Type Parameters:
      T - the type of body
      Parameters:
      status - the status
      body - the body
      Returns:
      a MockResponse;
    • of

      public static <T> MockResponse<T> of(int status, String statusText, T body)
      Construct a response with a status and body. The status text is just the string of the status
      Type Parameters:
      T - the type of body
      Parameters:
      status - the status
      statusText - the status text
      body - the body
      Returns:
      a MockResponse;
    • config

      public MockConfig config()
      get the MockConfig for this MockResponse
      Returns:
      the config
    • getBody

      public T getBody()
      Specified by:
      getBody in interface HttpResponse<T>
      Returns:
      the body
    • getRawBody

      protected String getRawBody()
    • getParsingError

      public Optional<UnirestParsingException> getParsingError()
      Description copied from interface: HttpResponse
      If the transformation to the body failed by an exception it will be kept here
      Specified by:
      getParsingError in interface HttpResponse<T>
      Returns:
      a possible RuntimeException. Checked exceptions are wrapped in a UnirestException
    • withHeader

      public MockResponse<T> withHeader(String key, String value)
      add a header value to the response
      Parameters:
      key - the header key
      value - the header value
      Returns:
      this MockResponse
    • failedToParse

      public MockResponse<T> failedToParse()
      Flag that there was a post-processing parsing error with the object Mapper this jams the body as a string into the parsing exception and sets a generic oops exception
      Returns:
      this MockResponse
    • failedToParse

      public MockResponse<T> failedToParse(Exception e, String originalBody)
      Flag that there was a post-processing parsing error with the object Mapper
      Parameters:
      e - the exception thrown
      originalBody - the original body before the object mapper got involved.
      Returns:
      this MockResponse
    • withConfigOptions

      public MockResponse<T> withConfigOptions(Consumer<MockConfig> c)
      Set some options on the current MockConfig.
      Parameters:
      c - a Consumer with options to set on the config
      Returns:
      this MockResponse
    • getStatus

      public int getStatus()
      Specified by:
      getStatus in interface HttpResponse<T>
      Returns:
      the HTTP status code.
    • getStatusText

      public String getStatusText()
      Specified by:
      getStatusText in interface HttpResponse<T>
      Returns:
      status text
    • getHeaders

      public Headers getHeaders()
      Specified by:
      getHeaders in interface HttpResponse<T>
      Returns:
      Response Headers (map) with same case as server response. For instance use getHeaders().getFirst("Location") and not getHeaders().getFirst("location") to get first header "Location"
    • mapBody

      public <V> V mapBody(Function<T,V> func)
      Description copied from interface: HttpResponse
      Map the body into another type
      Specified by:
      mapBody in interface HttpResponse<T>
      Type Parameters:
      V - The return type of the function
      Parameters:
      func - a function to transform a body type to something else.
      Returns:
      the return type
    • map

      public <V> HttpResponse<V> map(Function<T,V> func)
      Description copied from interface: HttpResponse
      Map the Response into another response with a different body
      Specified by:
      map in interface HttpResponse<T>
      Type Parameters:
      V - The return type of the function
      Parameters:
      func - a function to transform a body type to something else.
      Returns:
      the return type
    • setParsingException

      protected void setParsingException(String originalBody, RuntimeException e)
    • isSuccess

      public boolean isSuccess()
      Specified by:
      isSuccess in interface HttpResponse<T>
      Returns:
      true if the response was a 200-series response and no mapping exception happened, else false
    • ifSuccess

      public HttpResponse<T> ifSuccess(Consumer<HttpResponse<T>> consumer)
      Description copied from interface: HttpResponse
      If the response was a 200-series response. Invoke this consumer can be chained with ifFailure
      Specified by:
      ifSuccess in interface HttpResponse<T>
      Parameters:
      consumer - a function to consume a HttpResponse
      Returns:
      the same response
    • ifFailure

      public HttpResponse<T> ifFailure(Consumer<HttpResponse<T>> consumer)
      Description copied from interface: HttpResponse
      If the response was NOT a 200-series response or a mapping exception happened. Invoke this consumer can be chained with ifSuccess
      Specified by:
      ifFailure in interface HttpResponse<T>
      Parameters:
      consumer - a function to consume a HttpResponse
      Returns:
      the same response
    • mapError

      public <E> E mapError(Class<? extends E> errorClass)
      Description copied from interface: HttpResponse
      Map the body into a error class if the response was NOT a 200-series response or a mapping exception happened. Uses the system Object Mapper
      Specified by:
      mapError in interface HttpResponse<T>
      Type Parameters:
      E - the response type
      Parameters:
      errorClass - the class for the error
      Returns:
      the error object
    • ifFailure

      public <E> HttpResponse<T> ifFailure(Class<? extends E> errorClass, Consumer<HttpResponse<E>> consumer)
      Description copied from interface: HttpResponse
      If the response was NOT a 200-series response or a mapping exception happened. map the original body into a error type and invoke this consumer can be chained with ifSuccess
      Specified by:
      ifFailure in interface HttpResponse<T>
      Type Parameters:
      E - the type of error class to map the body
      Parameters:
      errorClass - the class of the error type to map to
      consumer - a function to consume a HttpResponse
      Returns:
      the same response
    • getCookies

      public Cookies getCookies()
      Description copied from interface: HttpResponse
      return a cookie collection parse from the set-cookie header
      Specified by:
      getCookies in interface HttpResponse<T>
      Returns:
      a Cookies collection
    • getRequestSummary

      public HttpRequestSummary getRequestSummary()
      Specified by:
      getRequestSummary in interface HttpResponse<T>
      Returns:
      a Summary of the HttpRequest that created this response