Mock Server Clients
MockServer can be controlled in the following ways:
- REST API
- Java
- JavaScript (both browser API & Node.js module)
The following activities are supported:
- creating expectations
- verifying which requests have been received
- clearing or resetting recorded request, expectations or logs (selectively)
-
Retrieving the following items:
- bind additional ports for MockServer to listen on
MockServer REST API
The REST API is documented using Open API specification
MockServer Java Client
The Java client has the following version:
- org.mockserver.client.server.MockServerClient - makes HTTP requests to a remote MockServer instance
- org.mockserver.integration.ClientAndServer - starts a local MockServer instance and makes HTTP requests to it
MockServer JavaScript Client
The JavaScript client has the following version:
- a browser based API file mockServerClient.js
- a Node.js npm module mockserver-client
To include the browser based client in an HTML page as follows:
<script src="https://rawgit.com/jamesdbloom/mockserver-client-node/mockserver-5.2.3/mockServerClient.js"></script>
new MockServerClient("localhost", 1080)
.when(
request()
.withMethod("GET")
.withPath("/view/cart")
.withCookies(
cookie("session", "4930456C-C718-476F-971F-CB8E047AB349")
)
.withQueryStringParameters(
param("cartId", "055CA455-1DF7-45BB-8535-4F83E7266092")
)
)
.respond(
response()
.withBody("some_response_body")
);
new ClientAndServer(1080)
.when(
request()
.withMethod("GET")
.withPath("/view/cart")
.withCookies(
cookie("session", "4930456C-C718-476F-971F-CB8E047AB349")
)
.withQueryStringParameters(
param("cartId", "055CA455-1DF7-45BB-8535-4F83E7266092")
)
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
.mockAnyResponse({
"httpRequest": {
"method": "GET",
"path": "/view/cart",
"queryStringParameters": {
"cartId": ["055CA455-1DF7-45BB-8535-4F83E7266092"]
},
"cookies": {
"session": "4930456C-C718-476F-971F-CB8E047AB349"
}
},
"httpResponse": {
"body": "some_response_body"
}
})
.then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
<script src="https://rawgit.com/jamesdbloom/mockserver-client-node/mockserver-5.2.3/mockServerClient.js"></script>
<script>
mockServerClient("localhost", 1080)
.mockAnyResponse({
"httpRequest": {
"method": "GET",
"path": "/view/cart",
"queryStringParameters": {
"cartId": ["055CA455-1DF7-45BB-8535-4F83E7266092"]
},
"cookies": {
"session": "4930456C-C718-476F-971F-CB8E047AB349"
}
},
"httpResponse": {
"body": "some_response_body"
}
})
.then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
</script>
curl -v -X PUT "http://localhost:1080/expectation" -d '{
"httpRequest" : {
"method" : "GET",
"path" : "/view/cart",
"queryStringParameters" : {
"cartId" : [ "055CA455-1DF7-45BB-8535-4F83E7266092" ]
},
"cookies" : {
"session" : "4930456C-C718-476F-971F-CB8E047AB349"
}
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
new MockServerClient("localhost", 1080)
.verify(
request()
.withPath("/some/path"),
VerificationTimes.atLeast(2)
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
.verify(
{
'path': '/some/path'
}, 2, false)
.then(
function () {
console.log("request found exactly 2 times");
},
function (error) {
console.log(error);
}
);
<script src="https://rawgit.com/jamesdbloom/mockserver-client-node/mockserver-5.2.3/mockServerClient.js"></script>
<script>
mockServerClient("localhost", 1080)
.verify(
{
'path': '/some/path'
}, 2, false)
.then(
function () {
console.log("request found exactly 2 times");
},
function (error) {
console.log(error);
}
);
</script>
curl -v -X PUT "http://localhost:1080/verify" -d '{
"httpRequest": {
"path": "/simple"
},
"times": {
"count": 2,
"exact": false
}
}'
new MockServerClient("localhost", 1080)
.verify(
request()
.withPath("/some/path/one"),
request()
.withPath("/some/path/two"),
request()
.withPath("/some/path/three")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
.verifySequence(
{
'path': '/some/path/one'
},
{
'path': '/some/path/two'
},
{
'path': '/some/path/three'
}
)
.then(
function () {
console.log("request sequence found in the order specified");
},
function (error) {
console.log(error);
}
);
<script src="https://rawgit.com/jamesdbloom/mockserver-client-node/mockserver-5.2.3/mockServerClient.js"></script>
<script>
mockServerClient("localhost", 1080)
.verifySequence(
{
'path': '/some/path/one'
},
{
'path': '/some/path/two'
},
{
'path': '/some/path/three'
}
)
.then(
function () {
console.log("request sequence found in the order specified");
},
function (error) {
console.log(error);
}
);
</script>
curl -v -X PUT "http://localhost:1080/verifySequence" -d '[
{
"path": "/some/path/one"
},
{
"path": "/some/path/two"
},
{
"path": "/some/path/three"
}
]'
HttpRequest[] recordedRequests = new MockServerClient("localhost", 1080)
.retrieveRecordedRequests(
request()
.withPath("/some/path")
.withMethod("POST")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
.retrieveRecordedRequests({
"path": "/some/path",
"method": "POST"
})
.then(
function (recordedRequests) {
console.log(JSON.stringify(recordedRequests));
},
function (error) {
console.log(error);
}
);
<script src="https://rawgit.com/jamesdbloom/mockserver-client-node/mockserver-5.2.3/mockServerClient.js"></script>
<script>
mockServerClient("localhost", 1080)
.retrieveRecordedRequests({
"path": "/some/path",
"method": "POST"
})
.then(
function (recordedRequests) {
console.log(JSON.stringify(recordedRequests));
},
function (error) {
console.log(error);
}
);
</script>
curl -v -X PUT "http://localhost:1080/retrieve?type=REQUESTS" -d '{
"path": "/some/path",
"method": "POST"
}'
HttpRequest[] recordedRequests = new MockServerClient("localhost", 1080)
.retrieveRecordedRequests(
request()
.withPath("/some/path")
.withMethod("POST")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
.retrieveLogMessages({
"path": "/some/path",
"method": "POST"
})
.then(
function (logMessages) {
// logMessages is a String[]
console.log(logMessages);
},
function (error) {
console.log(error);
}
);
<script src="https://rawgit.com/jamesdbloom/mockserver-client-node/mockserver-5.2.3/mockServerClient.js"></script>
<script>
mockServerClient("localhost", 1080)
.retrieveLogMessages({
"path": "/some/path",
"method": "POST"
})
.then(
function (logMessages) {
// logMessages is a String[]
console.log(logMessages);
},
function (error) {
console.log(error);
}
);
</script>
curl -v -X PUT "http://localhost:1080/retrieve?type=LOGS" -d '{
"path": "/some/path",
"method": "POST"
}'
new MockServerClient("localhost", 1080).clear(
request()
.withPath("/some/path")
.withMethod("POST")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
.clear({
'path': '/some/path'
})
.then(
function () {
console.log("cleared state that matches request matcher");
},
function (error) {
console.log(error);
}
);
<script src="https://rawgit.com/jamesdbloom/mockserver-client-node/mockserver-5.2.3/mockServerClient.js"></script>
<script>
mockServerClient("localhost", 1080)
.clear({
'path': '/some/path'
})
.then(
function () {
console.log("cleared state that matches request matcher");
},
function (error) {
console.log(error);
}
);
</script>
curl -v -X PUT "http://localhost:1080/clear" -d '{
"path": "/some/path"
}'
new MockServerClient("localhost", 1080).clear(
request()
.withPath("/some/path")
.withMethod("POST"),
ClearType.LOG
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
.clear({
'path': '/some/path'
}, 'LOG')
.then(
function () {
console.log("cleared state that matches request matcher");
},
function (error) {
console.log(error);
}
);
<script src="https://rawgit.com/jamesdbloom/mockserver-client-node/mockserver-5.2.3/mockServerClient.js"></script>
<script>
mockServerClient("localhost", 1080)
.clear({
'path': '/some/path'
}, 'LOG')
.then(
function () {
console.log("cleared state that matches request matcher");
},
function (error) {
console.log(error);
}
);
</script>
curl -v -X PUT "http://localhost:1080/clear?type=LOGS" -d '{
"path": "/some/path"
}'
new MockServerClient("localhost", 1080).reset();
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
.reset()
.then(
function () {
console.log("reset all state");
},
function (error) {
console.log(error);
}
);
<script src="https://rawgit.com/jamesdbloom/mockserver-client-node/mockserver-5.2.3/mockServerClient.js"></script>
<script>
mockServerClient("localhost", 1080)
.reset()
.then(
function () {
console.log("reset all state");
},
function (error) {
console.log(error);
}
);
</script>
curl -v -X PUT "http://localhost:1080/reset
List>nteger< boundPorts = new MockServerClient("localhost", 1080).bind(
0
);
curl -v -X PUT "http://localhost:1080/bind -d '{
"ports": [
0,
0
]
}'
List>nteger< boundPorts = new MockServerClient("localhost", 1080).bind(
1081, 1082
);
curl -v -X PUT "http://localhost:1080/bind -d '{
"ports": [
1081,
1082
]
}'