org.mockserver.client.server
Class MockServerClient

java.lang.Object
  extended by org.mockserver.client.AbstractClient
      extended by org.mockserver.client.server.MockServerClient

public class MockServerClient
extends AbstractClient

Author:
jamesdbloom

Field Summary
 
Fields inherited from class org.mockserver.client.AbstractClient
contextPath, expectationSerializer, host, httpRequestSerializer, logger, nettyHttpClient, port, verificationSequenceSerializer, verificationSerializer
 
Constructor Summary
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", 8080);
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", 8080, "/mockserver");
 
Method Summary
 MockServerClient clear(HttpRequest httpRequest)
          Clear all expectations that match the http
 MockServerClient dumpToLog()
          Pretty-print the json for all expectations to the log.
 MockServerClient dumpToLog(HttpRequest httpRequest)
          Pretty-print the json for all expectations that match the request to the log.
 MockServerClient reset()
          Reset MockServer by clearing all expectations
 Expectation[] retrieveAsExpectations(HttpRequest httpRequest)
          Retrieve the recorded requests that match the httpRequest parameter as expectations, use null for the parameter to retrieve all requests
 String retrieveAsJSON(HttpRequest httpRequest)
          Retrieve the recorded requests that match the httpRequest parameter as a JSON array, use null for the parameter to retrieve all requests
protected  void sendExpectation(Expectation expectation)
           
 MockServerClient stop()
          Stop MockServer gracefully (only support for Netty and Vert.X versions, 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( request() .withPath("/first_request") .withBody("some_request_body"), request() .withPath("/second_request") .withBody("some_request_body") );
 MockServerClient verify(HttpRequest httpRequest, VerificationTimes times)
          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
 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") .withHeaders( new Header("responseName", "responseValue") ) );
 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( new HttpRequest() .withPath("/some_path") .withBody("some_request_body"), VerificationTimes.exactly(5) ) .respond( new HttpResponse() .withBody("some_response_body") .withHeaders( new Header("responseName", "responseValue") ) );
 
Methods inherited from class org.mockserver.client.AbstractClient
calculatePath, sendRequest
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

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", 8080);

Parameters:
host - the host for the MockServer to communicate with
port - 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", 8080, "/mockserver");

Parameters:
host - the host for the MockServer to communicate with
port - the port for the MockServer to communicate with
contextPath - the context path that the MockServer war is deployed to
Method Detail

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") .withHeaders( new Header("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( new HttpRequest() .withPath("/some_path") .withBody("some_request_body"), VerificationTimes.exactly(5) ) .respond( new HttpResponse() .withBody("some_response_body") .withHeaders( new Header("responseName", "responseValue") ) );

Parameters:
httpRequest - the http request that must be matched for this expectation to respond
times - the number of times to respond when this http is matched
Returns:
an Expectation object that can be used to specify the response

dumpToLog

public MockServerClient dumpToLog()
Pretty-print the json for all expectations to the log. They are printed into a dedicated log called mockserver_request.log


dumpToLog

public MockServerClient dumpToLog(HttpRequest httpRequest)
Pretty-print the json for all expectations that match the request to the log. They are printed into a dedicated log called mockserver_request.log

Parameters:
httpRequest - the http request that is matched against when deciding what to log if null all requests are logged

reset

public MockServerClient reset()
Reset MockServer by clearing all expectations


stop

public MockServerClient stop()
Stop MockServer gracefully (only support for Netty and Vert.X versions, not supported for WAR version)


clear

public MockServerClient clear(HttpRequest httpRequest)
Clear all expectations 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

sendExpectation

protected void sendExpectation(Expectation expectation)

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 pass
times - the number of times this request must be matched
Throws:
AssertionError - if the request has not been found

retrieveAsExpectations

public Expectation[] retrieveAsExpectations(HttpRequest httpRequest)
Retrieve the recorded requests that match the httpRequest parameter as expectations, use null for the parameter to retrieve all requests

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 recorded by the proxy

retrieveAsJSON

public String retrieveAsJSON(HttpRequest httpRequest)
Retrieve the recorded requests that match the httpRequest parameter as a JSON array, use null for the parameter to retrieve all requests

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:
a JSON array of all expectations that have been recorded by the proxy


Copyright © 2015. All rights reserved.