Class MockResponse

java.lang.Object
com.shri.restinpeace.mock.MockResponse

public final class MockResponse extends Object
A canned response for MockRestServer to send back for a matching request.
  • Method Summary

    Modifier and Type
    Method
    Description
    Simulates a transport-level failure - the connection is closed before any response is sent, instead of returning an HTTP status - so a test can exercise RIP's "no response at all" error path (a kong.unirest.UnirestException thrown directly, not wrapped in RestInPeaceHttpException, and always retried by @Retry regardless of retryOnStatus) instead of only ever seeing a non-2xx status.
    delay(long delayMillis)
    Delays sending this response by delayMillis, to simulate a slow server - the way to prove @Timeout(readMillis = ...) (or RipClientConfig.readTimeoutMillis(...)) actually fires, rather than assuming it does because the annotation is present.
    header(String name, String value)
    Adds a response header.
    json(Object body)
    A 200 OK response, serializing body with the same Unirest ObjectMapper RIP itself delegates to (the one RIP.setObjectMapper(kong.unirest.ObjectMapper) sets) - so a test can hand this a plain object instead of hand-writing a JSON string.
    A 204 No Content response.
    A 304 Not Modified response, for a test simulating a cache revalidation hit - RIP's response cache sends If-None-Match/ If-Modified-Since automatically once a cached GET entry goes stale, and expects exactly this status back to keep serving the previously-cached body instead of treating the response as a fresh one.
    ok(String body)
    A 200 OK response with a body, defaulting Content-Type to application/json - override via header(java.lang.String, java.lang.String) for a plain-text/other body.
    status(int status, byte[] body)
    A response with the given status and raw bytes, for a binary (byte[]/File-returning) call.
    status(int status, String body)
    A response with the given status and a raw body, no default headers.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • ok

      public static MockResponse ok(String body)
      A 200 OK response with a body, defaulting Content-Type to application/json - override via header(java.lang.String, java.lang.String) for a plain-text/other body.
      Parameters:
      body - the response body
      Returns:
      the new response
    • json

      public static MockResponse json(Object body)
      A 200 OK response, serializing body with the same Unirest ObjectMapper RIP itself delegates to (the one RIP.setObjectMapper(kong.unirest.ObjectMapper) sets) - so a test can hand this a plain object instead of hand-writing a JSON string. Uses the globally-configured mapper specifically; a method under test whose client was built with a per-client RipClientConfig's own objectMapper(...) isn't necessarily decoding with the same one - pre-serialize to a String and use ok(String) instead in that case.
      Parameters:
      body - the object to serialize as the response body
      Returns:
      the new response
      Throws:
      RestInPeaceException - if no ObjectMapper is configured - call RIP.setObjectMapper(kong.unirest.ObjectMapper) first, same as RIP itself requires for JSON (de)serialization
    • noContent

      public static MockResponse noContent()
      A 204 No Content response.
      Returns:
      the new response
    • notModified

      public static MockResponse notModified()
      A 304 Not Modified response, for a test simulating a cache revalidation hit - RIP's response cache sends If-None-Match/ If-Modified-Since automatically once a cached GET entry goes stale, and expects exactly this status back to keep serving the previously-cached body instead of treating the response as a fresh one. Combine with header(java.lang.String, java.lang.String) for a fresh ETag/Cache-Control the revalidation should refresh the cache entry's freshness window with.
      Returns:
      the new response
    • status

      public static MockResponse status(int status, String body)
      A response with the given status and a raw body, no default headers.
      Parameters:
      status - the HTTP status code
      body - the response body
      Returns:
      the new response
    • status

      public static MockResponse status(int status, byte[] body)
      A response with the given status and raw bytes, for a binary (byte[]/File-returning) call.
      Parameters:
      status - the HTTP status code
      body - the response body
      Returns:
      the new response
    • connectionFailure

      public static MockResponse connectionFailure()
      Simulates a transport-level failure - the connection is closed before any response is sent, instead of returning an HTTP status - so a test can exercise RIP's "no response at all" error path (a kong.unirest.UnirestException thrown directly, not wrapped in RestInPeaceHttpException, and always retried by @Retry regardless of retryOnStatus) instead of only ever seeing a non-2xx status. Any header(java.lang.String, java.lang.String) or delay(long) set on the returned response is ignored, since no response is ever sent.
      Returns:
      the new response
    • header

      public MockResponse header(String name, String value)
      Adds a response header. Calling this more than once for the same name adds an additional value rather than replacing the previous one - the way HTTP itself allows a header to repeat (e.g. multiple Set-Cookie headers on one response) - so send several values by calling this once per value, in order.
      Parameters:
      name - the header name
      value - the header value
      Returns:
      this response
    • delay

      public MockResponse delay(long delayMillis)
      Delays sending this response by delayMillis, to simulate a slow server - the way to prove @Timeout(readMillis = ...) (or RipClientConfig.readTimeoutMillis(...)) actually fires, rather than assuming it does because the annotation is present. The delay happens before any response bytes are sent, simulating the server being slow to start responding - exactly what a read timeout guards against on a connection that's already open, as one to a loopback MockRestServer always is.
      Parameters:
      delayMillis - how long to wait before sending this response
      Returns:
      this response