Package com.shri.restinpeace.mock
Class MockResponse
java.lang.Object
com.shri.restinpeace.mock.MockResponse
A canned response for
MockRestServer to send back for a matching
request.-
Method Summary
Modifier and TypeMethodDescriptionstatic MockResponseSimulates 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 (akong.unirest.UnirestExceptionthrown directly, not wrapped inRestInPeaceHttpException, and always retried by@Retryregardless ofretryOnStatus) instead of only ever seeing a non-2xx status.delay(long delayMillis) Delays sending this response bydelayMillis, to simulate a slow server - the way to prove@Timeout(readMillis = ...)(orRipClientConfig.readTimeoutMillis(...)) actually fires, rather than assuming it does because the annotation is present.Adds a response header.static MockResponseA200 OKresponse, serializingbodywith the same UnirestObjectMapperRIP itself delegates to (the oneRIP.setObjectMapper(kong.unirest.ObjectMapper)sets) - so a test can hand this a plain object instead of hand-writing a JSON string.static MockResponseA204 No Contentresponse.static MockResponseA304 Not Modifiedresponse, for a test simulating a cache revalidation hit - RIP's response cache sendsIf-None-Match/If-Modified-Sinceautomatically once a cachedGETentry goes stale, and expects exactly this status back to keep serving the previously-cached body instead of treating the response as a fresh one.static MockResponseA200 OKresponse with a body, defaultingContent-Typetoapplication/json- override viaheader(java.lang.String, java.lang.String)for a plain-text/other body.static MockResponsestatus(int status, byte[] body) A response with the given status and raw bytes, for a binary (byte[]/File-returning) call.static MockResponseA response with the given status and a raw body, no default headers.
-
Method Details
-
ok
A200 OKresponse with a body, defaultingContent-Typetoapplication/json- override viaheader(java.lang.String, java.lang.String)for a plain-text/other body.- Parameters:
body- the response body- Returns:
- the new response
-
json
A200 OKresponse, serializingbodywith the same UnirestObjectMapperRIP itself delegates to (the oneRIP.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-clientRipClientConfig's ownobjectMapper(...)isn't necessarily decoding with the same one - pre-serialize to aStringand useok(String)instead in that case.- Parameters:
body- the object to serialize as the response body- Returns:
- the new response
- Throws:
RestInPeaceException- if noObjectMapperis configured - callRIP.setObjectMapper(kong.unirest.ObjectMapper)first, same as RIP itself requires for JSON (de)serialization
-
noContent
A204 No Contentresponse.- Returns:
- the new response
-
notModified
A304 Not Modifiedresponse, for a test simulating a cache revalidation hit - RIP's response cache sendsIf-None-Match/If-Modified-Sinceautomatically once a cachedGETentry 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 withheader(java.lang.String, java.lang.String)for a freshETag/Cache-Controlthe revalidation should refresh the cache entry's freshness window with.- Returns:
- the new response
-
status
A response with the given status and a raw body, no default headers.- Parameters:
status- the HTTP status codebody- the response body- Returns:
- the new response
-
status
A response with the given status and raw bytes, for a binary (byte[]/File-returning) call.- Parameters:
status- the HTTP status codebody- the response body- Returns:
- the new response
-
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 (akong.unirest.UnirestExceptionthrown directly, not wrapped inRestInPeaceHttpException, and always retried by@Retryregardless ofretryOnStatus) instead of only ever seeing a non-2xx status. Anyheader(java.lang.String, java.lang.String)ordelay(long)set on the returned response is ignored, since no response is ever sent.- Returns:
- the new response
-
header
Adds a response header. Calling this more than once for the samenameadds an additional value rather than replacing the previous one - the way HTTP itself allows a header to repeat (e.g. multipleSet-Cookieheaders on one response) - so send several values by calling this once per value, in order.- Parameters:
name- the header namevalue- the header value- Returns:
- this response
-
delay
Delays sending this response bydelayMillis, to simulate a slow server - the way to prove@Timeout(readMillis = ...)(orRipClientConfig.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 loopbackMockRestServeralways is.- Parameters:
delayMillis- how long to wait before sending this response- Returns:
- this response
-