The following code examples show how to match against different elements of a request using different matchers.
new MockServerClient("localhost", 1080)
.when(
request()
.withPath("/some/path")
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"path": "/some/path"
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"path" : "/some/path"
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
new MockServerClient("localhost", 1080)
.when(
request()
.withPath("/some/path"),
Times.exactly(2)
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"path": "/some/path"
},
"httpResponse": {
"body": "some_response_body"
},
"times": {
"remainingTimes": 2,
"unlimited": false
},
"timeToLive": {
"unlimited": true
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"path" : "/some/path"
},
"httpResponse" : {
"body" : "some_response_body"
},
"times" : {
"remainingTimes" : 2,
"unlimited" : false
},
"timeToLive" : {
"unlimited" : true
}
}'
new MockServerClient("localhost", 1080)
.when(
request()
.withPath("/some/path"),
Times.once(),
TimeToLive.exactly(TimeUnit.SECONDS, 60L)
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"path": "/some/path"
},
"httpResponse": {
"body": "some_response_body"
},
"times": {
"remainingTimes": 1,
"unlimited": false
},
"timeToLive": {
"timeUnit": "SECONDS",
"timeToLive": 60,
"unlimited": false
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"path" : "/some/path"
},
"httpResponse" : {
"body" : "some_response_body"
},
"times" : {
"remainingTimes" : 1,
"unlimited" : false
},
"timeToLive" : {
"timeUnit" : "SECONDS",
"timeToLive" : 60,
"unlimited" : false
}
}'
For details of the full regex syntax supported please see the JDK documentation.
new MockServerClient("localhost", 1080)
.when(
request()
// matches any requests those path starts with "/some"
.withPath("/some.*")
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
// matches any requests those path starts with "/some"
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"path": "/some.*"
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"path" : "/some.*"
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
new MockServerClient("localhost", 1080)
.when(
request()
// matches any requests those path does NOT start with "/some"
.withPath(not("/some.*"))
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
// matches any requests those path does NOT start with "/some"
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"path": "!/some.*"
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"path" : "!/some.*"
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
For details of the full regex syntax supported please see the JDK documentation.
new MockServerClient("localhost", 1080)
.when(
request()
// matches any requests that does NOT have a "GET" method
.withMethod(not("P.*{2,3}"))
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"method": "P.*{2,3}"
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"method" : "P.*{2,3}"
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
new MockServerClient("localhost", 1080)
.when(
request()
// matches any requests that does NOT have a "GET" method
.withMethod(not("GET"))
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
// matches any requests that does NOT have a "GET" method
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"method": "!GET"
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"method" : "!GET"
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
For details of the full regex syntax supported please see the JDK documentation.
new MockServerClient("localhost", 1080)
.when(
request()
.withPath("/some/path")
.withQueryStringParameters(
param("[A-z]{0,10}", "055CA455-1DF7-45BB-8535-4F83E7266092")
)
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"path": "/some/path",
"queryStringParameters": {
"[A-z]{0,10}": ["055CA455-1DF7-45BB-8535-4F83E7266092"]
}
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest": {
"path": "/some/path",
"queryStringParameters": {
"[A-z]{0,10}": ["055CA455-1DF7-45BB-8535-4F83E7266092"]
}
},
"httpResponse": {
"body": "some_response_body"
}
}'
For details of the full regex syntax supported please see the JDK documentation.
new MockServerClient("localhost", 1080)
.when(
request()
.withPath("/some/path")
.withQueryStringParameters(
param("cartId", "[A-Z0-9\\-]+")
)
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"path": "/some/path",
"queryStringParameters": {
"cartId": ["[A-Z0-9\\-]+"]
}
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest": {
"path": "/some/path",
"queryStringParameters": {
"cartId": ["[A-Z0-9\\-]+"]
}
},
"httpResponse": {
"body": "some_response_body"
}
}'
new MockServerClient("localhost", 1080)
.when(
request()
.withMethod("GET")
.withPath("/some/path")
.withHeaders(
header("Accept", "application/json"),
header("Accept-Encoding", "gzip, deflate, br")
)
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"method": "GET",
"path": "/some/path",
"headers": {
"Accept": ["application/json"],
"Accept-Encoding": ["gzip, deflate, br"]
}
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"method" : "GET",
"path" : "/some/path",
"headers" : {
"Accept" : [ "application/json" ],
"Accept-Encoding" : [ "gzip, deflate, br" ]
}
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
For details of the full regex syntax supported please see the JDK documentation.
new MockServerClient("localhost", 1080)
.when(
request()
.withPath("/some/path")
.withHeader(
header("Accept.*")
)
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"path": "/some/path",
"headers": {
// matches requests that have any header starting with the name Accept
"Accept.*": [ "" ]
}
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
// matches requests that have any header starting with the name Accept
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest": {
"path": "/some/path",
"headers": {
"Accept.*": [ "" ]
}
},
"httpResponse": {
"body": "some_response_body"
}
}'
For details of the full regex syntax supported please see the JDK documentation.
new MockServerClient("localhost", 1080)
.when(
request()
.withPath("/some/path")
.withHeader(
header("Accept.*", ".*gzip.*")
)
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"path": "/some/path",
"headers": {
// matches requests that have a header with a name starting with Accept and a value containing gzip
"Accept.*": [".*gzip.*"]
}
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
// matches requests that have a header with a name starting with Accept and a value containing gzip
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest": {
"path": "/some/path",
"headers": {
"Accept.*": [".*gzip.*"]
}
},
"httpResponse": {
"body": "some_response_body"
}
}'
new MockServerClient("localhost", 1080)
.when(
request()
.withPath("/some/path")
.withHeaders(
// matches requests that have an Accept header without the value "application/json"
header(string("Accept"), not("application/json")),
// matches requests that have an Accept-Encoding without the substring "gzip"
header(string("Accept-Encoding"), not(".*gzip.*"))
)
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"path": "/some/path",
"headers": {
// matches requests that have an Accept header without the value "application/json"
"Accept": ["!application/json"],
// matches requests that have an Accept-Encoding without the substring "gzip"
"Accept-Encoding": ["!.*gzip.*"]
}
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"path" : "/some/path",
"headers" : {
"Accept" : [ "!application/json" ],
"Accept-Encoding" : [ "!.*gzip.*" ]
}
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
new MockServerClient("localhost", 1080)
.when(
request()
.withPath("/some/path")
.withHeaders(
// matches requests that do not have either an Accept or an Accept-Encoding header
header(not("Accept")),
header(not("Accept-Encoding"))
)
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"path": "/some/path",
"headers": {
// matches requests that do not have either an Accept or an Accept-Encoding header
"!Accept": [".*"],
"!Accept-Encoding": [".*"]
}
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"path" : "/some/path",
"headers" : {
"!Accept" : [ ".*" ],
"!Accept-Encoding" : [ ".*" ]
}
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
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")
);
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);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/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)
.when(
request()
.withBody(subString("some_string"))
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"body": {
"type": "STRING",
"string": "some_string",
"subString": true
}
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"body" : {
"type": "STRING",
"string": "some_string",
"subString": true
}
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
new MockServerClient("localhost", 1080)
.when(
request()
.withBody(exact("我说中国话", Charsets.UTF_16))
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"body": {
"type": "STRING",
"string": "我说中国话",
"contentType": "text/plain; charset=utf-16"
}
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"body" : {
"type" : "STRING",
"string" : "我说中国话",
"contentType" : "text/plain; charset=utf-16"
}
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
For details of the full regex syntax supported please see the JDK documentation.
new MockServerClient("localhost", 1080)
.when(
request()
.withBody(regex("starts_with_.*"))
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"type": "REGEX",
"body": "starts_with_.*"
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"type": "REGEX",
"body" : "starts_with_.*"
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
new MockServerClient("localhost", 1080)
.when(
request()
.withMethod("POST")
.withHeaders(
header("Content-Type", "application/x-www-form-urlencoded")
)
.withBody(
params(
param("email", "joe.blogs@gmail.com"),
param("password", "secure_Password123")
)
)
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"method": "POST",
"headers": {
"Content-Type": ["application/x-www-form-urlencoded"]
},
"body": {
"type": "PARAMETERS",
"parameters": {
"email": ["joe.blogs@gmail.com"],
"password": ["secure_Password123"]
}
}
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"method" : "POST",
"headers" : {
"Content-Type" : [ "application/x-www-form-urlencoded" ]
},
"body" : {
"type" : "PARAMETERS",
"parameters" : {
"email" : [ "joe.blogs@gmail.com" ],
"password" : [ "secure_Password123" ]
}
}
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
Matches if at least one value is returned by the JsonPath expression
For details of the full xpath supported please see www.w3.org.
new MockServerClient("localhost", 1080)
.when(
request()
// matches any request with an XML body that contains
// one or more elements that match the XPath expression
.withBody(
xpath("/bookstore/book[price>30]/price")
)
)
.respond(
response()
.withBody("some_response_body")
);
// matches a request with the following body:
/*
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>31.95</price>
</book>
</bookstore>
*/
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"body": {
// matches any request with an XML body that contains
// one or more elements that match the XPath expression
"type": "XPATH",
"xpath": "/bookstore/book[price>30]/price"
}
},
"httpResponse": {
"body": "some_response_body"
}
});
// matches a request with the following body:
/*
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>31.95</price>
</book>
</bookstore>
*/
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"body" : {
"type" : "XPATH",
"xpath" : "/bookstore/book[price>30]/price"
}
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
Matches if at least one value is returned by the JsonPath expression
For details of the full xpath supported please see www.w3.org.
new MockServerClient("localhost", 1080)
.when(
request()
.withBody(
// matches any request with an XML body that does NOT
// contain one or more elements that match the XPath expression
not(xpath("/bookstore/book[price>30]/price"))
)
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"body": {
// matches any request with an XML body that does NOT
// contain one or more elements that match the XPath expression
"not": true,
"type": "XPATH",
"xpath": "/bookstore/book[price>30]/price"
}
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"body" : {
"not" : true,
"type" : "XPATH",
"xpath" : "/bookstore/book[price>30]/price"
}
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
new MockServerClient("localhost", 1080)
.when(
request()
.withBody(
xml("<bookstore>" + System.lineSeparator() +
" <book nationality=\"ITALIAN\" category=\"COOKING\">" + System.lineSeparator() +
" <title lang=\"en\">Everyday Italian</title>" + System.lineSeparator() +
" <author>Giada De Laurentiis</author>" + System.lineSeparator() +
" <year>2005</year>" + System.lineSeparator() +
" <price>30.00</price>" + System.lineSeparator() +
" </book>" + System.lineSeparator() +
"</bookstore>")
)
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"body": {
"type": "XML",
"xml": "<bookstore>\n" +
" <book nationality=\"ITALIAN\" category=\"COOKING\">\n" +
" <title lang=\"en\">Everyday Italian</title>\n" +
" <author>Giada De Laurentiis</author>\n" +
" <year>2005</year>\n" +
" <price>30.00</price>\n" +
" </book>\n" +
"</bookstore>"
}
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"body" : {
"type" : "XML",
"xml" : "<bookstore> <book nationality=\"ITALIAN\" category=\"COOKING\"><title lang=\"en\">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book> </bookstore>"
}
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
new MockServerClient("localhost", 1080)
.when(
request()
.withBody(
xmlSchema("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + System.lineSeparator() +
"<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" elementFormDefault=\"qualified\" attributeFormDefault=\"unqualified\">" + System.lineSeparator() +
" <!-- XML Schema Generated from XML Document on Wed Jun 28 2017 21:52:45 GMT+0100 (BST) -->" + System.lineSeparator() +
" <!-- with XmlGrid.net Free Online Service http://xmlgrid.net -->" + System.lineSeparator() +
" <xs:element name=\"notes\">" + System.lineSeparator() +
" <xs:complexType>" + System.lineSeparator() +
" <xs:sequence>" + System.lineSeparator() +
" <xs:element name=\"note\" maxOccurs=\"unbounded\">" + System.lineSeparator() +
" <xs:complexType>" + System.lineSeparator() +
" <xs:sequence>" + System.lineSeparator() +
" <xs:element name=\"to\" minOccurs=\"1\" maxOccurs=\"1\" type=\"xs:string\"></xs:element>" + System.lineSeparator() +
" <xs:element name=\"from\" minOccurs=\"1\" maxOccurs=\"1\" type=\"xs:string\"></xs:element>" + System.lineSeparator() +
" <xs:element name=\"heading\" minOccurs=\"1\" maxOccurs=\"1\" type=\"xs:string\"></xs:element>" + System.lineSeparator() +
" <xs:element name=\"body\" minOccurs=\"1\" maxOccurs=\"1\" type=\"xs:string\"></xs:element>" + System.lineSeparator() +
" </xs:sequence>" + System.lineSeparator() +
" </xs:complexType>" + System.lineSeparator() +
" </xs:element>" + System.lineSeparator() +
" </xs:sequence>" + System.lineSeparator() +
" </xs:complexType>" + System.lineSeparator() +
" </xs:element>" + System.lineSeparator() +
"</xs:schema>")
)
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"body": {
"type": "XML_SCHEMA",
"xmlSchema": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" elementFormDefault=\"qualified\" attributeFormDefault=\"unqualified\">\n" +
" <!-- XML Schema Generated from XML Document on Wed Jun 28 2017 21:52:45 GMT+0100 (BST) -->\n" +
" <!-- with XmlGrid.net Free Online Service http://xmlgrid.net -->\n" +
" <xs:element name=\"notes\">\n" +
" <xs:complexType>\n" +
" <xs:sequence>\n" +
" <xs:element name=\"note\" maxOccurs=\"unbounded\">\n" +
" <xs:complexType>\n" +
" <xs:sequence>\n" +
" <xs:element name=\"to\" minOccurs=\"1\" maxOccurs=\"1\" type=\"xs:string\"></xs:element>\n" +
" <xs:element name=\"from\" minOccurs=\"1\" maxOccurs=\"1\" type=\"xs:string\"></xs:element>\n" +
" <xs:element name=\"heading\" minOccurs=\"1\" maxOccurs=\"1\" type=\"xs:string\"></xs:element>\n" +
" <xs:element name=\"body\" minOccurs=\"1\" maxOccurs=\"1\" type=\"xs:string\"></xs:element>\n" +
" </xs:sequence>\n" +
" </xs:complexType>\n" +
" </xs:element>\n" +
" </xs:sequence>\n" +
" </xs:complexType>\n" +
" </xs:element>\n</xs:schema>"
}
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"body" : {
"type" : "XML_SCHEMA",
"xmlSchema" : "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" elementFormDefault=\"qualified\" attributeFormDefault=\"unqualified\"> <xs:element name=\"notes\"> <xs:complexType> <xs:sequence> <xs:element name=\"note\" maxOccurs=\"unbounded\"> <xs:complexType> <xs:sequence> <xs:element name=\"to\" minOccurs=\"1\" maxOccurs=\"1\" type=\"xs:string\"></xs:element> <xs:element name=\"from\" minOccurs=\"1\" maxOccurs=\"1\" type=\"xs:string\"></xs:element> <xs:element name=\"heading\" minOccurs=\"1\" maxOccurs=\"1\" type=\"xs:string\"></xs:element> <xs:element name=\"body\" minOccurs=\"1\" maxOccurs=\"1\" type=\"xs:string\"></xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>"
}
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
new MockServerClient("localhost", 1080)
.when(
request()
.withBody(
xmlSchemaFromResource("org/mockserver/examples/mockserver/testXmlSchema.xsd")
)
)
.respond(
response()
.withBody("some_response_body")
);
Json body matcher supports two mode STRICT which matches all fields, order of arrays and no additional fields allowed, and ONLY_MATCHING_FIELDS which only matches fields provided in the request matcher
new MockServerClient("localhost", 1080)
.when(
request()
.withBody(
json("{" + System.lineSeparator() +
" \"id\": 1," + System.lineSeparator() +
" \"name\": \"A green door\"," + System.lineSeparator() +
" \"price\": 12.50," + System.lineSeparator() +
" \"tags\": [\"home\", \"green\"]" + System.lineSeparator() +
"}",
MatchType.STRICT
)
)
)
.respond(
response()
.withStatusCode(HttpStatusCode.ACCEPTED_202.code())
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"body": {
"type": "JSON",
"json": JSON.stringify({
"id": 1,
"name": "A green door",
"price": 12.50,
"tags": ["home", "green"]
}),
"matchType": "STRICT"
}
},
"httpResponse": {
"statusCode": 202,
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"body" : {
"type" : "JSON",
"json" : "{ \"id\": 1, \"name\": \"A green door\", \"price\": 12.50, \"tags\": [\"home\", \"green\"] }",
"matchType" : "STRICT"
}
},
"httpResponse" : {
"statusCode" : 202,
"body" : "some_response_body"
}
}'
Json body matcher supports two mode STRICT which matches all fields, order of arrays and no additional fields allowed, and ONLY_MATCHING_FIELDS which only matches fields provided in the request matcher
new MockServerClient("localhost", 1080)
.when(
request()
.withBody(
json("{" + System.lineSeparator() +
" \"id\": 1," + System.lineSeparator() +
" \"name\": \"A green door\"," + System.lineSeparator() +
" \"price\": 12.50," + System.lineSeparator() +
" \"tags\": [\"home\", \"green\"]" + System.lineSeparator() +
"}",
MatchType.ONLY_MATCHING_FIELDS
)
)
)
.respond(
response()
.withStatusCode(HttpStatusCode.ACCEPTED_202.code())
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"body": {
"type": "JSON",
"json": JSON.stringify({
"id": 1,
"name": "A green door",
"price": 12.50,
"tags": ["home", "green"]
})
}
},
"httpResponse": {
"statusCode": 202,
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"body" : {
"type" : "JSON",
"json" : "{ \"id\": 1, \"name\": \"A green door\", \"price\": 12.50, \"tags\": [\"home\", \"green\"] }"
}
},
"httpResponse" : {
"statusCode" : 202,
"body" : "some_response_body"
}
}'
For details of the full json schema supported please see json-schema.org.
new MockServerClient("localhost", 1080)
.when(
request()
.withBody(
jsonSchema("{" + System.lineSeparator() +
" \"$schema\": \"http://json-schema.org/draft-04/schema#\"," + System.lineSeparator() +
" \"title\": \"Product\"," + System.lineSeparator() +
" \"description\": \"A product from Acme's catalog\"," + System.lineSeparator() +
" \"type\": \"object\"," + System.lineSeparator() +
" \"properties\": {" + System.lineSeparator() +
" \"id\": {" + System.lineSeparator() +
" \"description\": \"The unique identifier for a product\"," + System.lineSeparator() +
" \"type\": \"integer\"" + System.lineSeparator() +
" }," + System.lineSeparator() +
" \"name\": {" + System.lineSeparator() +
" \"description\": \"Name of the product\"," + System.lineSeparator() +
" \"type\": \"string\"" + System.lineSeparator() +
" }," + System.lineSeparator() +
" \"price\": {" + System.lineSeparator() +
" \"type\": \"number\"," + System.lineSeparator() +
" \"minimum\": 0," + System.lineSeparator() +
" \"exclusiveMinimum\": true" + System.lineSeparator() +
" }," + System.lineSeparator() +
" \"tags\": {" + System.lineSeparator() +
" \"type\": \"array\"," + System.lineSeparator() +
" \"items\": {" + System.lineSeparator() +
" \"type\": \"string\"" + System.lineSeparator() +
" }," + System.lineSeparator() +
" \"minItems\": 1," + System.lineSeparator() +
" \"uniqueItems\": true" + System.lineSeparator() +
" }" + System.lineSeparator() +
" }," + System.lineSeparator() +
" \"required\": [\"id\", \"name\", \"price\"]" + System.lineSeparator() +
"}"))
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"body": {
"type": "JSON_SCHEMA",
"jsonSchema": JSON.stringify({
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
"id": {
"description": "The unique identifier for a product",
"type": "integer"
},
"name": {
"description": "Name of the product",
"type": "string"
},
"price": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"uniqueItems": true
}
},
"required": ["id", "name", "price"]
})
}
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"body" : {
"type" : "JSON_SCHEMA",
"jsonSchema" : "{\"$schema\": \"http://json-schema.org/draft-04/schema#\", \"title\": \"Product\", \"description\": \"A product from Acme's catalog\", \"type\": \"object\", \"properties\": { \"id\": { \"description\": \"The unique identifier for a product\", \"type\": \"integer\" }, \"name\": { \"description\": \"Name of the product\", \"type\": \"string\"}, \"price\": { \"type\": \"number\", \"minimum\": 0, \"exclusiveMinimum\": true }, \"tags\": { \"type\": \"array\", \"items\": { \"type\": \"string\" }, \"minItems\": 1, \"uniqueItems\": true } }, \"required\": [\"id\", \"name\", \"price\"] }"
}
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
Matches if at least one value is returned by the JsonPath expression. For details of the full JsonPath supported please see github.com/json-path/JsonPath.
new MockServerClient("localhost", 1080)
.when(
request()
.withBody(
// matches any request with an JSON body that contain
// one or more fields that match the JsonPath expression
jsonPath("$.store.book[?(@.price < 10)]")
)
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"body": {
// matches any request with an JSON body that contain
// one or more fields that match the JsonPath expression
"type": "JSON_PATH",
"jsonPath": "$.store.book[?(@.price < 10)]"
}
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"body" : {
"type": "JSON_PATH",
"jsonPath": "$.store.book[?(@.price < 10)]"
}
},
"httpResponse" : {
"body" : "some_response_body"
}
}'
Matches if at least one value is returned by the JsonPath expression. For details of the full JsonPath supported please see github.com/json-path/JsonPath.
new MockServerClient("localhost", 1080)
.when(
request()
.withBody(
// matches any request with an JSON body that does NOT contain
// one or more fields that match the JsonPath expression
not(jsonPath("$.store.book[?(@.price < 10)]"))
)
)
.respond(
response()
.withBody("some_response_body")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080).mockAnyResponse({
"httpRequest": {
"body": {
// matches any request with an JSON body that does NOT contain
// one or more fields that match the JsonPath expression
"not": true,
"type": "JSON_PATH",
"jsonPath": "$.store.book[?(@.price < 10)]"
}
},
"httpResponse": {
"body": "some_response_body"
}
}).then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest" : {
"body" : {
"not": true,
"type": "JSON_PATH",
"jsonPath": "$.store.book[?(@.price < 10)]"
}
},
"httpResponse" : {
"body" : "some_response_body"
}
}'