public class MockRestClient extends RestClient implements HttpClientConnection
RestClient
.
This class is used for performing serverless unit testing of @Rest
-annotated and @Remote
-annotated classes.
The class itself extends from RestClient
providing it with the rich feature set of that API and combines
it with the Apache HttpClient HttpClientConnection
interface for processing requests.
The class converts HttpRequest
objects to instances of MockServletRequest
and MockServletResponse
which are passed directly
to the call handler on the resource class RestContext.execute(HttpServletRequest,HttpServletResponse)
.
In effect, you're fully testing your REST API as if it were running in a live servlet container, yet not
actually having to run in a servlet container.
All aspects of the client and server side code are tested, yet no servlet container is required. The actual
over-the-wire transmission is the only aspect being bypassed.
The following shows a simple example of invoking a PUT method on a simple REST interface and asserting the correct status code and response body:
Breaking apart the fluent method call above will help you understand how this works.
The
The MockRestRequest
object has convenience methods provided to allow you to set any properties
directly on the underlying HttpServletRequest
object. The following example shows how
this can be used to directly set roles on the request object to perform security testing.
Debug mode is provided that will cause your HTTP requests and responses to be sent to the console:
MockRestClient mr = MockRestClient
.
The class can also be used for testing of @Remote
-annotated interfaces against @Rest
-annotated resources.
Modifier and Type | Field and Description |
---|---|
static String |
MOCKRESTCLIENT_contextPath |
static String |
MOCKRESTCLIENT_mockHttpClientConnectionManager |
static String |
MOCKRESTCLIENT_pathVars |
static String |
MOCKRESTCLIENT_restBean |
static String |
MOCKRESTCLIENT_restBeanCtx |
static String |
MOCKRESTCLIENT_servletPath |
RESTCLIENT_callHandler, RESTCLIENT_console, RESTCLIENT_errorCodes, RESTCLIENT_executorService, RESTCLIENT_executorServiceShutdownOnClose, RESTCLIENT_formData, RESTCLIENT_headers, RESTCLIENT_ignoreErrors, RESTCLIENT_interceptors, RESTCLIENT_interceptors_add, RESTCLIENT_keepHttpClientOpen, RESTCLIENT_leakDetection, RESTCLIENT_logger, RESTCLIENT_logRequests, RESTCLIENT_logRequestsLevel, RESTCLIENT_logRequestsPredicate, RESTCLIENT_logToConsole, RESTCLIENT_parsers, RESTCLIENT_partParser, RESTCLIENT_partSerializer, RESTCLIENT_query, RESTCLIENT_rootUri, RESTCLIENT_serializers
BEAN_annotations, BEAN_beanClassVisibility, BEAN_beanConstructorVisibility, BEAN_beanDictionary, BEAN_beanDictionary_add, BEAN_beanDictionary_remove, BEAN_beanFieldVisibility, BEAN_beanFilters, BEAN_beanFilters_add, BEAN_beanFilters_remove, BEAN_beanMapPutReturnsOldValue, BEAN_beanMethodVisibility, BEAN_beansRequireDefaultConstructor, BEAN_beansRequireSerializable, BEAN_beansRequireSettersForGetters, BEAN_beansRequireSomeProperties, BEAN_bpi, BEAN_bpro, BEAN_bpwo, BEAN_bpx, BEAN_examples, BEAN_excludeProperties, BEAN_fluentSetters, BEAN_ignoreInvocationExceptionsOnGetters, BEAN_ignoreInvocationExceptionsOnSetters, BEAN_ignorePropertiesWithoutSetters, BEAN_ignoreTransientFields, BEAN_ignoreUnknownBeanProperties, BEAN_ignoreUnknownNullBeanProperties, BEAN_implClasses, BEAN_includeProperties, BEAN_notBeanClasses, BEAN_notBeanClasses_add, BEAN_notBeanClasses_remove, BEAN_notBeanPackages, BEAN_notBeanPackages_add, BEAN_notBeanPackages_remove, BEAN_pojoSwaps, BEAN_pojoSwaps_add, BEAN_pojoSwaps_remove, BEAN_propertyNamer, BEAN_sortProperties, BEAN_swaps, BEAN_swaps_add, BEAN_swaps_remove, BEAN_typePropertyName, BEAN_useEnumNames, BEAN_useInterfaceProxies, BEAN_useJavaBeanIntrospector, DEFAULT, DEFAULT_SORTED
CONTEXT_debug, CONTEXT_locale, CONTEXT_mediaType, CONTEXT_timeZone
Constructor and Description |
---|
MockRestClient(PropertyStore ps)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
static MockRestClient |
build(Object impl)
Creates a new
RestClient with no registered serializer or parser. |
static MockRestClient |
buildJson(Object impl)
Creates a new
RestClient with JSON marshalling support. |
static MockRestClient |
buildJsonLax(Object impl)
Creates a new
RestClient with JSON marshalling support. |
static MockRestClient |
buildLax(Object impl)
Creates a new
RestClient with no registered serializer or parser. |
static MockRestClient |
buildSimpleJson(Object impl)
Creates a new
RestClient with Simplified-JSON marshalling support. |
static MockRestClient |
buildSimpleJsonLax(Object impl)
Creates a new
RestClient with Simplified-JSON marshalling support. |
MockRestRequest |
callback(String callString)
Performs a REST call where the entire call is specified in a simple string.
|
void |
close()
Calls
Closeable.close() on the underlying CloseableHttpClient . |
static MockRestClientBuilder |
create(Object impl)
Creates a new
RestClientBuilder configured with the specified REST implementation bean or bean class. |
static MockRestClientBuilder |
createLax(Object impl)
Creates a new
RestClientBuilder configured with the specified REST implementation bean or bean class. |
protected MockRestRequest |
createRequest(URI uri,
String method,
boolean hasBody)
Creates a
RestRequest object from the specified HttpRequest object. |
protected MockRestResponse |
createResponse(RestRequest req,
HttpResponse httpResponse,
Parser parser)
Creates a
RestResponse object from the specified HttpResponse object. |
MockRestRequest |
delete(Object url)
Perform a
|
void |
flush() |
MockRestRequest |
formPost(Object url)
Same as
RestClient.formPost(Object, Object) but doesn't specify the input yet. |
MockRestRequest |
formPost(Object url,
Object body)
Perform a
|
MockRestRequest |
formPostPairs(Object url,
Object... parameters)
Perform a
|
MockRestRequest |
get()
Perform a
|
MockRestRequest |
get(Object url)
Perform a
|
HttpRequest |
getCurrentClientRequest()
Returns the current client-side REST request.
|
MockRestResponse |
getCurrentClientResponse()
Returns the current client-side REST response.
|
MockServletRequest |
getCurrentServerRequest()
Returns the current server-side REST request.
|
MockServletResponse |
getCurrentServerResponse()
Returns the current server-side REST response.
|
HttpConnectionMetrics |
getMetrics() |
int |
getSocketTimeout() |
MockRestRequest |
head(Object url)
Perform a
|
boolean |
isOpen() |
boolean |
isResponseAvailable(int timeout) |
boolean |
isStale() |
MockRestRequest |
options(Object url)
Perform an
|
MockRestRequest |
patch(Object url)
Same as
RestClient.patch(Object, Object) but don't specify the input yet. |
MockRestRequest |
patch(Object url,
Object body)
Perform a
|
MockRestRequest |
patch(Object url,
String body,
String contentType)
Perform a
|
MockRestRequest |
post(Object url)
Same as
RestClient.post(Object, Object) but don't specify the input yet. |
MockRestRequest |
post(Object url,
Object body)
Perform a
|
MockRestRequest |
post(Object url,
String body,
String contentType)
Perform a
|
MockRestRequest |
put(Object url)
Same as
RestClient.put(Object, Object) but don't specify the input yet. |
MockRestRequest |
put(Object url,
Object body)
Perform a
|
MockRestRequest |
put(Object url,
String body,
String contentType)
Perform a
|
void |
receiveResponseEntity(HttpResponse response) |
HttpResponse |
receiveResponseHeader() |
MockRestRequest |
request(String method,
Object url)
Perform a generic REST call.
|
MockRestRequest |
request(String method,
Object url,
boolean hasBody)
Perform a generic REST call.
|
MockRestRequest |
request(String method,
Object url,
Object body)
Perform a generic REST call.
|
void |
sendRequestEntity(HttpEntityEnclosingRequest request) |
void |
sendRequestHeader(HttpRequest request) |
void |
setSocketTimeout(int timeout) |
void |
shutdown() |
builder, closeQuietly, create, execute, execute, execute, execute, execute, execute, execute, execute, finalize, getConnectionManager, getParams, getRemote, getRemote, getRemote, getRrpcInterface, getRrpcInterface, getRrpcInterface, log, log, onClose, onConnect, onInit, run, toMap
_class, createBeanSession, createBeanSession, createDefaultBeanSessionArgs, createDefaultSessionArgs, createSession, createSession, createSession, dumpCacheStats, getAnnotations, getAnnotations, getAnnotations, getAnnotations, getAnnotations, getAnnotations, getAnnotations, getAnnotations, getBeanClassVisibility, getBeanConstructorVisibility, getBeanDictionaryClasses, getBeanFieldVisibility, getBeanFilters, getBeanMeta, getBeanMethodVisibility, getBeanRegistry, getBeanToStringSerializer, getBeanTypePropertyName, getBpi, getBpi, getBpro, getBpro, getBpwo, getBpwo, getBpx, getBpx, getClassMeta, getClassMeta, getClassMetaForObject, getDeclaredAnnotations, getDeclaredAnnotations, getExamples, getImplClassConstructor, getImplClasses, getLastAnnotation, getLastAnnotation, getLastAnnotation, getLastAnnotation, getLastAnnotation, getLastAnnotation, getLastAnnotation, getLastAnnotation, getLastDeclaredAnnotation, getLastDeclaredAnnotation, getNotBeanClasses, getNotBeanPackagesNames, getNotBeanPackagesPrefixes, getPropertyNamer, getSwaps, hasAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, hasDeclaredAnnotation, hasDeclaredAnnotation, hasSameCache, isBean, isBeanMapPutReturnsOldValue, isBeansRequireDefaultConstructor, isBeansRequireSerializable, isBeansRequireSettersForGetters, isBeansRequireSomeProperties, isFluentSetters, isIgnoreInvocationExceptionsOnGetters, isIgnoreInvocationExceptionsOnSetters, isIgnorePropertiesWithoutSetters, isIgnoreTransientFields, isIgnoreUnknownBeanProperties, isIgnoreUnknownNullBeanProperties, isNotABean, isSortProperties, isUseEnumNames, isUseInterfaceProxies, isUseJavaBeanIntrospector, object, resolveClassMeta, string
equals, getArrayProperty, getArrayProperty, getBooleanProperty, getCdlProperty, getClassArrayProperty, getClassArrayProperty, getClassArrayProperty, getClassListProperty, getClassListProperty, getClassMapProperty, getClassMapProperty, getClassProperty, getClassSetProperty, getClassSetProperty, getDefaultLocale, getDefaultMediaType, getDefaultTimeZone, getInstanceArrayProperty, getInstanceArrayProperty, getInstanceArrayProperty, getInstanceProperty, getInstanceProperty, getInstanceProperty, getIntegerProperty, getListProperty, getListProperty, getLongProperty, getMapProperty, getProperty, getProperty, getPropertyKeys, getPropertyStore, getSetProperty, getSetProperty, getStringProperty, getStringPropertyWithNone, hashCode, identityCode, isDebug, toString
public static final String MOCKRESTCLIENT_restBean
public static final String MOCKRESTCLIENT_restBeanCtx
public static final String MOCKRESTCLIENT_servletPath
public static final String MOCKRESTCLIENT_contextPath
public static final String MOCKRESTCLIENT_mockHttpClientConnectionManager
public static final String MOCKRESTCLIENT_pathVars
public MockRestClient(PropertyStore ps)
ps
- The REST bean or bean class annotated with @Rest
.
public static MockRestClientBuilder create(Object impl)
RestClientBuilder
configured with the specified REST implementation bean or bean class.impl
- The REST bean or bean class annotated with @Rest
.
public static MockRestClientBuilder createLax(Object impl)
RestClientBuilder
configured with the specified REST implementation bean or bean class.
Same as create(Object)
but HTTP 400+ codes don't trigger RestCallExceptions
.
impl
- The REST bean or bean class annotated with @Rest
.
public static MockRestClient build(Object impl)
RestClient
with no registered serializer or parser.
Equivalent to calling:
MockRestClient.create(impl).build();
impl
- The REST bean or bean class annotated with @Rest
.
public static MockRestClient buildLax(Object impl)
RestClient
with no registered serializer or parser.
Same as build(Object)
but HTTP 400+ codes don't trigger RestCallExceptions
.
Equivalent to calling:
MockRestClient.create(impl).ignoreErrors().build();
impl
- The REST bean or bean class annotated with @Rest
.
public static MockRestClient buildJson(Object impl)
RestClient
with JSON marshalling support.
Equivalent to calling:
MockRestClient.create(impl).json().build();
impl
- The REST bean or bean class annotated with @Rest
.
public static MockRestClient buildJsonLax(Object impl)
RestClient
with JSON marshalling support.
Same as buildJson(Object)
but HTTP 400+ codes don't trigger RestCallExceptions
.
Equivalent to calling:
MockRestClient.create(impl).json().ignoreErrors().build();
impl
- The REST bean or bean class annotated with @Rest
.
public static MockRestClient buildSimpleJson(Object impl)
RestClient
with Simplified-JSON marshalling support.
Equivalent to calling:
MockRestClient.create(impl).json().build();
impl
- The REST bean or bean class annotated with @Rest
.
public static MockRestClient buildSimpleJsonLax(Object impl)
RestClient
with Simplified-JSON marshalling support.
Same as buildSimpleJson(Object)
but HTTP 400+ codes don't trigger RestCallExceptions
.
Equivalent to calling:
MockRestClient.create(impl).json().ignoreErrors().build();
impl
- The REST bean or bean class annotated with @Rest
.
public MockRestRequest get(Object url) throws RestCallException
RestClient
get
in class RestClient
url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public MockRestRequest get() throws RestCallException
RestClient
get
in class RestClient
RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public MockRestRequest put(Object url, Object body) throws RestCallException
RestClient
put
in class RestClient
url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to body
- The object to serialize and transmit to the URI as the body of the request.
Can be of the following types:
Reader
- Raw contents of Reader
will be serialized to remote resource.
InputStream
- Raw contents of InputStream
will be serialized to remote resource.
Object
- POJO to be converted to text using the Serializer
registered with the
RestClient
.
HttpEntity
/ HttpResource
- Bypass Juneau serialization and pass HttpEntity directly to HttpClient.
NameValuePairSupplier
- Converted to a URL-encoded FORM post.
Supplier
- A supplier of anything on this list.
RestRequest
object that can be further tailored before executing the request
and getting the response as a parsed object.RestCallException
- If any authentication errors occurred.public MockRestRequest put(Object url, String body, String contentType) throws RestCallException
RestClient
put
in class RestClient
url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to body
- The object to serialize and transmit to the URI as the body of the request bypassing the serializer.contentType
- The content type of the request.RestRequest
object that can be further tailored before executing the request
and getting the response as a parsed object.RestCallException
- If any authentication errors occurred.public MockRestRequest put(Object url) throws RestCallException
RestClient
RestClient.put(Object, Object)
but don't specify the input yet.
You must call either RestRequest.body(Object)
or RestRequest.formData(String, Object)
to set the contents on the result object.
put
in class RestClient
url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- REST call failed.public MockRestRequest post(Object url, Object body) throws RestCallException
RestClient
RestClient.formPost(Object, Object)
for post
in class RestClient
url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to body
- The object to serialize and transmit to the URI as the body of the request.
Can be of the following types:
Reader
- Raw contents of Reader
will be serialized to remote resource.
InputStream
- Raw contents of InputStream
will be serialized to remote resource.
Object
- POJO to be converted to text using the Serializer
registered with the
RestClient
.
HttpEntity
/ HttpResource
- Bypass Juneau serialization and pass HttpEntity directly to HttpClient.
NameValuePairSupplier
- Converted to a URL-encoded FORM post.
Supplier
- A supplier of anything on this list.
RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public MockRestRequest post(Object url, String body, String contentType) throws RestCallException
RestClient
post
in class RestClient
url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to body
- The object to serialize and transmit to the URI as the body of the request bypassing the serializer.contentType
- The content type of the request.RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public MockRestRequest post(Object url) throws RestCallException
RestClient
RestClient.post(Object, Object)
but don't specify the input yet.
You must call either RestRequest.body(Object)
or RestRequest.formData(String, Object)
to set the
contents on the result object.
RestClient.formPost(Object, Object)
for post
in class RestClient
url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- REST call failed.public MockRestRequest delete(Object url) throws RestCallException
RestClient
delete
in class RestClient
url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public MockRestRequest options(Object url) throws RestCallException
RestClient
options
in class RestClient
url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public MockRestRequest head(Object url) throws RestCallException
RestClient
head
in class RestClient
url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public MockRestRequest formPost(Object url, Object body) throws RestCallException
RestClient
formPost
in class RestClient
url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to body
- The object to serialize and transmit to the URI as the body of the request.
NameValuePair
- URL-encoded as a single name-value pair.
NameValuePair
array - URL-encoded as name value pairs.
NameValuePairSupplier
- URL-encoded as name value pairs.
Reader
/InputStream
- Streamed directly and HttpResource
/BasicHttpResource
- Raw contents will be serialized to remote resource. Additional headers and media type will be set on request.
HttpEntity
/BasicHttpEntity
- Bypass Juneau serialization and pass HttpEntity directly to HttpClient.
Object
- Converted to a SerializedHttpEntity
using UrlEncodingSerializer
to serialize.
Supplier
- A supplier of anything on this list.
RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public MockRestRequest formPost(Object url) throws RestCallException
RestClient
RestClient.formPost(Object, Object)
but doesn't specify the input yet.formPost
in class RestClient
url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public MockRestRequest formPostPairs(Object url, Object... parameters) throws RestCallException
RestClient
formPostPairs
in class RestClient
url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to parameters
- The parameters of the form post.
BasicNameValuePair
objects.RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public MockRestRequest patch(Object url, Object body) throws RestCallException
RestClient
patch
in class RestClient
url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to body
- The object to serialize and transmit to the URI as the body of the request.
Can be of the following types:
Reader
- Raw contents of Reader
will be serialized to remote resource.
InputStream
- Raw contents of InputStream
will be serialized to remote resource.
HttpResource
/BasicHttpResource
- Raw contents will be serialized to remote resource. Additional headers and media type will be set on request.
HttpEntity
/BasicHttpEntity
- Bypass Juneau serialization and pass HttpEntity directly to HttpClient.
Object
- POJO to be converted to text using the Serializer
registered with the
RestClient
.
NameValuePairSupplier
- Converted to a URL-encoded FORM post.
Supplier
- A supplier of anything on this list.
RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public MockRestRequest patch(Object url, String body, String contentType) throws RestCallException
RestClient
patch
in class RestClient
url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to body
- The object to serialize and transmit to the URI as the body of the request bypassing the serializer.contentType
- The content type of the request.RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public MockRestRequest patch(Object url) throws RestCallException
RestClient
RestClient.patch(Object, Object)
but don't specify the input yet.
You must call RestRequest.body(Object)
to set the contents on the result object.
patch
in class RestClient
url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- REST call failed.public MockRestRequest callback(String callString) throws RestCallException
RestClient
This method is useful for performing callbacks when the target of a callback is passed in on an initial request, for example to signal when a long-running process has completed.
The call string can be any of the following formats:
The payload will always be sent using a simple StringEntity
.
callback
in class RestClient
callString
- The call string.RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- REST call failed.public MockRestRequest request(String method, Object url, Object body) throws RestCallException
RestClient
request
in class RestClient
method
- The HTTP method.url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to body
- The HTTP body content.
Can be of the following types:
Reader
- Raw contents of Reader
will be serialized to remote resource.
InputStream
- Raw contents of InputStream
will be serialized to remote resource.
HttpResource
/BasicHttpResource
- Raw contents will be serialized to remote resource. Additional headers and media type will be set on request.
HttpEntity
/BasicHttpEntity
- Bypass Juneau serialization and pass HttpEntity directly to HttpClient.
Object
- POJO to be converted to text using the Serializer
registered with the
RestClient
.
NameValuePairSupplier
- Converted to a URL-encoded FORM post.
Supplier
- A supplier of anything on this list.
RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public MockRestRequest request(String method, Object url) throws RestCallException
RestClient
request
in class RestClient
method
- The HTTP method.url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public MockRestRequest request(String method, Object url, boolean hasBody) throws RestCallException
RestClient
Typically you're going to use RestClient.request(String, Object)
or RestClient.request(String, Object, Object)
,
but this method is provided to allow you to perform non-standard HTTP methods (e.g. HTTP FOO).
request
in class RestClient
method
- The method name (e.g. url
- The URI of the remote REST resource.
URIBuilder
URI
URL
String
Object
- Converted to hasBody
- Boolean flag indicating if the specified request has content associated with it.RestRequest
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public HttpRequest getCurrentClientRequest()
Note that this uses a ThreadLocal
object for storage and so will not work on requests executed in
separate threads such as when using Futures
.
public MockRestResponse getCurrentClientResponse()
Note that this uses a ThreadLocal
object for storage and so will not work on requests executed in
separate threads such as when using Futures
.
public MockServletRequest getCurrentServerRequest()
Note that this uses a ThreadLocal
object for storage and so will not work on requests executed in
separate threads such as when using Futures
.
public MockServletResponse getCurrentServerResponse()
Note that this uses a ThreadLocal
object for storage and so will not work on requests executed in
separate threads such as when using Futures
.
protected MockRestRequest createRequest(URI uri, String method, boolean hasBody) throws RestCallException
RestClient
RestRequest
object from the specified HttpRequest
object.
Subclasses can override this method to provide their own specialized RestRequest
objects.
createRequest
in class RestClient
uri
- The target.method
- The HTTP method (uppercase).hasBody
- Whether this method has a request entity.RestRequest
object.RestCallException
- If an exception or non-200 response code occurred during the connection attempt.protected MockRestResponse createResponse(RestRequest req, HttpResponse httpResponse, Parser parser) throws RestCallException
RestClient
RestResponse
object from the specified HttpResponse
object.
Subclasses can override this method to provide their own specialized RestResponse
objects.
createResponse
in class RestClient
req
- The request creating this response.httpResponse
- The response object to wrap.parser
- The parser to use to parse the response.RestResponse
object.RestCallException
- If an exception or non-200 response code occurred during the connection attempt.public void close() throws IOException
RestClient
Closeable.close()
on the underlying CloseableHttpClient
.
It's good practice to call this method after the client is no longer used.
close
in interface Closeable
close
in interface AutoCloseable
close
in interface HttpConnection
close
in class RestClient
IOException
- Thrown by underlying stream.public boolean isOpen()
isOpen
in interface HttpConnection
public boolean isStale()
isStale
in interface HttpConnection
public void setSocketTimeout(int timeout)
setSocketTimeout
in interface HttpConnection
public int getSocketTimeout()
getSocketTimeout
in interface HttpConnection
public void shutdown() throws IOException
shutdown
in interface HttpConnection
IOException
public HttpConnectionMetrics getMetrics()
getMetrics
in interface HttpConnection
public boolean isResponseAvailable(int timeout) throws IOException
isResponseAvailable
in interface HttpClientConnection
IOException
public void sendRequestHeader(HttpRequest request) throws HttpException, IOException
sendRequestHeader
in interface HttpClientConnection
HttpException
IOException
public void sendRequestEntity(HttpEntityEnclosingRequest request) throws HttpException, IOException
sendRequestEntity
in interface HttpClientConnection
HttpException
IOException
public HttpResponse receiveResponseHeader() throws HttpException, IOException
receiveResponseHeader
in interface HttpClientConnection
HttpException
IOException
public void receiveResponseEntity(HttpResponse response) throws HttpException, IOException
receiveResponseEntity
in interface HttpClientConnection
HttpException
IOException
public void flush() throws IOException
flush
in interface HttpClientConnection
IOException
Copyright © 2016–2020 The Apache Software Foundation. All rights reserved.