Package org.mockserver.client
Class MockServerClient
- java.lang.Object
-
- org.mockserver.client.MockServerClient
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Stoppable
- Direct Known Subclasses:
ClientAndServer
public class MockServerClient extends Object implements Stoppable
- Author:
- jamesdbloom
-
-
Field Summary
Fields Modifier and Type Field Description protected CompletableFuture<Integer>
portFuture
-
Constructor Summary
Constructors Constructor Description MockServerClient(String host, int port)
Start the client communicating to a MockServer at the specified host and port for example: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(CompletableFuture<Integer> portFuture)
Start the client communicating to a MockServer on localhost at the port specified with the Future
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description List<Integer>
bind(Integer... ports)
Bind new ports to listen onMockServerClient
clear(HttpRequest httpRequest)
Clear all expectations and logs that match the httpMockServerClient
clear(HttpRequest httpRequest, ClearType type)
Clear expectations, logs or both that match the httpvoid
close()
String
contextPath()
boolean
hasStarted()
Returns whether MockServer has started, if called after MockServer has been stopped this method will block for 5 seconds while confirming that MockServer is not startingboolean
hasStarted(int attempts, long timeout, TimeUnit timeUnit)
Returns whether server MockServer has started, by polling the MockServer a configurable amount of timesboolean
hasStopped()
Returns whether MockServer has stopped, if called too quickly after starting MockServer this may return false because MockServer has not yet started, to ensure MockServer has started use hasStarted()boolean
hasStopped(int attempts, long timeout, TimeUnit timeUnit)
Returns whether server MockServer has stopped, by polling the MockServer a configurable amount of times.boolean
isRunning()
Deprecated.use hasStopped() or hasStarted() insteadboolean
isRunning(int attempts, long timeout, TimeUnit timeUnit)
Deprecated.use hasStopped() or hasStarted() insteadboolean
isSecure()
InetSocketAddress
remoteAddress()
MockServerClient
reset()
Reset MockServer by clearing all expectationsExpectation[]
retrieveActiveExpectations(HttpRequest httpRequest)
Retrieve the active expectations match the httpRequest parameter, use null for the parameter to retrieve all expectationsString
retrieveActiveExpectations(HttpRequest httpRequest, Format format)
Retrieve the active expectations match the httpRequest parameter, use null for the parameter to retrieve all expectationsString
retrieveLogMessages(HttpRequest httpRequest)
Retrieve the logs associated to a specific requests, this shows all logs for expectation matching, verification, clearing, etcString[]
retrieveLogMessagesArray(HttpRequest httpRequest)
Retrieve the logs associated to a specific requests, this shows all logs for expectation matching, verification, clearing, etcExpectation[]
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 requestsString
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 requestsHttpRequest[]
retrieveRecordedRequests(HttpRequest httpRequest)
Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requestsString
retrieveRecordedRequests(HttpRequest httpRequest, Format format)
Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requestsLogEventRequestAndResponse[]
retrieveRecordedRequestsAndResponses(HttpRequest httpRequest)
Retrieve the recorded requests and responses that match the httpRequest parameter, use null for the parameter to retrieve all requests and responsesString
retrieveRecordedRequestsAndResponses(HttpRequest httpRequest, Format format)
Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requestsvoid
sendExpectation(Expectation... expectations)
Specify one or more expectations, normally this method should not be used directly instead the when(...) and response(...) or forward(...) or error(...) methods should be used for example:MockServerClient
setRequestOverride(HttpRequest requestOverride)
void
stop()
Stop MockServer gracefully (only support for Netty version, not supported for WAR version)Future<MockServerClient>
stop(boolean ignoreFailure)
Stop MockServer gracefully (only support for Netty version, not supported for WAR version)Future<MockServerClient>
stopAsync()
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:MockServerClient
withSecure(boolean secure)
-
-
-
Field Detail
-
portFuture
protected CompletableFuture<Integer> portFuture
-
-
Constructor Detail
-
MockServerClient
public MockServerClient(CompletableFuture<Integer> portFuture)
Start the client communicating to a MockServer on localhost at the port specified with the Future- Parameters:
portFuture
- the port for the MockServer to communicate with
-
MockServerClient
public 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);
- Parameters:
host
- the host for the MockServer to communicate withport
- the port for the MockServer to communicate with
-
MockServerClient
public 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");
- Parameters:
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 to
-
-
Method Detail
-
setRequestOverride
public MockServerClient setRequestOverride(HttpRequest requestOverride)
-
isSecure
public boolean isSecure()
-
withSecure
public MockServerClient withSecure(boolean secure)
-
remoteAddress
public InetSocketAddress remoteAddress()
-
contextPath
public String contextPath()
-
isRunning
@Deprecated public boolean isRunning()
Deprecated.use hasStopped() or hasStarted() insteadReturns whether MockServer is running, if called too quickly after starting MockServer this may return false because MockServer has not yet started, to ensure MockServer has started use hasStarted()
-
isRunning
@Deprecated public boolean isRunning(int attempts, long timeout, TimeUnit timeUnit)
Deprecated.use hasStopped() or hasStarted() insteadReturns whether server MockServer is running, by polling the MockServer a configurable amount of times. If called too quickly after starting MockServer this may return false because MockServer has not yet started, to ensure MockServer has started use hasStarted()
-
hasStopped
public boolean hasStopped()
Returns whether MockServer has stopped, if called too quickly after starting MockServer this may return false because MockServer has not yet started, to ensure MockServer has started use hasStarted()
-
hasStopped
public boolean hasStopped(int attempts, long timeout, TimeUnit timeUnit)
Returns whether server MockServer has stopped, by polling the MockServer a configurable amount of times. If called too quickly after starting MockServer this may return false because MockServer has not yet started, to ensure MockServer has started use hasStarted()
-
hasStarted
public boolean hasStarted()
Returns whether MockServer has started, if called after MockServer has been stopped this method will block for 5 seconds while confirming that MockServer is not starting
-
hasStarted
public boolean hasStarted(int attempts, long timeout, TimeUnit timeUnit)
Returns whether server MockServer has started, by polling the MockServer a configurable amount of times
-
stopAsync
public Future<MockServerClient> stopAsync()
Stop MockServer gracefully (only support for Netty version, not supported for WAR version)
-
stop
public void stop()
Stop MockServer gracefully (only support for Netty version, not supported for WAR version)
-
stop
public Future<MockServerClient> stop(boolean ignoreFailure)
Stop MockServer gracefully (only support for Netty version, not supported for WAR version)
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
reset
public MockServerClient reset()
Reset MockServer by clearing all expectations
-
clear
public MockServerClient clear(HttpRequest httpRequest)
Clear all expectations and logs that match the http- Parameters:
httpRequest
- the http request that is matched against when deciding whether to clear each expectation if null all expectations are cleared
-
clear
public MockServerClient clear(HttpRequest httpRequest, ClearType type)
Clear expectations, logs or both that match the http- Parameters:
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 BOTH
-
verify
public MockServerClient verify(HttpRequest... httpRequests) throws AssertionError
Verify a list of requests have been sent in the order specified for example:mockServerClient .verify( request() .withPath("/first_request") .withBody("some_request_body"), request() .withPath("/second_request") .withBody("some_request_body") );
- Parameters:
httpRequests
- the http requests that must be matched for this verification to pass- Throws:
AssertionError
- if the request has not been found
-
verify
public MockServerClient verify(HttpRequest httpRequest, VerificationTimes times) throws AssertionError
Verify a request has been sent for example: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
- Parameters:
httpRequest
- the http request that must be matched for this verification to passtimes
- the number of times this request must be matched- Throws:
AssertionError
- if the request has not been found
-
verifyZeroInteractions
public MockServerClient verifyZeroInteractions() throws AssertionError
Verify no requests have been have been sent.- Throws:
AssertionError
- if any request has been found
-
retrieveRecordedRequests
public HttpRequest[] retrieveRecordedRequests(HttpRequest httpRequest)
Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requests- Parameters:
httpRequest
- the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requests- Returns:
- an array of all requests that have been recorded by the MockServer in the order they have been received and including duplicates where the same request has been received multiple times
-
retrieveRecordedRequests
public String retrieveRecordedRequests(HttpRequest httpRequest, Format format)
Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requests- Parameters:
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 JSON- Returns:
- an array of all requests that have been recorded by the MockServer in the order they have been received and including duplicates where the same request has been received multiple times
-
retrieveRecordedRequestsAndResponses
public LogEventRequestAndResponse[] retrieveRecordedRequestsAndResponses(HttpRequest httpRequest)
Retrieve the recorded requests and responses that match the httpRequest parameter, use null for the parameter to retrieve all requests and responses- Parameters:
httpRequest
- the http request that is matched against when deciding whether to return each request (and its corresponding response), use null for the parameter to retrieve for all requests- Returns:
- an array of all requests and responses that have been recorded by the MockServer in the order they have been received and including duplicates where the same request has been received multiple times
-
retrieveRecordedRequestsAndResponses
public String retrieveRecordedRequestsAndResponses(HttpRequest httpRequest, Format format)
Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter to retrieve all requests- Parameters:
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 JSON- Returns:
- an array of all requests that have been recorded by the MockServer in the order they have been received and including duplicates where the same request has been received multiple times
-
retrieveRecordedExpectations
public 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- Parameters:
httpRequest
- the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requests- Returns:
- an array of all expectations that have been recorded by the MockServer in the order they have been received and including duplicates where the same request has been received multiple times
-
retrieveRecordedExpectations
public 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- Parameters:
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 JSON- Returns:
- an array of all expectations that have been recorded by the MockServer in the order they have been received and including duplicates where the same request has been received multiple times
-
retrieveLogMessages
public String retrieveLogMessages(HttpRequest httpRequest)
Retrieve the logs associated to a specific requests, this shows all logs for expectation matching, verification, clearing, etc- Parameters:
httpRequest
- the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requests- Returns:
- all log messages recorded by the MockServer when creating expectations, matching expectations, performing verification, clearing logs, etc
-
retrieveLogMessagesArray
public String[] retrieveLogMessagesArray(HttpRequest httpRequest)
Retrieve the logs associated to a specific requests, this shows all logs for expectation matching, verification, clearing, etc- Parameters:
httpRequest
- the http request that is matched against when deciding whether to return each request, use null for the parameter to retrieve for all requests- Returns:
- an array of all log messages recorded by the MockServer when creating expectations, matching expectations, performing verification, clearing logs, etc
-
when
public ForwardChainExpectation when(HttpRequest httpRequest)
Specify an unlimited expectation that will respond regardless of the number of matching http for example:mockServerClient .when( request() .withPath("/some_path") .withBody("some_request_body") ) .respond( response() .withBody("some_response_body") .withHeader("responseName", "responseValue") )
- Parameters:
httpRequest
- the http request that must be matched for this expectation to respond- Returns:
- an Expectation object that can be used to specify the response
-
when
public 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:mockServerClient .when( request() .withPath("/some_path") .withBody("some_request_body"), Times.exactly(5) ) .respond( response() .withBody("some_response_body") .withHeader("responseName", "responseValue") )
- Parameters:
httpRequest
- the http request that must be matched for this expectation to respondtimes
- the number of times to respond when this http is matched- Returns:
- an Expectation object that can be used to specify the response
-
when
public 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: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") )
- Parameters:
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 active- Returns:
- an Expectation object that can be used to specify the response
-
sendExpectation
public void sendExpectation(Expectation... expectations)
Specify one or more expectations, normally this method should not be used directly instead the when(...) and response(...) or forward(...) or error(...) methods should be used for example: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") )
- Parameters:
expectations
- one or more expectations
-
retrieveActiveExpectations
public Expectation[] retrieveActiveExpectations(HttpRequest httpRequest)
Retrieve the active expectations match the httpRequest parameter, use null for the parameter to retrieve all expectations- Parameters:
httpRequest
- the http request that is matched against when deciding whether to return each expectation, use null for the parameter to retrieve for all requests- Returns:
- an array of all expectations that have been setup and have not expired
-
retrieveActiveExpectations
public String retrieveActiveExpectations(HttpRequest httpRequest, Format format)
Retrieve the active expectations match the httpRequest parameter, use null for the parameter to retrieve all expectations- Parameters:
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 JSON- Returns:
- an array of all expectations that have been setup and have not expired
-
-