public class MockServerClient extends Object implements Closeable
Modifier and Type | Field and Description |
---|---|
protected MockServerLogger |
mockServerLogger |
protected Future<Integer> |
portFuture |
Constructor and Description |
---|
MockServerClient(Future<Integer> portFuture)
Start the client communicating to a MockServer on localhost at the port
specified with the Future
|
MockServerClient(String host,
int port)
Start the client communicating to a MockServer at the specified host and port
for example:
MockServerClient mockServerClient = new MockServerClient("localhost", 1080);
|
MockServerClient(String host,
int port,
String contextPath)
Start the client communicating to a MockServer at the specified host and port
and contextPath for example:
MockServerClient mockServerClient = new MockServerClient("localhost", 1080, "/mockserver");
|
Modifier and Type | Method and Description |
---|---|
List<Integer> |
bind(Integer... ports)
Bind new ports to listen on
|
MockServerClient |
clear(HttpRequest httpRequest)
Clear all expectations and logs that match the http
|
MockServerClient |
clear(HttpRequest httpRequest,
ClearType type)
Clear expectations, logs or both that match the http
|
void |
close() |
String |
contextPath() |
boolean |
isRunning()
Returns whether MockServer is running
|
boolean |
isRunning(int attempts,
long timeout,
TimeUnit timeUnit)
Returns whether server MockServer is running, by polling the MockServer a configurable amount of times
|
InetSocketAddress |
remoteAddress() |
MockServerClient |
reset()
Reset MockServer by clearing all expectations
|
Expectation[] |
retrieveActiveExpectations(HttpRequest httpRequest)
Retrieve the active expectations match the httpRequest parameter, use null for the parameter to retrieve all expectations
|
String |
retrieveActiveExpectations(HttpRequest httpRequest,
Format format)
Retrieve the active expectations match the httpRequest parameter, use null for the parameter to retrieve all expectations
|
String |
retrieveLogMessages(HttpRequest httpRequest)
Retrieve the logs associated to a specific requests, this shows all logs for expectation matching, verification, clearing, etc
|
String[] |
retrieveLogMessagesArray(HttpRequest httpRequest)
Retrieve the logs associated to a specific requests, this shows all logs for expectation matching, verification, clearing, etc
|
Expectation[] |
retrieveRecordedExpectations(HttpRequest httpRequest)
Retrieve the request-response combinations that have been recorded as a list of expectations, only those that match the httpRequest parameter are returned, use null to retrieve all requests
|
String |
retrieveRecordedExpectations(HttpRequest httpRequest,
Format format)
Retrieve the request-response combinations that have been recorded as a list of expectations, only those that match the httpRequest parameter are returned, use null to retrieve all requests
|
HttpRequest[] |
retrieveRecordedRequests(HttpRequest httpRequest)
Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requests
|
String |
retrieveRecordedRequests(HttpRequest httpRequest,
Format format)
Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requests
|
MockServerClient |
stop()
Stop MockServer gracefully (only support for Netty version, not supported for WAR version)
|
MockServerClient |
stop(boolean ignoreFailure)
Stop MockServer gracefully (only support for Netty version, not supported for WAR version)
|
MockServerClient |
verify(HttpRequest... httpRequests)
Verify a list of requests have been sent in the order specified for example:
|
MockServerClient |
verify(HttpRequest httpRequest,
VerificationTimes times)
Verify a request has been sent for example:
|
MockServerClient |
verifyZeroInteractions()
Verify no requests have been have been sent.
|
ForwardChainExpectation |
when(HttpRequest httpRequest)
Specify an unlimited expectation that will respond regardless of the number of matching http
for example:
|
ForwardChainExpectation |
when(HttpRequest httpRequest,
Times times)
Specify an limited expectation that will respond a specified number of times when the http is matched
for example:
|
ForwardChainExpectation |
when(HttpRequest httpRequest,
Times times,
TimeToLive timeToLive)
Specify an limited expectation that will respond a specified number of times when the http is matched
for example:
|
protected final MockServerLogger mockServerLogger
public MockServerClient(Future<Integer> portFuture)
portFuture
- the port for the MockServer to communicate withpublic MockServerClient(String host, int port)
host
- the host for the MockServer to communicate withport
- the port for the MockServer to communicate withpublic MockServerClient(String host, int port, String contextPath)
host
- the host for the MockServer to communicate withport
- the port for the MockServer to communicate withcontextPath
- the context path that the MockServer war is deployed topublic InetSocketAddress remoteAddress()
public String contextPath()
public boolean isRunning()
public boolean isRunning(int attempts, long timeout, TimeUnit timeUnit)
public MockServerClient stop()
public MockServerClient stop(boolean ignoreFailure)
public void close()
close
in interface Closeable
close
in interface AutoCloseable
public MockServerClient reset()
public MockServerClient clear(HttpRequest httpRequest)
httpRequest
- the http request that is matched against when deciding whether to clear each expectation if null all expectations are clearedpublic MockServerClient clear(HttpRequest httpRequest, ClearType type)
httpRequest
- the http request that is matched against when deciding whether to clear each expectation if null all expectations are clearedtype
- the type to clear, EXPECTATION, LOG or BOTHpublic MockServerClient verify(HttpRequest... httpRequests) throws AssertionError
mockServerClient .verify( request() .withPath("/first_request") .withBody("some_request_body"), request() .withPath("/second_request") .withBody("some_request_body") );
httpRequests
- the http requests that must be matched for this verification to passAssertionError
- if the request has not been foundpublic MockServerClient verify(HttpRequest httpRequest, VerificationTimes times) throws AssertionError
mockServerClient .verify( request() .withPath("/some_path") .withBody("some_request_body"), VerificationTimes.exactly(3) );VerificationTimes supports multiple static factory methods: once() - verify the request was only received once exactly(n) - verify the request was only received exactly n times atLeast(n) - verify the request was only received at least n times
httpRequest
- the http request that must be matched for this verification to passtimes
- the number of times this request must be matchedAssertionError
- if the request has not been foundpublic MockServerClient verifyZeroInteractions() throws AssertionError
AssertionError
- if any request has been foundpublic HttpRequest[] retrieveRecordedRequests(HttpRequest httpRequest)
httpRequest
- the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requestspublic String retrieveRecordedRequests(HttpRequest httpRequest, Format format)
httpRequest
- the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requestsformat
- the format to retrieve the expectations, either JAVA or JSONpublic Expectation[] retrieveRecordedExpectations(HttpRequest httpRequest)
httpRequest
- the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requestspublic String retrieveRecordedExpectations(HttpRequest httpRequest, Format format)
httpRequest
- the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requestsformat
- the format to retrieve the expectations, either JAVA or JSONpublic String retrieveLogMessages(HttpRequest httpRequest)
httpRequest
- the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requestspublic String[] retrieveLogMessagesArray(HttpRequest httpRequest)
httpRequest
- the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requestspublic ForwardChainExpectation when(HttpRequest httpRequest)
mockServerClient .when( request() .withPath("/some_path") .withBody("some_request_body") ) .respond( response() .withBody("some_response_body") .withHeader("responseName", "responseValue") )
httpRequest
- the http request that must be matched for this expectation to respondpublic ForwardChainExpectation when(HttpRequest httpRequest, Times times)
mockServerClient .when( request() .withPath("/some_path") .withBody("some_request_body"), Times.exactly(5) ) .respond( response() .withBody("some_response_body") .withHeader("responseName", "responseValue") )
httpRequest
- the http request that must be matched for this expectation to respondtimes
- the number of times to respond when this http is matchedpublic ForwardChainExpectation when(HttpRequest httpRequest, Times times, TimeToLive timeToLive)
mockServerClient .when( request() .withPath("/some_path") .withBody("some_request_body"), Times.exactly(5), TimeToLive.exactly(TimeUnit.SECONDS, 120) ) .respond( response() .withBody("some_response_body") .withHeader("responseName", "responseValue") )
httpRequest
- the http request that must be matched for this expectation to respondtimes
- the number of times to respond when this http is matchedtimeToLive
- the length of time from when the server receives the expectation that the expectation should be activepublic Expectation[] retrieveActiveExpectations(HttpRequest httpRequest)
httpRequest
- the http request that is matched against when deciding whether to return each expectation, use null for the parameter to retrieve for all requestspublic String retrieveActiveExpectations(HttpRequest httpRequest, Format format)
httpRequest
- the http request that is matched against when deciding whether to return each expectation, use null for the parameter to retrieve for all requestsformat
- the format to retrieve the expectations, either JAVA or JSONCopyright © 2018. All rights reserved.