Example Code - JavaScript
To setup a simple expectation that returns a JSON body for all requests on a given path:
mockServerClient("localhost", 1080).mockSimpleResponse('/somePath', { name: 'value' }, 203);
To setup a more complex expectation:
mockServerClient("localhost", 1080).mockAnyResponse(
{
'httpRequest': {
'method': 'POST',
'path': '/somePath',
'queryStringParameters': [
{
'name': 'test',
'values': [ 'true' ]
}
],
'body': {
'type': 'STRING',
'value': 'someBody'
}
},
'httpResponse': {
'statusCode': 200,
'body': JSON.stringify({ name: 'value' }),
'delay': {
'timeUnit': 'MILLISECONDS',
'value': 250
}
},
'times': {
'remainingTimes': 1,
'unlimited': false
}
}
);
To setup an expectation that forwards all requests:
mockServerClient("localhost", 1080).mockAnyResponse(
{
'httpRequest': {
'method': 'GET',
'path': '/somePath'
},
"httpForward": {
"host": "www.mock-server.com",
"port": 80,
"scheme": "HTTP"
},
'times': {
'remainingTimes': 1,
'unlimited': false
}
}
);
It is also possible to make AJAX calls directly without the client as follows:
var xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open("PUT", "http://localhost:1080/expectation", false);
xmlHttpRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xmlHttpRequest.send(JSON.stringify({
"httpRequest": {
"method": "POST",
"path": "/login",
"body": {
"type": "JSON",
"value": JSON.stringify({ username: "foo", password: "bar" })
}
},
"httpResponse": {
"statusCode": 401,
"headers": [
{
"name": "Content-Type",
"values": ["application/json; charset=utf-8"]
},
{
"name": "Cache-Control",
"values": ["public, max-age=86400"]
}
],
"body": JSON.stringify({ message: "incorrect username and password combination" })
}
}));