Package kong.unirest.core
Class MockResponse<T>
- java.lang.Object
-
- kong.unirest.core.BaseResponse<T>
-
- kong.unirest.core.MockResponse<T>
-
- Type Parameters:
T
- the body type
- All Implemented Interfaces:
HttpResponse<T>
public class MockResponse<T> extends BaseResponse<T>
A Mock Response that can be used in testing.
-
-
Field Summary
Fields Modifier and Type Field Description private T
body
private java.util.Optional<UnirestParsingException>
ex
private MockConfig
mockConfig
-
Constructor Summary
Constructors Constructor Description MockResponse(int status, java.lang.String statusText, T body)
Construct a mock ResponseMockResponse(int status, java.lang.String statusText, T body, MockConfig config)
Construct a mock Response
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> MockResponse<T>
bad(T body)
Construct a simple failed (400 bad request) response with a bodyMockConfig
config()
get the MockConfig for this MockResponseMockResponse<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 exceptionMockResponse<T>
failedToParse(java.lang.Exception e, java.lang.String originalBody)
Flag that there was a post-processing parsing error with the object MapperT
getBody()
java.util.Optional<UnirestParsingException>
getParsingError()
If the transformation to the body failed by an exception it will be kept hereprotected java.lang.String
getRawBody()
static <T> MockResponse<T>
of(int status, java.lang.String statusText, T body)
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 bodyMockResponse<T>
withConfigOptions(java.util.function.Consumer<MockConfig> c)
Set some options on the current MockConfig.MockResponse<T>
withHeader(java.lang.String key, java.lang.String value)
add a header value to the response-
Methods inherited from class kong.unirest.core.BaseResponse
getCookies, getHeaders, getRequestSummary, getStatus, getStatusText, ifFailure, ifFailure, ifSuccess, isSuccess, map, mapBody, mapError, setParsingException
-
-
-
-
Field Detail
-
mockConfig
private final MockConfig mockConfig
-
ex
private java.util.Optional<UnirestParsingException> ex
-
body
private final T body
-
-
Constructor Detail
-
MockResponse
public MockResponse(int status, java.lang.String statusText, T body)
Construct a mock Response- Parameters:
status
- the status of the responsestatusText
- the status textbody
- the body
-
MockResponse
public MockResponse(int status, java.lang.String statusText, T body, MockConfig config)
Construct a mock Response- Parameters:
status
- the status of the responsestatusText
- the status textbody
- the bodyconfig
- a mockConfig for post-processing options
-
-
Method Detail
-
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 statusbody
- the body- Returns:
- a MockResponse;
-
of
public static <T> MockResponse<T> of(int status, java.lang.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 statusstatusText
- the status textbody
- 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 interfaceHttpResponse<T>
- Specified by:
getBody
in classBaseResponse<T>
- Returns:
- the body
-
getRawBody
protected java.lang.String getRawBody()
- Specified by:
getRawBody
in classBaseResponse<T>
-
getParsingError
public java.util.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 interfaceHttpResponse<T>
- Overrides:
getParsingError
in classBaseResponse<T>
- Returns:
- a possible RuntimeException. Checked exceptions are wrapped in a UnirestException
-
withHeader
public MockResponse<T> withHeader(java.lang.String key, java.lang.String value)
add a header value to the response- Parameters:
key
- the header keyvalue
- 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(java.lang.Exception e, java.lang.String originalBody)
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
public MockResponse<T> withConfigOptions(java.util.function.Consumer<MockConfig> c)
Set some options on the current MockConfig.- Parameters:
c
- a Consumer with options to set on the config- Returns:
- this MockResponse
-
-