Please Note: There are over 100 more detailed code examples in Java, JavaScript and the REST API below.

new MockServerClient("localhost", 1080)
    .when(
        request()
            .withMethod("POST")
            .withPath("/login")
            .withBody("{username: 'foo', password: 'bar'}")
    )
    .respond(
        response()
            .withStatusCode(302)
            .withCookie(
                "sessionId", "2By8LOhBmaW5nZXJwcmludCIlMDAzMW"
            )
            .withHeader(
                "Location", "https://www.mock-server.com"
            )
    );

Please Note: There are over 100 more detailed code examples in Java, JavaScript and the REST API below.

var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
    "httpRequest": {
        "method": "POST",
        "path": "/login",
        "body": "{username: 'foo', password: 'bar'}"
    },
    "httpResponse": {
        "statusCode": 302,
        "headers": {
            "Location" : [ "https://www.mock-server.com" ]
        },
        "cookies": {
            "sessionId" : "2By8LOhBmaW5nZXJwcmludCIlMDAzMW"
        }
    }
});

An expectation contains:

MockServer will play expectations in the exact order they are added. For example, if an expectation A is added with Times.exactly(3) then expectation B is added with Times.exactly(2) with the same request matcher they will be applied in the following order A, A, A, B, B.

 

Request Matchers

A request matcher can contain any of the following matchers:

{% include_subpage _includes/request_matcher_code_examples.html %}
 

Actions

Actions can be one of the following types:

 

A response action can be:

{% include_subpage _includes/response_action_code_examples.html %}
 

A forward action can be:

{% include_subpage _includes/forward_action_code_examples.html %}
 

An error action can return an invalid response as a sequence of bytes or drop the connection

{% include_subpage _includes/error_action_code_examples.html %}