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.
A request matcher can contain any of the following matchers:
Actions can be one of the following types:
A response action can be:
either a response literal containing any of the following:
or a templated response using javascript or velocity with a delay
or a callback used to dynamically generate a response based on the request:
as a server side callback implemented as a java class that has a default constructor, implements org.mockserver.mock.action.ExpectationResponseCallback and is available on the classpath
as a client side callback implemented as a closure using the java or javascript clients
A forward action can be:
either an exact forwarder, that forwards requests exactly as it receives them, containing the following:
or an overridden request, with a delay, that allows any element of requests to be overridden before they are forwarded
or a templated forwarder using javascript or velocity, with a delay, that allows requests to be modified or completely re-written before they are forwarded
or a callback used to dynamically generate the request to forward based on the request received by MockServer:
as a server side callback implemented as a java class that has a default constructor, implements org.mockserver.mock.action.ExpectationForwardCallback and is available on the classpath
as a client side callback implemented as a closure using the java or javascript clients
An error action can return an invalid response as a sequence of bytes or drop the connection