mockserver-client
Advanced tools
Comparing version 5.2.1 to 5.2.2
@@ -9,4 +9,4 @@ { | ||
"dependencies": { | ||
"mockserver-client": "5.2.1" | ||
"mockserver-client": "5.2.2" | ||
} | ||
} |
@@ -9,5 +9,5 @@ { | ||
"dependencies": { | ||
"mockserver-client": "5.2.1", | ||
"mockserver-node": "5.2.1" | ||
"mockserver-client": "5.2.2", | ||
"mockserver-node": "5.2.2" | ||
} | ||
} |
@@ -9,5 +9,5 @@ { | ||
"dependencies": { | ||
"mockserver-client": "5.2.1", | ||
"mockserver-node": "5.2.1" | ||
"mockserver-client": "5.2.2", | ||
"mockserver-node": "5.2.2" | ||
} | ||
} |
@@ -11,559 +11,583 @@ /* | ||
(function () { | ||
"use strict"; | ||
"use strict"; | ||
var makeRequest = (typeof require !== 'undefined' ? require('./sendRequest').sendRequest : function (host, port, path, jsonBody, resolveCallback) { | ||
var body = (typeof jsonBody === "string" ? jsonBody : JSON.stringify(jsonBody || "")); | ||
var url = 'http://' + host + ':' + port + path; | ||
var makeRequest = (typeof require !== 'undefined' ? require('./sendRequest').sendRequest : function (host, port, path, jsonBody, resolveCallback) { | ||
var body = (typeof jsonBody === "string" ? jsonBody : JSON.stringify(jsonBody || "")); | ||
var url = 'http://' + host + ':' + port + path; | ||
return { | ||
then: function (sucess, error) { | ||
try { | ||
var xmlhttp = new XMLHttpRequest(); | ||
xmlhttp.addEventListener("load", (function (sucess, error) { | ||
return function () { | ||
if (error && this.status >= 400 && this.status < 600) { | ||
error({ | ||
statusCode: this.status, | ||
body: this.responseText | ||
}); | ||
} else { | ||
sucess && sucess({ | ||
statusCode: this.status, | ||
body: this.responseText | ||
}); | ||
} | ||
}; | ||
})(sucess, error)); | ||
xmlhttp.open('PUT', url); | ||
xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8"); | ||
xmlhttp.send(body); | ||
} catch (e) { | ||
error && error(e); | ||
} | ||
} | ||
}; | ||
}); | ||
return { | ||
then: function (sucess, error) { | ||
try { | ||
var xmlhttp = new XMLHttpRequest(); | ||
xmlhttp.addEventListener("load", (function (sucess, error) { | ||
return function () { | ||
if (error && this.status >= 400 && this.status < 600) { | ||
if (this.statusCode === 404) { | ||
error("404 Not Found"); | ||
} else { | ||
error(this.responseText); | ||
} | ||
} else { | ||
sucess && sucess({ | ||
statusCode: this.status, | ||
body: this.responseText | ||
}); | ||
} | ||
}; | ||
})(sucess, error)); | ||
xmlhttp.open('PUT', url); | ||
xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8"); | ||
xmlhttp.send(body); | ||
} catch (e) { | ||
error && error(e); | ||
} | ||
} | ||
}; | ||
}); | ||
/** | ||
* Start the client communicating to a MockServer at the specified host and port | ||
* for example: | ||
* | ||
* var client = mockServerClient("localhost", 1080); | ||
* | ||
* @param host the host for the MockServer to communicate with | ||
* @param port the port for the MockServer to communicate with | ||
* @param contextPath the context path if MockServer was deployed as a war | ||
*/ | ||
mockServerClient = function (host, port, contextPath) { | ||
var cleanedContextPath = (function (contextPath) { | ||
if (contextPath) { | ||
if (!contextPath.endsWith("/")) { | ||
contextPath += "/"; | ||
} | ||
if (!contextPath.startsWith("/")) { | ||
contextPath = "/" + contextPath; | ||
} | ||
return contextPath; | ||
} else { | ||
return ''; | ||
} | ||
})(contextPath); | ||
/** | ||
* The default headers added to to the mocked response when using mockSimpleResponse(...) | ||
* Start the client communicating to a MockServer at the specified host and port | ||
* for example: | ||
* | ||
* var client = mockServerClient("localhost", 1080); | ||
* | ||
* @param host the host for the MockServer to communicate with | ||
* @param port the port for the MockServer to communicate with | ||
* @param contextPath the context path if MockServer was deployed as a war | ||
*/ | ||
var defaultResponseHeaders = [ | ||
{"name": "Content-Type", "values": ["application/json; charset=utf-8"]}, | ||
{"name": "Cache-Control", "values": ["no-cache, no-store"]} | ||
]; | ||
var defaultRequestHeaders = []; | ||
mockServerClient = function (host, port, contextPath) { | ||
var arrayUniqueConcatenate = function (arrayTarget, arraySource) { | ||
if (arraySource && arraySource.length) { | ||
if (arrayTarget && arrayTarget.length) { | ||
for (var i = 0; i < arraySource.length; i++) { | ||
var arrayTargetAlreadyHasValue = false; | ||
for (var j = 0; j < arrayTarget.length; j++) { | ||
if (JSON.stringify(arraySource[i]) === JSON.stringify(arrayTarget[j])) { | ||
arrayTargetAlreadyHasValue = true; | ||
} | ||
var cleanedContextPath = (function (contextPath) { | ||
if (contextPath) { | ||
if (!contextPath.endsWith("/")) { | ||
contextPath += "/"; | ||
} | ||
if (!contextPath.startsWith("/")) { | ||
contextPath = "/" + contextPath; | ||
} | ||
return contextPath; | ||
} else { | ||
return ''; | ||
} | ||
if (!arrayTargetAlreadyHasValue) { | ||
arrayTarget.push(arraySource[i]); | ||
})(contextPath); | ||
/** | ||
* The default headers added to to the mocked response when using mockSimpleResponse(...) | ||
*/ | ||
var defaultResponseHeaders = [ | ||
{"name": "Content-Type", "values": ["application/json; charset=utf-8"]}, | ||
{"name": "Cache-Control", "values": ["no-cache, no-store"]} | ||
]; | ||
var defaultRequestHeaders = []; | ||
var arrayUniqueConcatenate = function (arrayTarget, arraySource) { | ||
if (arraySource && arraySource.length) { | ||
if (arrayTarget && arrayTarget.length) { | ||
for (var i = 0; i < arraySource.length; i++) { | ||
var arrayTargetAlreadyHasValue = false; | ||
for (var j = 0; j < arrayTarget.length; j++) { | ||
if (JSON.stringify(arraySource[i]) === JSON.stringify(arrayTarget[j])) { | ||
arrayTargetAlreadyHasValue = true; | ||
} | ||
} | ||
if (!arrayTargetAlreadyHasValue) { | ||
arrayTarget.push(arraySource[i]); | ||
} | ||
} | ||
} else { | ||
arrayTarget = arraySource; | ||
} | ||
} | ||
} | ||
} else { | ||
arrayTarget = arraySource; | ||
} | ||
} | ||
return arrayTarget; | ||
}; | ||
var createRequestMatcher = function (path) { | ||
return { | ||
method: "", | ||
path: path, | ||
body: "", | ||
headers: defaultRequestHeaders, | ||
cookies: [], | ||
queryStringParameters: [] | ||
}; | ||
return arrayTarget; | ||
}; | ||
var createRequestMatcher = function (path) { | ||
return { | ||
method: "", | ||
path: path, | ||
body: "", | ||
headers: defaultRequestHeaders, | ||
cookies: [], | ||
queryStringParameters: [] | ||
}; | ||
}; | ||
var createExpectation = function (path, responseBody, statusCode) { | ||
return { | ||
httpRequest: createRequestMatcher(path), | ||
httpResponse: { | ||
statusCode: statusCode || 200, | ||
body: JSON.stringify(responseBody), | ||
cookies: [], | ||
headers: defaultResponseHeaders, | ||
delay: { | ||
timeUnit: "MICROSECONDS", | ||
value: 0 | ||
} | ||
}, | ||
times: { | ||
remainingTimes: 1, | ||
unlimited: false | ||
} | ||
}; | ||
}; | ||
var createExpectationWithCallback = function (requestMatcher, clientId, times) { | ||
var timesObject; | ||
if (typeof times === 'number') { | ||
timesObject = { | ||
remainingTimes: times, | ||
unlimited: false | ||
}; | ||
} else if (typeof times === 'object') { | ||
timesObject = times; | ||
} | ||
requestMatcher.headers = arrayUniqueConcatenate(requestMatcher.headers, defaultRequestHeaders); | ||
return { | ||
httpRequest: requestMatcher, | ||
httpObjectCallback: { | ||
clientId: clientId | ||
}, | ||
times: timesObject || { | ||
remainingTimes: 1, | ||
unlimited: false | ||
} | ||
}; | ||
}; | ||
var createExpectation = function (path, responseBody, statusCode) { | ||
return { | ||
httpRequest: createRequestMatcher(path), | ||
httpResponse: { | ||
statusCode: statusCode || 200, | ||
body: JSON.stringify(responseBody), | ||
cookies: [], | ||
headers: defaultResponseHeaders, | ||
delay: { | ||
timeUnit: "MICROSECONDS", | ||
value: 0 | ||
} | ||
}, | ||
times: { | ||
remainingTimes: 1, | ||
unlimited: false | ||
} | ||
}; | ||
}; | ||
var createExpectationWithCallback = function (requestMatcher, clientId, times) { | ||
var timesObject; | ||
if (typeof times === 'number') { | ||
timesObject = { | ||
remainingTimes: times, | ||
unlimited: false | ||
}; | ||
} else if (typeof times === 'object') { | ||
timesObject = times; | ||
} | ||
requestMatcher.headers = arrayUniqueConcatenate(requestMatcher.headers, defaultRequestHeaders); | ||
return { | ||
httpRequest: requestMatcher, | ||
httpObjectCallback: { | ||
clientId: clientId | ||
}, | ||
times: timesObject || { | ||
remainingTimes: 1, | ||
unlimited: false | ||
} | ||
}; | ||
}; | ||
var WebSocketClient = (typeof require !== 'undefined' ? require('./webSocketClient').webSocketClient : function (host, port, contextPath) { | ||
var clientId; | ||
var clientIdHandler; | ||
var requestHandler; | ||
var browserWebSocket; | ||
var WebSocketClient = (typeof require !== 'undefined' ? require('./webSocketClient').webSocketClient : function (host, port, contextPath) { | ||
var clientId; | ||
var clientIdHandler; | ||
var requestHandler; | ||
var browserWebSocket; | ||
if (typeof(window) !== "undefined") { | ||
if (window.WebSocket) { | ||
browserWebSocket = window.WebSocket; | ||
} else if (window.MozWebSocket) { | ||
browserWebSocket = window.MozWebSocket; | ||
} else { | ||
throw "Your browser does not support web sockets."; | ||
} | ||
} | ||
if (browserWebSocket) { | ||
var webSocketLocation = "ws://" + host + ":" + port + contextPath + "/_mockserver_callback_websocket"; | ||
var socket = new WebSocket(webSocketLocation); | ||
socket.onmessage = function (event) { | ||
var message = JSON.parse(event.data); | ||
if (message.type === "org.mockserver.model.HttpRequest") { | ||
var request = JSON.parse(message.value); | ||
var response = requestHandler(request); | ||
if (socket.readyState === WebSocket.OPEN) { | ||
socket.send(JSON.stringify(response)); | ||
} else { | ||
throw "The socket is not open."; | ||
if (typeof(window) !== "undefined") { | ||
if (window.WebSocket) { | ||
browserWebSocket = window.WebSocket; | ||
} else if (window.MozWebSocket) { | ||
browserWebSocket = window.MozWebSocket; | ||
} else { | ||
throw "Your browser does not support web sockets."; | ||
} | ||
} | ||
} else if (message.type === "org.mockserver.client.serialization.model.WebSocketClientIdDTO") { | ||
var registration = JSON.parse(message.value); | ||
if (registration.clientId) { | ||
clientId = registration.clientId; | ||
if (clientIdHandler) { | ||
clientIdHandler(clientId); | ||
} | ||
} | ||
} | ||
}; | ||
socket.onopen = function (event) { | ||
}; | ||
socket.onclose = function (event) { | ||
if (browserWebSocket) { | ||
var webSocketLocation = "ws://" + host + ":" + port + contextPath + "/_mockserver_callback_websocket"; | ||
var socket = new WebSocket(webSocketLocation); | ||
socket.onmessage = function (event) { | ||
var message = JSON.parse(event.data); | ||
if (message.type === "org.mockserver.model.HttpRequest") { | ||
var request = JSON.parse(message.value); | ||
var response = requestHandler(request); | ||
if (socket.readyState === WebSocket.OPEN) { | ||
socket.send(JSON.stringify(response)); | ||
} else { | ||
throw "The socket is not open."; | ||
} | ||
} else if (message.type === "org.mockserver.client.serialization.model.WebSocketClientIdDTO") { | ||
var registration = JSON.parse(message.value); | ||
if (registration.clientId) { | ||
clientId = registration.clientId; | ||
if (clientIdHandler) { | ||
clientIdHandler(clientId); | ||
} | ||
} | ||
} | ||
}; | ||
socket.onopen = function (event) { | ||
}; | ||
} | ||
}; | ||
socket.onclose = function (event) { | ||
function requestCallback(callback) { | ||
requestHandler = callback; | ||
} | ||
}; | ||
} | ||
function clientIdCallback(callback) { | ||
clientIdHandler = callback; | ||
if (clientId) { | ||
clientIdHandler(clientId); | ||
} | ||
} | ||
function requestCallback(callback) { | ||
requestHandler = callback; | ||
} | ||
return { | ||
requestCallback: requestCallback, | ||
clientIdCallback: clientIdCallback | ||
}; | ||
}); | ||
function clientIdCallback(callback) { | ||
clientIdHandler = callback; | ||
if (clientId) { | ||
clientIdHandler(clientId); | ||
} | ||
} | ||
/** | ||
* Setup an expectation in the MockServer by specifying an expectation object | ||
* for example: | ||
* | ||
* mockServerClient("localhost", 1080).mockAnyResponse( | ||
* { | ||
* 'httpRequest': { | ||
* 'path': '/somePath', | ||
* 'body': { | ||
* 'type': "STRING", | ||
* 'value': 'someBody' | ||
* } | ||
* }, | ||
* 'httpResponse': { | ||
* 'statusCode': 200, | ||
* 'body': Base64.encode(JSON.stringify({ name: 'first_body' })), | ||
* 'delay': { | ||
* 'timeUnit': 'MILLISECONDS', | ||
* 'value': 250 | ||
* } | ||
* }, | ||
* 'times': { | ||
* 'remainingTimes': 1, | ||
* 'unlimited': false | ||
* } | ||
* } | ||
* ); | ||
* | ||
* @param expectation the expectation to setup on the MockServer | ||
*/ | ||
var mockAnyResponse = function (expectation) { | ||
return makeRequest(host, port, "/expectation", addDefaultExpectationHeaders(expectation)); | ||
}; | ||
/** | ||
* Setup an expectation in the MockServer by specifying a request matcher, and | ||
* a local request handler function. The request handler function receives each | ||
* request (that matches the request matcher) and returns the response that the | ||
* MockServer will return for this expectation. | ||
* | ||
* for example: | ||
* | ||
* mockServerClient("localhost", 1080).mockWithCallback( | ||
* { | ||
* path: '/somePath', | ||
* body: 'some_request_body' | ||
* }, | ||
* function (request) { | ||
* var response = { | ||
* statusCode: 200, | ||
* body: 'some_response_body' | ||
* }; | ||
* return response | ||
* } | ||
* ).then( | ||
* function () { | ||
* alert('expectation sent'); | ||
* }, | ||
* function (error) { | ||
* alert('error'); | ||
* } | ||
* ); | ||
* | ||
* @param requestMatcher the request matcher for the expectation | ||
* @param requestHandler the function to be called back when the request is matched | ||
*/ | ||
var mockWithCallback = function (requestMatcher, requestHandler, times) { | ||
return { | ||
then: function (sucess, error) { | ||
try { | ||
var webSocketClient = WebSocketClient(host, port, cleanedContextPath); | ||
webSocketClient.requestCallback(function (request) { | ||
return { | ||
type: "org.mockserver.model.HttpResponse", | ||
value: JSON.stringify(requestHandler(request)) | ||
}; | ||
}); | ||
webSocketClient.clientIdCallback(function (clientId) { | ||
return makeRequest(host, port, "/expectation", createExpectationWithCallback(requestMatcher, clientId, times)).then(sucess, error) | ||
}); | ||
} catch (e) { | ||
error && error(e); | ||
} | ||
} | ||
}; | ||
}; | ||
/** | ||
* Setup an expectation in the MockServer without having to specify the full expectation object | ||
* for example: | ||
* | ||
* mockServerClient("localhost", 1080).mockSimpleResponse('/somePath', { name: 'value' }, 203); | ||
* | ||
* @param path the path to match requests against | ||
* @param responseBody the response body to return if a request matches | ||
* @param statusCode the response code to return if a request matches | ||
*/ | ||
var mockSimpleResponse = function (path, responseBody, statusCode) { | ||
return mockAnyResponse(createExpectation(path, responseBody, statusCode)); | ||
}; | ||
/** | ||
* Override: | ||
* | ||
* - default headers that are used to specify the response headers in mockSimpleResponse(...) | ||
* (note: if you use mockAnyResponse(...) the default headers are not used) | ||
* | ||
* - headers added to every request matcher, this is particularly useful for running tests in parallel | ||
* | ||
* for example: | ||
* | ||
* mockServerClient("localhost", 1080).setDefaultHeaders([ | ||
* {"name": "Content-Type", "values": ["application/json; charset=utf-8"]}, | ||
* {"name": "Cache-Control", "values": ["no-cache, no-store"]} | ||
* ],[ | ||
* {"name": "sessionId", "values": ["786fcf9b-606e-605f-181d-c245b55e5eac"]} | ||
* ]) | ||
* | ||
* @param responseHeaders the default headers to be added to every response | ||
* @param requestHeaders the default headers to be added to every request matcher | ||
*/ | ||
var setDefaultHeaders = function (responseHeaders, requestHeaders) { | ||
if (responseHeaders) { | ||
defaultResponseHeaders = responseHeaders; | ||
} | ||
if (requestHeaders) { | ||
defaultRequestHeaders = requestHeaders; | ||
} | ||
return _this; | ||
}; | ||
var addDefaultRequestMatcherHeaders = function (pathOrRequestMatcher) { | ||
var requestMatcher; | ||
if (typeof pathOrRequestMatcher === "string") { | ||
requestMatcher = { | ||
path: pathOrRequestMatcher | ||
return { | ||
requestCallback: requestCallback, | ||
clientIdCallback: clientIdCallback | ||
}; | ||
}); | ||
/** | ||
* Setup an expectation in the MockServer by specifying an expectation object | ||
* for example: | ||
* | ||
* mockServerClient("localhost", 1080).mockAnyResponse( | ||
* { | ||
* 'httpRequest': { | ||
* 'path': '/somePath', | ||
* 'body': { | ||
* 'type': "STRING", | ||
* 'value': 'someBody' | ||
* } | ||
* }, | ||
* 'httpResponse': { | ||
* 'statusCode': 200, | ||
* 'body': Base64.encode(JSON.stringify({ name: 'first_body' })), | ||
* 'delay': { | ||
* 'timeUnit': 'MILLISECONDS', | ||
* 'value': 250 | ||
* } | ||
* }, | ||
* 'times': { | ||
* 'remainingTimes': 1, | ||
* 'unlimited': false | ||
* } | ||
* } | ||
* ); | ||
* | ||
* @param expectation the expectation to setup on the MockServer | ||
*/ | ||
var mockAnyResponse = function (expectation) { | ||
return makeRequest(host, port, "/expectation", addDefaultExpectationHeaders(expectation)); | ||
}; | ||
} else if (typeof pathOrRequestMatcher === "object") { | ||
requestMatcher = pathOrRequestMatcher; | ||
} else { | ||
requestMatcher = {}; | ||
} | ||
if (defaultRequestHeaders.length) { | ||
if (requestMatcher.httpRequest) { | ||
requestMatcher.httpRequest.headers = arrayUniqueConcatenate(requestMatcher.httpRequest.headers, defaultRequestHeaders); | ||
} else { | ||
requestMatcher.headers = arrayUniqueConcatenate(requestMatcher.headers, defaultRequestHeaders); | ||
} | ||
} | ||
return requestMatcher; | ||
}; | ||
var addDefaultResponseMatcherHeaders = function (response) { | ||
var responseMatcher; | ||
if (typeof response === "object") { | ||
responseMatcher = response; | ||
} else { | ||
responseMatcher = {}; | ||
} | ||
if (defaultResponseHeaders.length) { | ||
if (responseMatcher.httpResponse) { | ||
responseMatcher.httpResponse.headers = arrayUniqueConcatenate(responseMatcher.httpResponse.headers, defaultResponseHeaders); | ||
} else { | ||
responseMatcher.headers = arrayUniqueConcatenate(responseMatcher.headers, defaultResponseHeaders); | ||
} | ||
} | ||
return responseMatcher; | ||
}; | ||
var addDefaultExpectationHeaders = function (expectation) { | ||
if (Array.isArray(expectation)) { | ||
for (var i = 0; i < expectation.length; i++) { | ||
expectation[i].httpRequest = addDefaultRequestMatcherHeaders(expectation[i].httpRequest); | ||
expectation[i].httpResponse = addDefaultResponseMatcherHeaders(expectation[i].httpResponse); | ||
} | ||
} else { | ||
expectation.httpRequest = addDefaultRequestMatcherHeaders(expectation.httpRequest); | ||
expectation.httpResponse = addDefaultResponseMatcherHeaders(expectation.httpResponse); | ||
} | ||
return expectation; | ||
}; | ||
/** | ||
* Verify a request has been sent for example: | ||
* | ||
* expect(client.verify({ | ||
* 'httpRequest': { | ||
* 'method': 'POST', | ||
* 'path': '/somePath' | ||
* } | ||
* })).toBeTruthy(); | ||
* | ||
* @param request the http request that must be matched for this verification to pass | ||
* @param count the number of times this request must be matched | ||
* @param exact true if the count is matched as "equal to" or false if the count is matched as "greater than or equal to" | ||
*/ | ||
var verify = function (request, count, exact) { | ||
if (count === undefined) { | ||
count = 1; | ||
} | ||
return { | ||
then: function (sucess, error) { | ||
request.headers = arrayUniqueConcatenate(request.headers, defaultRequestHeaders); | ||
return makeRequest(host, port, "/verify", { | ||
"httpRequest": request, | ||
"times": { | ||
"count": count, | ||
"exact": exact | ||
/** | ||
* Setup an expectation in the MockServer by specifying a request matcher, and | ||
* a local request handler function. The request handler function receives each | ||
* request (that matches the request matcher) and returns the response that the | ||
* MockServer will return for this expectation. | ||
* | ||
* for example: | ||
* | ||
* mockServerClient("localhost", 1080).mockWithCallback( | ||
* { | ||
* path: '/somePath', | ||
* body: 'some_request_body' | ||
* }, | ||
* function (request) { | ||
* var response = { | ||
* statusCode: 200, | ||
* body: 'some_response_body' | ||
* }; | ||
* return response | ||
* } | ||
* ).then( | ||
* function () { | ||
* alert('expectation sent'); | ||
* }, | ||
* function (error) { | ||
* alert('error'); | ||
* } | ||
* ); | ||
* | ||
* @param requestMatcher the request matcher for the expectation | ||
* @param requestHandler the function to be called back when the request is matched | ||
*/ | ||
var mockWithCallback = function (requestMatcher, requestHandler, times) { | ||
return { | ||
then: function (sucess, error) { | ||
try { | ||
var webSocketClient = WebSocketClient(host, port, cleanedContextPath); | ||
webSocketClient.requestCallback(function (request) { | ||
return { | ||
type: "org.mockserver.model.HttpResponse", | ||
value: JSON.stringify(requestHandler(request)) | ||
}; | ||
}); | ||
webSocketClient.clientIdCallback(function (clientId) { | ||
return makeRequest(host, port, "/expectation", createExpectationWithCallback(requestMatcher, clientId, times)).then(sucess, error) | ||
}); | ||
} catch (e) { | ||
error && error(e); | ||
} | ||
} | ||
}; | ||
}; | ||
/** | ||
* Setup an expectation in the MockServer without having to specify the full expectation object | ||
* for example: | ||
* | ||
* mockServerClient("localhost", 1080).mockSimpleResponse('/somePath', { name: 'value' }, 203); | ||
* | ||
* @param path the path to match requests against | ||
* @param responseBody the response body to return if a request matches | ||
* @param statusCode the response code to return if a request matches | ||
*/ | ||
var mockSimpleResponse = function (path, responseBody, statusCode) { | ||
return mockAnyResponse(createExpectation(path, responseBody, statusCode)); | ||
}; | ||
/** | ||
* Override: | ||
* | ||
* - default headers that are used to specify the response headers in mockSimpleResponse(...) | ||
* (note: if you use mockAnyResponse(...) the default headers are not used) | ||
* | ||
* - headers added to every request matcher, this is particularly useful for running tests in parallel | ||
* | ||
* for example: | ||
* | ||
* mockServerClient("localhost", 1080).setDefaultHeaders([ | ||
* {"name": "Content-Type", "values": ["application/json; charset=utf-8"]}, | ||
* {"name": "Cache-Control", "values": ["no-cache, no-store"]} | ||
* ],[ | ||
* {"name": "sessionId", "values": ["786fcf9b-606e-605f-181d-c245b55e5eac"]} | ||
* ]) | ||
* | ||
* @param responseHeaders the default headers to be added to every response | ||
* @param requestHeaders the default headers to be added to every request matcher | ||
*/ | ||
var setDefaultHeaders = function (responseHeaders, requestHeaders) { | ||
if (responseHeaders) { | ||
defaultResponseHeaders = responseHeaders; | ||
} | ||
}).then( | ||
function () { | ||
sucess && sucess(); | ||
}, | ||
function (result) { | ||
if (result.statusCode !== 202) { | ||
error && error(result.body); | ||
} else { | ||
error && sucess(result); | ||
} | ||
if (requestHeaders) { | ||
defaultRequestHeaders = requestHeaders; | ||
} | ||
); | ||
} | ||
}; | ||
}; | ||
/** | ||
* Verify a sequence of requests has been sent for example: | ||
* | ||
* client.verifySequence( | ||
* { | ||
* 'method': 'POST', | ||
* 'path': '/first_request' | ||
* }, | ||
* { | ||
* 'method': 'POST', | ||
* 'path': '/second_request' | ||
* }, | ||
* { | ||
* 'method': 'POST', | ||
* 'path': '/third_request' | ||
* } | ||
* ); | ||
* | ||
* @param arguments the list of http requests that must be matched for this verification to pass | ||
*/ | ||
var verifySequence = function () { | ||
var requestSequence = []; | ||
for (var i = 0; i < arguments.length; i++) { | ||
var requestMatcher = arguments[i]; | ||
requestMatcher.headers = arrayUniqueConcatenate(requestMatcher.headers, defaultRequestHeaders); | ||
requestSequence.push(requestMatcher); | ||
} | ||
return { | ||
then: function (sucess, error) { | ||
return makeRequest(host, port, "/verifySequence", { | ||
"httpRequests": requestSequence | ||
}).then( | ||
function () { | ||
sucess && sucess(); | ||
}, | ||
function (result) { | ||
if (result.statusCode !== 202) { | ||
error && error(result.body); | ||
} else { | ||
error && sucess(result); | ||
} | ||
return _this; | ||
}; | ||
var addDefaultRequestMatcherHeaders = function (pathOrRequestMatcher) { | ||
var requestMatcher; | ||
if (typeof pathOrRequestMatcher === "string") { | ||
requestMatcher = { | ||
path: pathOrRequestMatcher | ||
}; | ||
} else if (typeof pathOrRequestMatcher === "object") { | ||
requestMatcher = pathOrRequestMatcher; | ||
} else { | ||
requestMatcher = {}; | ||
} | ||
); | ||
} | ||
}; | ||
}; | ||
/** | ||
* Reset MockServer by clearing all expectations | ||
*/ | ||
var reset = function () { | ||
return makeRequest(host, port, "/reset"); | ||
}; | ||
/** | ||
* Clear all expectations that match the specified path | ||
* | ||
* @param pathOrRequestMatcher if a string is passed in the value will be treated as the path to | ||
* decide which expectations to cleared, however if an object is passed | ||
* in the value will be treated as a full request matcher object | ||
* @param type the type to clear 'EXPECTATIONS', 'LOG' or 'ALL', defaults to 'ALL' if not specified | ||
*/ | ||
var clear = function (pathOrRequestMatcher, type) { | ||
if (type) { | ||
var typeEnum = ['EXPECTATIONS', 'LOG', 'ALL']; | ||
if (typeEnum.indexOf(type) === -1) { | ||
throw new Error("\"" + (type || "undefined") + "\" is not a supported value for \"type\" parameter only " + typeEnum + " are allowed values"); | ||
} | ||
} | ||
return makeRequest(host, port, "/clear" + (type ? "?type=" + type : ""), addDefaultRequestMatcherHeaders(pathOrRequestMatcher)); | ||
}; | ||
if (defaultRequestHeaders.length) { | ||
if (requestMatcher.httpRequest) { | ||
requestMatcher.httpRequest.headers = arrayUniqueConcatenate(requestMatcher.httpRequest.headers, defaultRequestHeaders); | ||
} else { | ||
requestMatcher.headers = arrayUniqueConcatenate(requestMatcher.headers, defaultRequestHeaders); | ||
} | ||
} | ||
return requestMatcher; | ||
}; | ||
var addDefaultResponseMatcherHeaders = function (response) { | ||
var responseMatcher; | ||
if (typeof response === "object") { | ||
responseMatcher = response; | ||
} else { | ||
responseMatcher = {}; | ||
} | ||
if (defaultResponseHeaders.length) { | ||
if (responseMatcher.httpResponse) { | ||
responseMatcher.httpResponse.headers = arrayUniqueConcatenate(responseMatcher.httpResponse.headers, defaultResponseHeaders); | ||
} else { | ||
responseMatcher.headers = arrayUniqueConcatenate(responseMatcher.headers, defaultResponseHeaders); | ||
} | ||
} | ||
return responseMatcher; | ||
}; | ||
var addDefaultExpectationHeaders = function (expectation) { | ||
if (Array.isArray(expectation)) { | ||
for (var i = 0; i < expectation.length; i++) { | ||
expectation[i].httpRequest = addDefaultRequestMatcherHeaders(expectation[i].httpRequest); | ||
expectation[i].httpResponse = addDefaultResponseMatcherHeaders(expectation[i].httpResponse); | ||
} | ||
} else { | ||
expectation.httpRequest = addDefaultRequestMatcherHeaders(expectation.httpRequest); | ||
expectation.httpResponse = addDefaultResponseMatcherHeaders(expectation.httpResponse); | ||
} | ||
return expectation; | ||
}; | ||
/** | ||
* Verify a request has been sent for example: | ||
* | ||
* expect(client.verify({ | ||
* 'httpRequest': { | ||
* 'method': 'POST', | ||
* 'path': '/somePath' | ||
* } | ||
* })).toBeTruthy(); | ||
* | ||
* @param request the http request that must be matched for this verification to pass | ||
* @param count the number of times this request must be matched | ||
* @param exact true if the count is matched as "equal to" or false if the count is matched as "greater than or equal to" | ||
*/ | ||
var verify = function (request, count, exact) { | ||
if (count === undefined) { | ||
count = 1; | ||
} | ||
return { | ||
then: function (sucess, error) { | ||
request.headers = arrayUniqueConcatenate(request.headers, defaultRequestHeaders); | ||
return makeRequest(host, port, "/verify", { | ||
"httpRequest": request, | ||
"times": { | ||
"count": count, | ||
"exact": exact | ||
} | ||
}).then( | ||
function () { | ||
sucess && sucess(); | ||
}, | ||
function (result) { | ||
if (!result.statusCode || result.statusCode !== 202) { | ||
error && error(result); | ||
} else { | ||
error && sucess(result); | ||
} | ||
} | ||
); | ||
} | ||
}; | ||
}; | ||
/** | ||
* Verify a sequence of requests has been sent for example: | ||
* | ||
* client.verifySequence( | ||
* { | ||
* 'method': 'POST', | ||
* 'path': '/first_request' | ||
* }, | ||
* { | ||
* 'method': 'POST', | ||
* 'path': '/second_request' | ||
* }, | ||
* { | ||
* 'method': 'POST', | ||
* 'path': '/third_request' | ||
* } | ||
* ); | ||
* | ||
* @param arguments the list of http requests that must be matched for this verification to pass | ||
*/ | ||
var verifySequence = function () { | ||
var requestSequence = []; | ||
for (var i = 0; i < arguments.length; i++) { | ||
var requestMatcher = arguments[i]; | ||
requestMatcher.headers = arrayUniqueConcatenate(requestMatcher.headers, defaultRequestHeaders); | ||
requestSequence.push(requestMatcher); | ||
} | ||
return { | ||
then: function (sucess, error) { | ||
return makeRequest(host, port, "/verifySequence", { | ||
"httpRequests": requestSequence | ||
}).then( | ||
function () { | ||
sucess && sucess(); | ||
}, | ||
function (result) { | ||
if (!result.statusCode || result.statusCode !== 202) { | ||
error && error(result); | ||
} else { | ||
error && sucess(result); | ||
} | ||
} | ||
); | ||
} | ||
}; | ||
}; | ||
/** | ||
* Reset MockServer by clearing all expectations | ||
*/ | ||
var reset = function () { | ||
return makeRequest(host, port, "/reset"); | ||
}; | ||
/** | ||
* Clear all expectations that match the specified path | ||
* | ||
* @param pathOrRequestMatcher if a string is passed in the value will be treated as the path to | ||
* decide which expectations to cleared, however if an object is passed | ||
* in the value will be treated as a full request matcher object | ||
* @param type the type to clear 'EXPECTATIONS', 'LOG' or 'ALL', defaults to 'ALL' if not specified | ||
*/ | ||
var clear = function (pathOrRequestMatcher, type) { | ||
if (type) { | ||
var typeEnum = ['EXPECTATIONS', 'LOG', 'ALL']; | ||
if (typeEnum.indexOf(type) === -1) { | ||
throw new Error("\"" + (type || "undefined") + "\" is not a supported value for \"type\" parameter only " + typeEnum + " are allowed values"); | ||
} | ||
} | ||
return makeRequest(host, port, "/clear" + (type ? "?type=" + type : ""), addDefaultRequestMatcherHeaders(pathOrRequestMatcher)); | ||
}; | ||
/** | ||
* Retrieve the recorded requests that match the parameter, as follows: | ||
* - use a string value to match on path, | ||
* - use a request matcher object to match on a full request, | ||
* - or use null to retrieve all requests | ||
* | ||
* @param pathOrRequestMatcher if a string is passed in the value will be treated as the path, however | ||
* if an object is passed in the value will be treated as a full request | ||
* matcher object, if null is passed in it will be treated as match all | ||
*/ | ||
var retrieveRequests = function (pathOrRequestMatcher) { | ||
return { | ||
then: function (sucess, error) { | ||
makeRequest(host, port, "/retrieve?type=REQUESTS&format=JSON", addDefaultRequestMatcherHeaders(pathOrRequestMatcher)) | ||
.then(function (result) { | ||
sucess(result.body && JSON.parse(result.body)); | ||
}); | ||
} | ||
}; | ||
}; | ||
/** | ||
* Retrieve the setup expectations that match the parameter, | ||
* the expectation is retrieved by matching the parameter | ||
* on the expectations own request matcher, as follows: | ||
* - use a string value to match on path, | ||
* - use a request matcher object to match on a full request, | ||
* - or use null to retrieve all requests | ||
* | ||
* @param pathOrRequestMatcher if a string is passed in the value will be treated as the path, however | ||
* if an object is passed in the value will be treated as a full request | ||
* matcher object, if null is passed in it will be treated as match all | ||
*/ | ||
var retrieveExpectations = function (pathOrRequestMatcher) { | ||
return { | ||
then: function (sucess, error) { | ||
return makeRequest(host, port, "/retrieve?type=ACTIVE_EXPECTATIONS&format=JSON", addDefaultRequestMatcherHeaders(pathOrRequestMatcher)) | ||
.then(function (result) { | ||
sucess(result.body && JSON.parse(result.body)); | ||
}); | ||
} | ||
}; | ||
}; | ||
/** | ||
* Retrieve the recorded requests that match the parameter, as follows: | ||
* - use a string value to match on path, | ||
* - use a request matcher object to match on a full request, | ||
* - or use null to retrieve all requests | ||
* | ||
* @param pathOrRequestMatcher if a string is passed in the value will be treated as the path, however | ||
* if an object is passed in the value will be treated as a full request | ||
* matcher object, if null is passed in it will be treated as match all | ||
*/ | ||
var retrieveRecordedRequests = function (pathOrRequestMatcher) { | ||
return { | ||
then: function (sucess, error) { | ||
makeRequest(host, port, "/retrieve?type=REQUESTS&format=JSON", addDefaultRequestMatcherHeaders(pathOrRequestMatcher)) | ||
.then(function (result) { | ||
sucess(result.body && JSON.parse(result.body)); | ||
}); | ||
} | ||
}; | ||
}; | ||
/** | ||
* Retrieve the active expectations that match the parameter, | ||
* the expectations are retrieved by matching the parameter | ||
* on the expectations own request matcher, as follows: | ||
* - use a string value to match on path, | ||
* - use a request matcher object to match on a full request, | ||
* - or use null to retrieve all requests | ||
* | ||
* @param pathOrRequestMatcher if a string is passed in the value will be treated as the path, however | ||
* if an object is passed in the value will be treated as a full request | ||
* matcher object, if null is passed in it will be treated as match all | ||
*/ | ||
var retrieveActiveExpectations = function (pathOrRequestMatcher) { | ||
return { | ||
then: function (sucess, error) { | ||
return makeRequest(host, port, "/retrieve?type=ACTIVE_EXPECTATIONS&format=JSON", addDefaultRequestMatcherHeaders(pathOrRequestMatcher)) | ||
.then(function (result) { | ||
sucess(result.body && JSON.parse(result.body)); | ||
}); | ||
} | ||
}; | ||
}; | ||
/** | ||
* Retrieve the request-response pairs as expectations that match the | ||
* parameter, expectations are retrieved by matching the parameter | ||
* on the expectations own request matcher, as follows: | ||
* - use a string value to match on path, | ||
* - use a request matcher object to match on a full request, | ||
* - or use null to retrieve all requests | ||
* | ||
* @param pathOrRequestMatcher if a string is passed in the value will be treated as the path, however | ||
* if an object is passed in the value will be treated as a full request | ||
* matcher object, if null is passed in it will be treated as match all | ||
*/ | ||
var retrieveRecordedExpectations = function (pathOrRequestMatcher) { | ||
return { | ||
then: function (sucess, error) { | ||
return makeRequest(host, port, "/retrieve?type=RECORDED_EXPECTATIONS&format=JSON", addDefaultRequestMatcherHeaders(pathOrRequestMatcher)) | ||
.then(function (result) { | ||
sucess(result.body && JSON.parse(result.body)); | ||
}); | ||
} | ||
}; | ||
}; | ||
var _this = { | ||
mockAnyResponse: mockAnyResponse, | ||
mockWithCallback: mockWithCallback, | ||
mockSimpleResponse: mockSimpleResponse, | ||
setDefaultHeaders: setDefaultHeaders, | ||
verify: verify, | ||
verifySequence: verifySequence, | ||
reset: reset, | ||
clear: clear, | ||
retrieveRequests: retrieveRequests, | ||
retrieveExpectations: retrieveExpectations | ||
var _this = { | ||
mockAnyResponse: mockAnyResponse, | ||
mockWithCallback: mockWithCallback, | ||
mockSimpleResponse: mockSimpleResponse, | ||
setDefaultHeaders: setDefaultHeaders, | ||
verify: verify, | ||
verifySequence: verifySequence, | ||
reset: reset, | ||
clear: clear, | ||
retrieveRecordedRequests: retrieveRecordedRequests, | ||
retrieveActiveExpectations: retrieveActiveExpectations, | ||
retrieveRecordedExpectations: retrieveRecordedExpectations | ||
}; | ||
return _this; | ||
}; | ||
return _this; | ||
}; | ||
if (typeof module !== 'undefined') { | ||
module.exports = { | ||
mockServerClient: mockServerClient | ||
}; | ||
} | ||
if (typeof module !== 'undefined') { | ||
module.exports = { | ||
mockServerClient: mockServerClient | ||
}; | ||
} | ||
})(); |
{ | ||
"name": "mockserver-client", | ||
"description": "A node client for the MockServer", | ||
"version": "5.2.1", | ||
"version": "5.2.2", | ||
"homepage": "https://github.com/jamesdbloom/mockserver", | ||
@@ -40,3 +40,3 @@ "author": { | ||
"karma-xvfb-chrome-launcher": "0.0.1", | ||
"mockserver-node": "5.2.1", | ||
"mockserver-node": "5.2.2", | ||
"nodeunit": "~0.11" | ||
@@ -43,0 +43,0 @@ }, |
@@ -11,67 +11,68 @@ /* | ||
(function () { | ||
"use strict"; | ||
"use strict"; | ||
var makeRequest = (typeof require !== 'undefined' ? require('./sendRequest').sendRequest : function (host, port, path, jsonBody, resolveCallback) { | ||
var body = (typeof jsonBody === "string" ? jsonBody : JSON.stringify(jsonBody || "")); | ||
var url = 'http://' + host + ':' + port + path; | ||
var makeRequest = (typeof require !== 'undefined' ? require('./sendRequest').sendRequest : function (host, port, path, jsonBody, resolveCallback) { | ||
var body = (typeof jsonBody === "string" ? jsonBody : JSON.stringify(jsonBody || "")); | ||
var url = 'http://' + host + ':' + port + path; | ||
return { | ||
then: function (sucess, error) { | ||
try { | ||
var xmlhttp = new XMLHttpRequest(); | ||
xmlhttp.addEventListener("load", (function (sucess, error) { | ||
return function () { | ||
if (error && this.status >= 400 && this.status < 600) { | ||
error({ | ||
statusCode: this.status, | ||
body: this.responseText | ||
}); | ||
} else { | ||
sucess && sucess({ | ||
statusCode: this.status, | ||
body: this.responseText | ||
}); | ||
} | ||
}; | ||
})(sucess, error)); | ||
xmlhttp.open('PUT', url); | ||
xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8"); | ||
xmlhttp.send(body); | ||
} catch (e) { | ||
error && error(e); | ||
} | ||
} | ||
}; | ||
}); | ||
return { | ||
then: function (sucess, error) { | ||
try { | ||
var xmlhttp = new XMLHttpRequest(); | ||
xmlhttp.addEventListener("load", (function (sucess, error) { | ||
return function () { | ||
if (error && this.status >= 400 && this.status < 600) { | ||
if (this.statusCode === 404) { | ||
error("404 Not Found"); | ||
} else { | ||
error(this.responseText); | ||
} | ||
} else { | ||
sucess && sucess({ | ||
statusCode: this.status, | ||
body: this.responseText | ||
}); | ||
} | ||
}; | ||
})(sucess, error)); | ||
xmlhttp.open('PUT', url); | ||
xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8"); | ||
xmlhttp.send(body); | ||
} catch (e) { | ||
error && error(e); | ||
} | ||
} | ||
}; | ||
}); | ||
/** | ||
* Start the client communicating to a MockServer proxy at the specified host and port | ||
* for example: | ||
* | ||
* var client = proxyClient("localhost", 1080); | ||
* | ||
* @param host the host for the proxy to communicate with | ||
* @param port the port for the proxy to communicate with | ||
*/ | ||
proxyClient = function (host, port) { | ||
/** | ||
* Start the client communicating to a MockServer proxy at the specified host and port | ||
* for example: | ||
* | ||
* var client = proxyClient("localhost", 1080); | ||
* | ||
* @param host the host for the proxy to communicate with | ||
* @param port the port for the proxy to communicate with | ||
*/ | ||
proxyClient = function (host, port) { | ||
var addDefaultRequestMatcherHeaders = function (pathOrRequestMatcher) { | ||
var responseMatcher; | ||
if (typeof pathOrRequestMatcher === "string") { | ||
responseMatcher = { | ||
path: pathOrRequestMatcher | ||
var addDefaultRequestMatcherHeaders = function (pathOrRequestMatcher) { | ||
var responseMatcher; | ||
if (typeof pathOrRequestMatcher === "string") { | ||
responseMatcher = { | ||
path: pathOrRequestMatcher | ||
}; | ||
} else if (typeof pathOrRequestMatcher === "object") { | ||
responseMatcher = pathOrRequestMatcher; | ||
} else { | ||
responseMatcher = { | ||
path: ".*" | ||
}; | ||
} | ||
return responseMatcher; | ||
}; | ||
} else if (typeof pathOrRequestMatcher === "object") { | ||
responseMatcher = pathOrRequestMatcher; | ||
} else { | ||
responseMatcher = { | ||
path: ".*" | ||
}; | ||
} | ||
return responseMatcher; | ||
}; | ||
/** | ||
* Verify a request has been sent for example: | ||
* | ||
* expect(client.verify({ | ||
/** | ||
* Verify a request has been sent for example: | ||
* | ||
* expect(client.verify({ | ||
* 'httpRequest': { | ||
@@ -82,138 +83,161 @@ * 'method': 'POST', | ||
* })).toBeTruthy(); | ||
* | ||
* @param request the http request that must be matched for this verification to pass | ||
* @param count the number of times this request must be matched | ||
* @param exact true if the count is matched as "equal to" or false if the count is matched as "greater than or equal to" | ||
*/ | ||
var verify = function (request, count, exact) { | ||
if (count === undefined) { | ||
count = 1; | ||
} | ||
return { | ||
then: function (sucess, error) { | ||
return makeRequest(host, port, "/verify", { | ||
"httpRequest": request, | ||
"times": { | ||
"count": count, | ||
"exact": exact | ||
* | ||
* @param request the http request that must be matched for this verification to pass | ||
* @param count the number of times this request must be matched | ||
* @param exact true if the count is matched as "equal to" or false if the count is matched as "greater than or equal to" | ||
*/ | ||
var verify = function (request, count, exact) { | ||
if (count === undefined) { | ||
count = 1; | ||
} | ||
}).then( | ||
function () { | ||
sucess && sucess(); | ||
}, | ||
function (result) { | ||
if (result.statusCode !== 202) { | ||
error && error(result.body); | ||
} else { | ||
error && sucess(result); | ||
} | ||
} | ||
); | ||
} | ||
}; | ||
}; | ||
/** | ||
* Verify a sequence of requests has been sent for example: | ||
* | ||
* client.verifySequence( | ||
* { | ||
return { | ||
then: function (sucess, error) { | ||
return makeRequest(host, port, "/verify", { | ||
"httpRequest": request, | ||
"times": { | ||
"count": count, | ||
"exact": exact | ||
} | ||
}).then( | ||
function () { | ||
sucess && sucess(); | ||
}, | ||
function (result) { | ||
if (!result.statusCode || result.statusCode !== 202) { | ||
error && error(result); | ||
} else { | ||
error && sucess(result); | ||
} | ||
} | ||
); | ||
} | ||
}; | ||
}; | ||
/** | ||
* Verify a sequence of requests has been sent for example: | ||
* | ||
* client.verifySequence( | ||
* { | ||
* 'method': 'POST', | ||
* 'path': '/first_request' | ||
* }, | ||
* { | ||
* { | ||
* 'method': 'POST', | ||
* 'path': '/second_request' | ||
* }, | ||
* { | ||
* { | ||
* 'method': 'POST', | ||
* 'path': '/third_request' | ||
* } | ||
* ); | ||
* | ||
* @param arguments the list of http requests that must be matched for this verification to pass | ||
*/ | ||
var verifySequence = function () { | ||
var requestSequence = []; | ||
for (var i = 0; i < arguments.length; i++) { | ||
requestSequence.push(arguments[i]); | ||
} | ||
return { | ||
then: function (sucess, error) { | ||
return makeRequest(host, port, "/verifySequence", { | ||
"httpRequests": requestSequence | ||
}).then( | ||
function () { | ||
sucess && sucess(); | ||
}, | ||
function (result) { | ||
if (result.statusCode !== 202) { | ||
error && error(result.body); | ||
} else { | ||
error && sucess(result); | ||
} | ||
* ); | ||
* | ||
* @param arguments the list of http requests that must be matched for this verification to pass | ||
*/ | ||
var verifySequence = function () { | ||
var requestSequence = []; | ||
for (var i = 0; i < arguments.length; i++) { | ||
requestSequence.push(arguments[i]); | ||
} | ||
); | ||
} | ||
}; | ||
}; | ||
/** | ||
* Reset the proxy by clearing all recorded requests | ||
*/ | ||
var reset = function () { | ||
return makeRequest(host, port, "/reset"); | ||
}; | ||
/** | ||
* Clear all recorded requests that match the specified path | ||
* | ||
* @param pathOrRequestMatcher if a string is passed in the value will be treated as the path to | ||
* decide which recorded requests to cleared, however if an object is | ||
* passed in the value will be treated as a full request matcher object | ||
* @param type the type to clear 'EXPECTATIONS', 'LOG' or 'ALL', defaults to 'ALL' if not specified | ||
*/ | ||
var clear = function (pathOrRequestMatcher, type) { | ||
if (type) { | ||
var typeEnum = ['EXPECTATIONS', 'LOG', 'ALL']; | ||
if (typeEnum.indexOf(type) === -1) { | ||
throw new Error("\"" + (type || "undefined") + "\" is not a supported value for \"type\" parameter only " + typeEnum + " are allowed values"); | ||
} | ||
} | ||
return makeRequest(host, port, "/clear" + (type ? "?type=" + type : ""), addDefaultRequestMatcherHeaders(pathOrRequestMatcher)); | ||
}; | ||
return { | ||
then: function (sucess, error) { | ||
return makeRequest(host, port, "/verifySequence", { | ||
"httpRequests": requestSequence | ||
}).then( | ||
function () { | ||
sucess && sucess(); | ||
}, | ||
function (result) { | ||
if (!result.statusCode || result.statusCode !== 202) { | ||
error && error(result); | ||
} else { | ||
error && sucess(result); | ||
} | ||
} | ||
); | ||
} | ||
}; | ||
}; | ||
/** | ||
* Reset the proxy by clearing all recorded requests | ||
*/ | ||
var reset = function () { | ||
return makeRequest(host, port, "/reset"); | ||
}; | ||
/** | ||
* Clear all recorded requests that match the specified path | ||
* | ||
* @param pathOrRequestMatcher if a string is passed in the value will be treated as the path to | ||
* decide which recorded requests to cleared, however if an object is | ||
* passed in the value will be treated as a full request matcher object | ||
* @param type the type to clear 'EXPECTATIONS', 'LOG' or 'ALL', defaults to 'ALL' if not specified | ||
*/ | ||
var clear = function (pathOrRequestMatcher, type) { | ||
if (type) { | ||
var typeEnum = ['EXPECTATIONS', 'LOG', 'ALL']; | ||
if (typeEnum.indexOf(type) === -1) { | ||
throw new Error("\"" + (type || "undefined") + "\" is not a supported value for \"type\" parameter only " + typeEnum + " are allowed values"); | ||
} | ||
} | ||
return makeRequest(host, port, "/clear" + (type ? "?type=" + type : ""), addDefaultRequestMatcherHeaders(pathOrRequestMatcher)); | ||
}; | ||
/** | ||
* Retrieve the recorded requests that match the parameter, as follows: | ||
* - use a string value to match on path, | ||
* - use a request matcher object to match on a full request, | ||
* - or use null to retrieve all requests | ||
* | ||
* @param pathOrRequestMatcher if a string is passed in the value will be treated as the path, however | ||
* if an object is passed in the value will be treated as a full request | ||
* matcher object, if null is passed in it will be treated as match all | ||
*/ | ||
var retrieveRequests = function (pathOrRequestMatcher) { | ||
return { | ||
then: function (sucess, error) { | ||
makeRequest(host, port, "/retrieve?type=REQUESTS&format=JSON", addDefaultRequestMatcherHeaders(pathOrRequestMatcher)) | ||
.then(function (result) { | ||
sucess(result.body && JSON.parse(result.body)); | ||
}); | ||
} | ||
}; | ||
}; | ||
/** | ||
* Retrieve the recorded requests that match the parameter, as follows: | ||
* - use a string value to match on path, | ||
* - use a request matcher object to match on a full request, | ||
* - or use null to retrieve all requests | ||
* | ||
* @param pathOrRequestMatcher if a string is passed in the value will be treated as the path, however | ||
* if an object is passed in the value will be treated as a full request | ||
* matcher object, if null is passed in it will be treated as match all | ||
*/ | ||
var retrieveRecordedRequests = function (pathOrRequestMatcher) { | ||
return { | ||
then: function (sucess, error) { | ||
makeRequest(host, port, "/retrieve?type=REQUESTS&format=JSON", addDefaultRequestMatcherHeaders(pathOrRequestMatcher)) | ||
.then(function (result) { | ||
sucess(result.body && JSON.parse(result.body)); | ||
}); | ||
} | ||
}; | ||
}; | ||
/** | ||
* Retrieve the request-response pairs as expectations that match the | ||
* parameter, expectations are retrieved by matching the parameter | ||
* on the expectations own request matcher, as follows: | ||
* - use a string value to match on path, | ||
* - use a request matcher object to match on a full request, | ||
* - or use null to retrieve all requests | ||
* | ||
* @param pathOrRequestMatcher if a string is passed in the value will be treated as the path, however | ||
* if an object is passed in the value will be treated as a full request | ||
* matcher object, if null is passed in it will be treated as match all | ||
*/ | ||
var retrieveRecordedExpectations = function (pathOrRequestMatcher) { | ||
return { | ||
then: function (sucess, error) { | ||
return makeRequest(host, port, "/retrieve?type=RECORDED_EXPECTATIONS&format=JSON", addDefaultRequestMatcherHeaders(pathOrRequestMatcher)) | ||
.then(function (result) { | ||
sucess(result.body && JSON.parse(result.body)); | ||
}); | ||
} | ||
}; | ||
}; | ||
var _this = { | ||
verify: verify, | ||
verifySequence: verifySequence, | ||
reset: reset, | ||
clear: clear, | ||
retrieveRequests: retrieveRequests | ||
var _this = { | ||
verify: verify, | ||
verifySequence: verifySequence, | ||
reset: reset, | ||
clear: clear, | ||
retrieveRecordedRequests: retrieveRecordedRequests, | ||
retrieveRecordedExpectations: retrieveRecordedExpectations | ||
}; | ||
return _this; | ||
}; | ||
return _this; | ||
}; | ||
if (typeof module !== 'undefined') { | ||
module.exports = { | ||
proxyClient: proxyClient | ||
}; | ||
} | ||
if (typeof module !== 'undefined') { | ||
module.exports = { | ||
proxyClient: proxyClient | ||
}; | ||
} | ||
})(); |
@@ -176,2 +176,3 @@ # mockserver-client-node | ||
2017-12-11 | v5.2.1 | Improved error output + upgrade to 5.2.1 | ||
2017-12-12 | v5.2.2 | Fixed incorrect error format 5.2.2 | ||
@@ -178,0 +179,0 @@ --- |
@@ -15,3 +15,3 @@ /* | ||
var defer = function() { | ||
var defer = function () { | ||
var promise = (global.protractor ? protractor.promise : Q); | ||
@@ -55,3 +55,7 @@ var deferred = promise.defer(); | ||
if (response.statusCode >= 400 && response.statusCode < 600) { | ||
deferred.reject(data); | ||
if (response.statusCode === 404) { | ||
deferred.reject("404 Not Found"); | ||
} else { | ||
deferred.reject(data); | ||
} | ||
} else { | ||
@@ -58,0 +62,0 @@ deferred.resolve({ |
@@ -491,3 +491,3 @@ if ((typeof __karma__ !== 'undefined' ? __karma__.config.mode === 'with_proxy' : window.location.href.indexOf('proxy=true') !== -1)) { | ||
// when | ||
var requests = client.retrieveRequests({ | ||
var requests = client.retrieveRecordedRequests({ | ||
"httpRequest": { | ||
@@ -558,3 +558,3 @@ "path": "/somePathOne" | ||
// when | ||
var requests = proxyClient("localhost", proxyPort).retrieveRequests("/somePathOne") | ||
var requests = proxyClient("localhost", proxyPort).retrieveRecordedRequests("/somePathOne") | ||
.then(function (requests) { | ||
@@ -561,0 +561,0 @@ |
@@ -29,6 +29,6 @@ (function () { | ||
var callback = function (response) { | ||
var body = ''; | ||
var data = ''; | ||
response.on('data', function (chunk) { | ||
body += chunk; | ||
data += chunk; | ||
}); | ||
@@ -38,7 +38,7 @@ | ||
if (response.statusCode >= 400 && response.statusCode < 600) { | ||
deferred.reject({ | ||
statusCode: response.statusCode, | ||
headers: response.headers, | ||
body: body | ||
}); | ||
if (response.statusCode === 404) { | ||
deferred.reject("404 Not Found"); | ||
} else { | ||
deferred.reject(data); | ||
} | ||
} else { | ||
@@ -48,3 +48,3 @@ deferred.resolve({ | ||
headers: response.headers, | ||
body: body | ||
body: data | ||
}); | ||
@@ -90,3 +90,3 @@ } | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -96,3 +96,3 @@ // and - another request | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -118,3 +118,3 @@ // and - a verification that passes | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -124,3 +124,3 @@ // and - another request | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -146,3 +146,3 @@ // and - a verification that passes | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -169,3 +169,3 @@ // when - a verification that should fail | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -196,3 +196,3 @@ // when - a verification that should fail | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -222,9 +222,9 @@ // when - a verification that should fail | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
sendRequestViaProxy("http://localhost:" + mockServerPort + "/two", undefined, "GET") | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
sendRequestViaProxy("http://localhost:" + mockServerPort + "/three", undefined, "GET") | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -261,11 +261,11 @@ // when | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
sendRequestViaProxy("http://localhost:" + mockServerPort + "/two", undefined, "GET") | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
sendRequestViaProxy("http://localhost:" + mockServerPort + "/three", undefined, "GET") | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -320,3 +320,3 @@ // when - wrong order | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -326,3 +326,3 @@ // and - another request | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -375,3 +375,3 @@ // and - a verification that passes | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -381,3 +381,3 @@ // and - another request | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -431,3 +431,3 @@ // and - a verification that passes | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -437,3 +437,3 @@ // and - another request | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -489,3 +489,3 @@ // and - a verification that passes | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -495,3 +495,3 @@ // and - another request | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -548,3 +548,3 @@ // and - a verification that passes | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -556,3 +556,3 @@ sendRequestViaProxy("http://localhost:" + mockServerPort + "/somePathTwo", undefined, "GET") | ||
// when | ||
client.retrieveRequests({ | ||
client.retrieveRecordedRequests({ | ||
"httpRequest": { | ||
@@ -604,3 +604,3 @@ "path": "/somePathOne" | ||
.then(fail(test), function (error) { | ||
test.equal(error.statusCode, 404); | ||
test.equal(error, "404 Not Found"); | ||
@@ -612,3 +612,3 @@ sendRequestViaProxy("http://localhost:" + mockServerPort + "/somePathTwo", undefined, "GET") | ||
// when | ||
proxyClient("localhost", proxyPort).retrieveRequests("/somePathOne") | ||
proxyClient("localhost", proxyPort).retrieveRecordedRequests("/somePathOne") | ||
.then(function (requests) { | ||
@@ -615,0 +615,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
498324
9836
183