public abstract class AbstractClient<T extends AbstractClient> extends Object implements Closeable
Modifier and Type | Field and Description |
---|---|
protected String |
contextPath |
protected ExpectationSerializer |
expectationSerializer |
protected String |
host |
protected HttpRequestSerializer |
httpRequestSerializer |
protected org.slf4j.Logger |
logger |
protected NettyHttpClient |
nettyHttpClient |
protected int |
port |
protected PortBindingSerializer |
portBindingSerializer |
protected VerificationSequenceSerializer |
verificationSequenceSerializer |
protected VerificationSerializer |
verificationSerializer |
Modifier | Constructor and Description |
---|---|
protected |
AbstractClient(String host,
int port,
String contextPath,
Class<T> clientClass)
Start the client communicating to the proxy at the specified host and port
and contextPath for example:
|
Modifier and Type | Method and Description |
---|---|
List<Integer> |
bind(Integer... ports)
Bind new ports to listen on
|
protected String |
calculatePath(String path) |
T |
clear(HttpRequest httpRequest)
Clear all expectations and logs that match the http
|
T |
clear(HttpRequest httpRequest,
HttpStateHandler.ClearType type)
Clear expectations, logs or both that match the http
|
void |
close() |
String |
contextPath() |
protected String |
formatErrorMessage(String message,
Object... objects) |
boolean |
isRunning()
Returns the server (MockServer or Proxy) is running
|
boolean |
isRunning(int attempts,
long timeout,
TimeUnit timeUnit)
Returns the server (MockServer or Proxy) is running, by polling the MockServer a configurable amount of times
|
InetSocketAddress |
remoteAddress() |
T |
reset()
Reset server (MockServer or Proxy) by clearing all expectations
|
Expectation[] |
retrieveRecordedExpectations(HttpRequest httpRequest)
Retrieve the recorded requests that match the httpRequest parameter, use null for the parameter 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
|
protected HttpResponse |
sendRequest(HttpRequest request) |
T |
stop()
Stop server (MockServer or Proxy) gracefully (only support for Netty version, not supported for WAR version)
|
T |
stop(boolean ignoreFailure)
Stop server (MockServer or Proxy) gracefully (only support for Netty version, not supported for WAR version)
|
T |
verify(HttpRequest... httpRequests)
Verify a list of requests have been sent in the order specified for example:
|
T |
verify(HttpRequest httpRequest,
VerificationTimes times)
Verify a request has been sent for example:
|
T |
verifyZeroInteractions()
Verify no requests have been have been sent.
|
protected final org.slf4j.Logger logger
protected final String host
protected final int port
protected final String contextPath
protected NettyHttpClient nettyHttpClient
protected HttpRequestSerializer httpRequestSerializer
protected PortBindingSerializer portBindingSerializer
protected ExpectationSerializer expectationSerializer
protected VerificationSerializer verificationSerializer
protected VerificationSequenceSerializer verificationSequenceSerializer
protected AbstractClient(String host, int port, String contextPath, Class<T> clientClass)
ProxyClient mockServerClient = new ProxyClient("localhost", 1080, "/proxy");
host
- the host for the proxy to communicate withport
- the port for the proxy to communicate withcontextPath
- the context path that the proxy war is deployed toprotected HttpResponse sendRequest(HttpRequest request)
public InetSocketAddress remoteAddress()
public String contextPath()
public boolean isRunning()
public boolean isRunning(int attempts, long timeout, TimeUnit timeUnit)
public T stop()
public T stop(boolean ignoreFailure)
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
public T reset()
public T clear(HttpRequest httpRequest)
httpRequest
- the http request that is matched against when deciding whether to clear each expectation if null all expectations are clearedpublic T clear(HttpRequest httpRequest, HttpStateHandler.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 T 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 T 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 T 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 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 requestsCopyright © 2018. All rights reserved.