Package kong.unirest.core
Class MockResponse<T>
java.lang.Object
kong.unirest.core.MockResponse<T>
- Type Parameters:
T- the body type
- All Implemented Interfaces:
HttpResponse<T>
A Mock Response that can be used in testing.
-
Constructor Summary
ConstructorsConstructorDescriptionMockResponse(int status, String statusText, T body) Construct a mock ResponseMockResponse(int status, String statusText, T body, MockConfig config) Construct a mock Response -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> MockResponse<T> bad(T body) Construct a simple failed (400 bad request) response with a bodyconfig()get the MockConfig for this MockResponseFlag 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 exceptionfailedToParse(Exception e, String originalBody) Flag that there was a post-processing parsing error with the object MappergetBody()return a cookie collection parse from the set-cookie headerIf the transformation to the body failed by an exception it will be kept hereprotected Stringint<E> HttpResponse<T> ifFailure(Class<? extends E> errorClass, Consumer<HttpResponse<E>> consumer) 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 ifSuccessifFailure(Consumer<HttpResponse<T>> consumer) If the response was NOT a 200-series response or a mapping exception happened.ifSuccess(Consumer<HttpResponse<T>> consumer) If the response was a 200-series response.boolean<V> HttpResponse<V> Map the Response into another response with a different body<V> VMap the body into another type<E> EMap the body into a error class if the response was NOT a 200-series response or a mapping exception happened.static <T> MockResponse<T> Construct a response with a status and body.static <T> MockResponse<T> of(int status, T body) Construct a response with a status and body.static <T> MockResponse<T> ok(T body) Construct a simple successful (200 ok) response with a bodyprotected voidsetParsingException(String originalBody, RuntimeException e) Set some options on the current MockConfig.withHeader(String key, String value) add a header value to the response
-
Constructor Details
-
MockResponse
Construct a mock Response- Parameters:
status- the status of the responsestatusText- the status textbody- the body
-
MockResponse
Construct a mock Response- Parameters:
status- the status of the responsestatusText- the status textbody- the bodyconfig- a mockConfig for post-processing options
-
-
Method Details
-
ok
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
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
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 statusbody- the body- Returns:
- a MockResponse;
-
of
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 statusstatusText- the status textbody- the body- Returns:
- a MockResponse;
-
config
get the MockConfig for this MockResponse- Returns:
- the config
-
getBody
- Specified by:
getBodyin interfaceHttpResponse<T>- Returns:
- the body
-
getRawBody
-
getParsingError
Description copied from interface:HttpResponseIf the transformation to the body failed by an exception it will be kept here- Specified by:
getParsingErrorin interfaceHttpResponse<T>- Returns:
- a possible RuntimeException. Checked exceptions are wrapped in a UnirestException
-
withHeader
add a header value to the response- Parameters:
key- the header keyvalue- the header value- Returns:
- this MockResponse
-
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
Flag that there was a post-processing parsing error with the object Mapper- Parameters:
e- the exception thrownoriginalBody- the original body before the object mapper got involved.- Returns:
- this MockResponse
-
withConfigOptions
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:
getStatusin interfaceHttpResponse<T>- Returns:
- the HTTP status code.
-
getStatusText
- Specified by:
getStatusTextin interfaceHttpResponse<T>- Returns:
- status text
-
getHeaders
- Specified by:
getHeadersin interfaceHttpResponse<T>- Returns:
- Response Headers (map) with same case as server response.
For instance use
getHeaders().getFirst("Location")and notgetHeaders().getFirst("location")to get first header "Location"
-
mapBody
Description copied from interface:HttpResponseMap the body into another type- Specified by:
mapBodyin interfaceHttpResponse<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
Description copied from interface:HttpResponseMap the Response into another response with a different body- Specified by:
mapin interfaceHttpResponse<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
-
isSuccess
public boolean isSuccess()- Specified by:
isSuccessin interfaceHttpResponse<T>- Returns:
- true if the response was a 200-series response and no mapping exception happened, else false
-
ifSuccess
Description copied from interface:HttpResponseIf the response was a 200-series response. Invoke this consumer can be chained with ifFailure- Specified by:
ifSuccessin interfaceHttpResponse<T>- Parameters:
consumer- a function to consume a HttpResponse- Returns:
- the same response
-
ifFailure
Description copied from interface:HttpResponseIf the response was NOT a 200-series response or a mapping exception happened. Invoke this consumer can be chained with ifSuccess- Specified by:
ifFailurein interfaceHttpResponse<T>- Parameters:
consumer- a function to consume a HttpResponse- Returns:
- the same response
-
mapError
Description copied from interface:HttpResponseMap 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:
mapErrorin interfaceHttpResponse<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:HttpResponseIf 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:
ifFailurein interfaceHttpResponse<T>- Type Parameters:
E- the type of error class to map the body- Parameters:
errorClass- the class of the error type to map toconsumer- a function to consume a HttpResponse- Returns:
- the same response
-
getCookies
Description copied from interface:HttpResponsereturn a cookie collection parse from the set-cookie header- Specified by:
getCookiesin interfaceHttpResponse<T>- Returns:
- a Cookies collection
-
getRequestSummary
- Specified by:
getRequestSummaryin interfaceHttpResponse<T>- Returns:
- a Summary of the HttpRequest that created this response
-