Class RestResponse
- All Implemented Interfaces:
HttpMessage
,HttpResponse
- Direct Known Subclasses:
MockRestResponse
Instances of this class are created by calling the RestRequest.run()
method.
See Also:
-
Constructor Summary
ModifierConstructorDescriptionprotected
RestResponse
(RestClient client, RestRequest request, HttpResponse response, Parser parser) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a header to this message.void
Adds a header to this message.Provides the ability to perform fluent-style assertions on the response character encoding.Provides the ability to perform fluent-style assertions on this response body.assertContent
(String value) Provides the ability to perform fluent-style assertions on this response body.assertContentMatches
(String value) Provides the ability to perform fluent-style assertions on this response body.assertHeader
(String name) Provides the ability to perform fluent-style assertions on a response header.Provides the ability to perform fluent-style assertions on the responseStatusLine
object.assertStatus
(int value) Provides the ability to perform fluent-style assertions on the response status code.Caches the response body so that it can be read as a stream multiple times.consume()
Consumes the response body.boolean
containsHeader
(String name) Checks if a certain header is present in this message.Returns all the headers of this message.Shortcut for retrieving the response charset from theContent-Type header.Returns the body of the response.Shortcut for retrieving the response content type from theContent-Type header.Obtains the message entity of this response.getFirstHeader
(String name) Returns the first header with a specified name of this message.Returns the response header with the specified name.getHeaders
(String name) Returns all the headers with a specified name of this message.getLastHeader
(String name) Returns the last header with a specified name of this message.Obtains the locale of this response.Deprecated.Use configuration classes providedorg.apache.http.config andorg.apache.http.client.config .protected HttpPartParserSession
Creates a session of the client-default parat parser.protected HttpPartParserSession
getPartParserSession
(HttpPartParser parser) Creates a session of the specified part parser.Returns the protocol version this message is compatible with.Returns the status line reason phrase of the response.Returns the request object that created this response object.int
Returns the status code of the response.Obtains the status line of this response.getStringHeader
(String name) Shortcut for callinggetHeader(name).asString()
.Returns an iterator of all the headers.headerIterator
(String name) Returns an iterator of the headers with a given name.Logs a message.Logs a message.void
removeHeader
(Header header) Removes a header from this message.void
removeHeaders
(String name) Removes all headers with a certain name from this message.void
setEntity
(HttpEntity entity) Associates a response entity with this response.void
Overwrites the first header with the same name.void
Overwrites the first header with the same name.void
setHeaders
(Header[] headers) Overwrites all the headers in the message.void
Changes the locale of this response.void
setParams
(HttpParams params) Deprecated.Use configuration classes providedorg.apache.http.config andorg.apache.http.client.config .void
setReasonPhrase
(String reason) Updates the status line of this response with a new reason phrase.void
setStatusCode
(int code) Updates the status line of this response with a new status code.void
setStatusLine
(ProtocolVersion ver, int code) Sets the status line of this response.void
setStatusLine
(ProtocolVersion ver, int code, String reason) Sets the status line of this response with a reason phrase.void
setStatusLine
(StatusLine statusline) Sets the status line of this response.
-
Constructor Details
-
RestResponse
protected RestResponse(RestClient client, RestRequest request, HttpResponse response, Parser parser) Constructor.- Parameters:
client
- The RestClient that created this response.request
- The REST request.response
- The HTTP response. Can benull .parser
- The overridden parser passed intoRestRequest.parser(Parser)
.
-
-
Method Details
-
getRequest
Returns the request object that created this response object.- Returns:
- The request object that created this response object.
-
consume
Consumes the response body.This is equivalent to closing the input stream.
- Returns:
- This object.
- Throws:
RestCallException
- If one of theRestCallInterceptors
threw an exception.
-
getStatusCode
Returns the status code of the response. Shortcut for callinggetStatusLine().getStatusCode()
.- Returns:
- The status code of the response.
-
getReasonPhrase
Returns the status line reason phrase of the response. Shortcut for callinggetStatusLine().getReasonPhrase()
.- Returns:
- The status line reason phrase of the response.
-
assertStatus
Provides the ability to perform fluent-style assertions on the responseStatusLine
object.Examples:
MyBean
bean =client .get(URI ) .run() .assertStatus().asCode().is(200) .getContent().as(MyBean.class );- Returns:
- A new fluent assertion object.
-
assertStatus
Provides the ability to perform fluent-style assertions on the response status code.Examples:
MyBean
bean =client .get(URI ) .run() .assertStatus(200) .getContent().as(MyBean.class );- Parameters:
value
- The value to assert.- Returns:
- A new fluent assertion object.
-
getStringHeader
Shortcut for callinggetHeader(name).asString()
.- Parameters:
name
- The header name.- Returns:
- The header value, never
null
-
getCharacterEncoding
Shortcut for retrieving the response charset from theContent-Type header.- Returns:
- The response charset.
- Throws:
RestCallException
- If REST call failed.
-
getContentType
Shortcut for retrieving the response content type from theContent-Type header.This is equivalent to calling
getHeader( ."Content-Type" ).as(ContentType.class )- Returns:
- The response charset.
- Throws:
RestCallException
- If REST call failed.
-
assertCharset
Provides the ability to perform fluent-style assertions on the response character encoding.Examples:
// Validates that the response content charset is UTF-8. client .get(URI ) .run() .assertCharset().is("utf-8" );- Returns:
- A new fluent assertion object.
- Throws:
RestCallException
- If REST call failed.
-
assertHeader
Provides the ability to perform fluent-style assertions on a response header.Examples:
// Validates the content type header is provided. client .get(URI ) .run() .assertHeader("Content-Type" ).exists();// Validates the content type is JSON. client .get(URI ) .run() .assertHeader("Content-Type" ).is("application/json" );// Validates the content type is JSON using test predicate. client .get(URI ) .run() .assertHeader("Content-Type" ).is(x ->x .equals("application/json" ));// Validates the content type is JSON by just checking for substring. client .get(URI ) .run() .assertHeader("Content-Type" ).contains("json" );// Validates the content type is JSON using regular expression. client .get(URI ) .run() .assertHeader("Content-Type" ).isPattern(".*json.*" );// Validates the content type is JSON using case-insensitive regular expression. client .get(URI ) .run() .assertHeader("Content-Type" ).isPattern(".*json.*" ,CASE_INSENSITIVE );The assertion test returns the original response object allowing you to chain multiple requests like so:
// Validates the header and converts it to a bean. MediaTypemediaType =client .get(URI ) .run() .assertHeader("Content-Type" ).isNotEmpty() .assertHeader("Content-Type" ).isPattern(".*json.*" ) .getHeader("Content-Type" ).as(MediaType.class );- Parameters:
name
- The header name.- Returns:
- A new fluent assertion object.
-
getContent
Returns the body of the response. This method can be called multiple times returning the same response body each time.- Returns:
- The body of the response.
-
assertContent
Provides the ability to perform fluent-style assertions on this response body.Examples:
// Validates the response body equals the text "OK". client .get(URI ) .run() .assertContent().is("OK" );// Validates the response body contains the text "OK". client .get(URI ) .run() .assertContent().isContains("OK" );// Validates the response body passes a predicate test. client .get(URI ) .run() .assertContent().is(x ->x .contains("OK" ));// Validates the response body matches a regular expression. client .get(URI ) .run() .assertContent().isPattern(".*OK.*" );// Validates the response body matches a regular expression using regex flags. client .get(URI ) .run() .assertContent().isPattern(".*OK.*" ,MULTILINE &CASE_INSENSITIVE );// Validates the response body matches a regular expression in the form of an existing Pattern. Patternpattern = Pattern.compile (".*OK.*" );client .get(URI ) .run() .assertContent().isPattern(pattern );The assertion test returns the original response object allowing you to chain multiple requests like so:
// Validates the response body matches a regular expression. MyBeanbean =client .get(URI ) .run() .assertContent().isPattern(".*OK.*" ); .assertContent().isNotPattern(".*ERROR.*" ) .getContent().as(MyBean.class );Notes:
-
If no charset was found on the
Content-Type
response header,"UTF-8" is assumed. -
When using this method, the body is automatically cached by calling the
ResponseContent.cache()
. - The input stream is automatically closed after this call.
- Returns:
- A new fluent assertion object.
-
If no charset was found on the
-
assertContent
Provides the ability to perform fluent-style assertions on this response body.A shortcut for calling
assertContent().is( .value )Examples:
// Validates the response body equals the text "OK". client .get(URI ) .run() .assertContent("OK" );- Parameters:
value
- The value to assert.- Returns:
- This object.
-
assertContentMatches
Provides the ability to perform fluent-style assertions on this response body.A shortcut for calling
assertContent().asString().isMatches( .value )- Parameters:
value
- The value to assert.- Returns:
- This object.
- See Also:
-
cacheContent
Caches the response body so that it can be read as a stream multiple times. This is equivalent to calling the following:getContent().cache();
- Returns:
- The body of the response.
-
log
Logs a message.- Parameters:
level
- The log level.t
- The throwable cause.msg
- The message withMessageFormat
-style arguments.args
- The arguments.- Returns:
- This object.
-
log
Logs a message.- Parameters:
level
- The log level.msg
- The message withMessageFormat
-style arguments.args
- The arguments.- Returns:
- This object.
-
getStatusLine
Obtains the status line of this response. The status line can be set using one of the setStatusLine methods, or it can be initialized in a constructor.- Specified by:
getStatusLine
in interfaceHttpResponse
- Returns:
- The status line, or
null if not yet set.
-
setStatusLine
Sets the status line of this response.- Specified by:
setStatusLine
in interfaceHttpResponse
- Parameters:
statusline
- The status line of this response
-
setStatusLine
Sets the status line of this response.The reason phrase will be determined based on the current locale.
- Specified by:
setStatusLine
in interfaceHttpResponse
- Parameters:
ver
- The HTTP version.code
- The status code.
-
setStatusLine
Sets the status line of this response with a reason phrase.- Specified by:
setStatusLine
in interfaceHttpResponse
- Parameters:
ver
- The HTTP version.code
- The status code.reason
- The reason phrase, ornull to omit.
-
setStatusCode
Updates the status line of this response with a new status code.- Specified by:
setStatusCode
in interfaceHttpResponse
- Parameters:
code
- The HTTP status code.- Throws:
IllegalStateException
- If the status line has not be set.
-
setReasonPhrase
Updates the status line of this response with a new reason phrase.- Specified by:
setReasonPhrase
in interfaceHttpResponse
- Parameters:
reason
- The new reason phrase as a single-line string, ornull to unset the reason phrase.- Throws:
IllegalStateException
- If the status line has not be set.
-
getEntity
Obtains the message entity of this response.The entity is provided by calling setEntity.
Notes:
- Unlike the
HttpResponse.getEntity()
method, this method never returns anull response. Instead,getContent().isPresent() can be used to determine whether the response has a body.
- Specified by:
getEntity
in interfaceHttpResponse
- Returns:
- The response entity. Never
null .
- Unlike the
-
setEntity
Associates a response entity with this response.Notes:
- If an entity has already been set for this response and it depends on an input stream
(
HttpEntity.isStreaming()
returnstrue ), it must be fully consumed in order to ensure release of resources.
- Specified by:
setEntity
in interfaceHttpResponse
- Parameters:
entity
- The entity to associate with this response, ornull to unset.
- If an entity has already been set for this response and it depends on an input stream
(
-
getLocale
Obtains the locale of this response. The locale is used to determine the reason phrase for the status code. It can be changed usingsetLocale(Locale)
.- Specified by:
getLocale
in interfaceHttpResponse
- Returns:
- The locale of this response, never
null .
-
setLocale
Changes the locale of this response.- Specified by:
setLocale
in interfaceHttpResponse
- Parameters:
loc
- The new locale.
-
getProtocolVersion
Returns the protocol version this message is compatible with.- Specified by:
getProtocolVersion
in interfaceHttpMessage
- Returns:
- The protocol version this message is compatible with.
-
containsHeader
Checks if a certain header is present in this message.Header values are ignored.
- Specified by:
containsHeader
in interfaceHttpMessage
- Parameters:
name
- The header name to check for.- Returns:
true if at least one header with this name is present.
-
getHeaders
Returns all the headers with a specified name of this message. Header values are ignored.
Headers are ordered in the sequence they were sent over a connection.- Specified by:
getHeaders
in interfaceHttpMessage
- Parameters:
name
- The name of the headers to return.- Returns:
- All the headers with a specified name of this message.
-
getFirstHeader
Returns the first header with a specified name of this message.If there is more than one matching header in the message the first element of
getHeaders(String)
is returned.This method always returns a value so that you can perform assertions on the result.
- Specified by:
getFirstHeader
in interfaceHttpMessage
- Parameters:
name
- The name of the header to return.- Returns:
- The header, never
null .
-
getLastHeader
Returns the last header with a specified name of this message.If there is more than one matching header in the message the last element of
getHeaders(String)
is returned.This method always returns a value so that you can perform assertions on the result.
- Specified by:
getLastHeader
in interfaceHttpMessage
- Parameters:
name
- The name of the header to return.- Returns:
- The header, never
null .
-
getHeader
Returns the response header with the specified name.If more that one header with the given name exists the values will be combined with
", " as per RFC 2616 Section 4.2.- Parameters:
name
- The name of the header to return.- Returns:
- The header, never
null .
-
getAllHeaders
Returns all the headers of this message. Headers are ordered in the sequence they were sent over a connection.- Specified by:
getAllHeaders
in interfaceHttpMessage
- Returns:
- All the headers of this message.
-
addHeader
Adds a header to this message. The header will be appended to the end of the list.- Specified by:
addHeader
in interfaceHttpMessage
- Parameters:
header
- The header to append.
-
addHeader
Adds a header to this message. The header will be appended to the end of the list.- Specified by:
addHeader
in interfaceHttpMessage
- Parameters:
name
- The name of the header.value
- The value of the header.
-
setHeader
Overwrites the first header with the same name. The new header will be appended to the end of the list, if no header with the given name can be found.- Specified by:
setHeader
in interfaceHttpMessage
- Parameters:
header
- The header to set.
-
setHeader
Overwrites the first header with the same name. The new header will be appended to the end of the list, if no header with the given name can be found.- Specified by:
setHeader
in interfaceHttpMessage
- Parameters:
name
- The name of the header.value
- The value of the header.
-
setHeaders
Overwrites all the headers in the message.- Specified by:
setHeaders
in interfaceHttpMessage
- Parameters:
headers
- The array of headers to set.
-
removeHeader
Removes a header from this message.- Specified by:
removeHeader
in interfaceHttpMessage
- Parameters:
header
- The header to remove.
-
removeHeaders
Removes all headers with a certain name from this message.- Specified by:
removeHeaders
in interfaceHttpMessage
- Parameters:
name
- The name of the headers to remove.
-
headerIterator
Returns an iterator of all the headers.- Specified by:
headerIterator
in interfaceHttpMessage
- Returns:
Iterator
that returnsHeader
objects in the sequence they are sent over a connection.
-
headerIterator
Returns an iterator of the headers with a given name.- Specified by:
headerIterator
in interfaceHttpMessage
- Parameters:
name
- The name of the headers over which to iterate, ornull for all headers.- Returns:
Iterator
that returnsHeader
objects with the argument name in the sequence they are sent over a connection.
-
getParams
Deprecated.Use configuration classes providedorg.apache.http.config andorg.apache.http.client.config .Returns the parameters effective for this message as set bysetParams(HttpParams)
.- Specified by:
getParams
in interfaceHttpMessage
- Returns:
- The parameters effective for this message as set by
setParams(HttpParams)
.
-
setParams
Deprecated.Use configuration classes providedorg.apache.http.config andorg.apache.http.client.config .Provides parameters to be used for the processing of this message.- Specified by:
setParams
in interfaceHttpMessage
- Parameters:
params
- The parameters.
-
getPartParserSession
Creates a session of the specified part parser.- Parameters:
parser
- The parser to create a session for.- Returns:
- A session of the specified parser.
-
getPartParserSession
Creates a session of the client-default parat parser.- Returns:
- A session of the specified parser.
-