mockserver-client
Advanced tools
Comparing version 5.6.1 to 5.7.0
@@ -13,36 +13,2 @@ /* | ||
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) { | ||
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); | ||
} | ||
} | ||
}; | ||
}); | ||
/** | ||
@@ -57,5 +23,45 @@ * Start the client communicating at the specified host and port | ||
* @param contextPath the context path if server was deployed as a war | ||
* @param tls enable TLS (i.e. HTTPS) for communication to server | ||
* @param caCertPemFilePath provide custom CA Certificate (defaults to MockServer CA Certificate) | ||
*/ | ||
mockServerClient = function (host, port, contextPath) { | ||
mockServerClient = function (host, port, contextPath, tls, caCertPemFilePath) { | ||
var makeRequest = (typeof require !== 'undefined' ? require('./sendRequest').sendRequest(tls, caCertPemFilePath) : 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) { | ||
if (this.statusCode === 404) { | ||
error("404 Not Found"); | ||
} else { | ||
error(this.responseText); | ||
} | ||
} else { | ||
if (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) { | ||
if (error) { | ||
error(e); | ||
} | ||
} | ||
} | ||
}; | ||
}); | ||
var cleanedContextPath = (function (contextPath) { | ||
@@ -213,6 +219,4 @@ if (contextPath) { | ||
socket.onopen = function (event) { | ||
console.log("foo"); | ||
}; | ||
socket.onclose = function (event) { | ||
console.log("foo"); | ||
}; | ||
@@ -238,3 +242,86 @@ } | ||
/** | ||
* 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: | ||
* | ||
* client.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 | ||
}; | ||
} else if (typeof pathOrRequestMatcher === "object") { | ||
requestMatcher = pathOrRequestMatcher; | ||
} else { | ||
requestMatcher = {}; | ||
} | ||
if (defaultRequestHeaders) { | ||
if (requestMatcher.httpRequest) { | ||
requestMatcher.httpRequest.headers = headersUniqueConcatenate(requestMatcher.httpRequest.headers, defaultRequestHeaders); | ||
} else { | ||
requestMatcher.headers = headersUniqueConcatenate(requestMatcher.headers, defaultRequestHeaders); | ||
} | ||
} | ||
return requestMatcher; | ||
}; | ||
var addDefaultResponseMatcherHeaders = function (response) { | ||
var responseMatcher; | ||
if (typeof response === "object") { | ||
responseMatcher = response; | ||
} else { | ||
responseMatcher = {}; | ||
} | ||
if (defaultResponseHeaders) { | ||
if (responseMatcher.httpResponse) { | ||
responseMatcher.httpResponse.headers = headersUniqueConcatenate(responseMatcher.httpResponse.headers, defaultResponseHeaders); | ||
} else { | ||
responseMatcher.headers = headersUniqueConcatenate(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); | ||
if(!expectation[i].httpResponseTemplate && !expectation[i].httpResponseClassCallback && !expectation[i].httpResponseObjectCallback && !expectation[i].httpForward && !expectation[i].httpForwardTemplate && !expectation[i].httpForwardClassCallback && !expectation[i].httpForwardObjectCallback && !expectation[i].httpOverrideForwardedRequest && !expectation[i].httpError) { | ||
expectation[i].httpResponse = addDefaultResponseMatcherHeaders(expectation[i].httpResponse); | ||
} | ||
} | ||
} else { | ||
expectation.httpRequest = addDefaultRequestMatcherHeaders(expectation.httpRequest); | ||
if(!expectation.httpResponseTemplate && !expectation.httpResponseClassCallback && !expectation.httpResponseObjectCallback && !expectation.httpForward && !expectation.httpForwardTemplate && !expectation.httpForwardClassCallback && !expectation.httpForwardObjectCallback && !expectation.httpOverrideForwardedRequest && !expectation.httpError) { | ||
expectation.httpResponse = addDefaultResponseMatcherHeaders(expectation.httpResponse); | ||
} | ||
} | ||
return expectation; | ||
}; | ||
/** | ||
* Setup an expectation by specifying an expectation object | ||
@@ -308,3 +395,3 @@ * for example: | ||
try { | ||
var webSocketClient = WebSocketClient(host, port, cleanedContextPath); | ||
var webSocketClient = new WebSocketClient(host, port, cleanedContextPath); | ||
webSocketClient.requestCallback(function (request) { | ||
@@ -321,6 +408,8 @@ var response = requestHandler(request); | ||
webSocketClient.clientIdCallback(function (clientId) { | ||
return makeRequest(host, port, "/expectation", createExpectationWithCallback(requestMatcher, clientId, times)).then(sucess, error) | ||
return makeRequest(host, port, "/expectation", createExpectationWithCallback(requestMatcher, clientId, times)).then(sucess, error); | ||
}); | ||
} catch (e) { | ||
error && error(e); | ||
if (error) { | ||
error(e); | ||
} | ||
} | ||
@@ -344,84 +433,2 @@ } | ||
/** | ||
* 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: | ||
* | ||
* client.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 | ||
}; | ||
} else if (typeof pathOrRequestMatcher === "object") { | ||
requestMatcher = pathOrRequestMatcher; | ||
} else { | ||
requestMatcher = {}; | ||
} | ||
if (defaultRequestHeaders) { | ||
if (requestMatcher.httpRequest) { | ||
requestMatcher.httpRequest.headers = headersUniqueConcatenate(requestMatcher.httpRequest.headers, defaultRequestHeaders); | ||
} else { | ||
requestMatcher.headers = headersUniqueConcatenate(requestMatcher.headers, defaultRequestHeaders); | ||
} | ||
} | ||
return requestMatcher; | ||
}; | ||
var addDefaultResponseMatcherHeaders = function (response) { | ||
var responseMatcher; | ||
if (typeof response === "object") { | ||
responseMatcher = response; | ||
} else { | ||
responseMatcher = {}; | ||
} | ||
if (defaultResponseHeaders) { | ||
if (responseMatcher.httpResponse) { | ||
responseMatcher.httpResponse.headers = headersUniqueConcatenate(responseMatcher.httpResponse.headers, defaultResponseHeaders); | ||
} else { | ||
responseMatcher.headers = headersUniqueConcatenate(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); | ||
if(!expectation[i].httpResponseTemplate && !expectation[i].httpResponseClassCallback && !expectation[i].httpResponseObjectCallback && !expectation[i].httpForward && !expectation[i].httpForwardTemplate && !expectation[i].httpForwardClassCallback && !expectation[i].httpForwardObjectCallback && !expectation[i].httpOverrideForwardedRequest && !expectation[i].httpError) { | ||
expectation[i].httpResponse = addDefaultResponseMatcherHeaders(expectation[i].httpResponse); | ||
} | ||
} | ||
} else { | ||
expectation.httpRequest = addDefaultRequestMatcherHeaders(expectation.httpRequest); | ||
if(!expectation.httpResponseTemplate && !expectation.httpResponseClassCallback && !expectation.httpResponseObjectCallback && !expectation.httpForward && !expectation.httpForwardTemplate && !expectation.httpForwardClassCallback && !expectation.httpForwardObjectCallback && !expectation.httpOverrideForwardedRequest && !expectation.httpError) { | ||
expectation.httpResponse = addDefaultResponseMatcherHeaders(expectation.httpResponse); | ||
} | ||
} | ||
return expectation; | ||
}; | ||
/** | ||
* Verify a request has been sent for example: | ||
@@ -455,9 +462,15 @@ * | ||
function () { | ||
sucess && sucess(); | ||
if (sucess) { | ||
sucess(); | ||
} | ||
}, | ||
function (result) { | ||
if (!result.statusCode || result.statusCode !== 202) { | ||
error && error(result); | ||
if (error) { | ||
error(result); | ||
} | ||
} else { | ||
error && sucess(result); | ||
if (error) { | ||
sucess(result); | ||
} | ||
} | ||
@@ -502,9 +515,15 @@ } | ||
function () { | ||
sucess && sucess(); | ||
if (sucess) { | ||
sucess(); | ||
} | ||
}, | ||
function (result) { | ||
if (!result.statusCode || result.statusCode !== 202) { | ||
error && error(result); | ||
if (error) { | ||
error(result); | ||
} | ||
} else { | ||
error && sucess(result); | ||
if (error) { | ||
sucess(result); | ||
} | ||
} | ||
@@ -571,2 +590,22 @@ } | ||
/** | ||
* Retrieve the recorded requests and their responses that match the parameter: | ||
* - 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 retrieveRecordedRequestsAndResponses = function (pathOrRequestMatcher) { | ||
return { | ||
then: function (sucess, error) { | ||
makeRequest(host, port, "/retrieve?type=REQUEST_RESPONSES&format=JSON", addDefaultRequestMatcherHeaders(pathOrRequestMatcher)) | ||
.then(function (result) { | ||
sucess(result.body && JSON.parse(result.body)); | ||
}); | ||
} | ||
}; | ||
}; | ||
/** | ||
* Retrieve the active expectations that match the parameter, | ||
@@ -648,2 +687,3 @@ * the expectations are retrieved by matching the parameter | ||
retrieveRecordedRequests: retrieveRecordedRequests, | ||
retrieveRecordedRequestsAndResponses: retrieveRecordedRequestsAndResponses, | ||
retrieveActiveExpectations: retrieveActiveExpectations, | ||
@@ -650,0 +690,0 @@ retrieveRecordedExpectations: retrieveRecordedExpectations, |
{ | ||
"name": "mockserver-client", | ||
"description": "A node client for the MockServer", | ||
"version": "5.6.1", | ||
"version": "5.7.0", | ||
"homepage": "https://github.com/jamesdbloom/mockserver", | ||
@@ -22,2 +22,11 @@ "author": { | ||
}, | ||
"files": [ | ||
"README.md", | ||
"LICENSE.md", | ||
"index.js", | ||
"mockServerClient.js", | ||
"sendRequest.js", | ||
"webSocketClient.js", | ||
"stop_MockServer.sh" | ||
], | ||
"devDependencies": { | ||
@@ -29,13 +38,11 @@ "grunt": "^1.0.3", | ||
"grunt-exec": "~3.0", | ||
"grunt-jasmine-runner": "~0.6", | ||
"grunt-karma": "~3.0", | ||
"jasmine-core": "~3.3", | ||
"karma": "~3.1", | ||
"karma-chrome-launcher": "~2.2", | ||
"karma-firefox-launcher": "~1.1", | ||
"karma": "^4.4.1", | ||
"karma-chrome-launcher": "~3.1", | ||
"karma-firefox-launcher": "~1.2", | ||
"karma-jasmine": "~2.0", | ||
"karma-safari-launcher": "~1.0", | ||
"karma-spec-reporter": "0.0.32", | ||
"karma-xvfb-chrome-launcher": "0.0.1", | ||
"mockserver-node": "5.6.1", | ||
"mockserver-node": "5.7.0", | ||
"nodeunit": "^0.11.3" | ||
@@ -50,4 +57,4 @@ }, | ||
"q": "~2.0", | ||
"websocket": "^1.0.28" | ||
"websocket": "^1.0.30" | ||
} | ||
} |
@@ -185,2 +185,3 @@ # mockserver-client-node | ||
2019-07-26 | v5.6.1 | Upgrading MockServer to 5.6.1 | ||
2019-07-26 | v5.7.0 | Cleaned code & upgrading to 5.7.0 | ||
@@ -187,0 +188,0 @@ --- |
@@ -14,6 +14,8 @@ /* | ||
var http = require('http'); | ||
var fs = require('fs'); | ||
var glob = require('glob'); | ||
var defer = function () { | ||
var promise = (global.protractor && protractor.promise.USE_PROMISE_MANAGER !== false) | ||
? protractor.promise | ||
var promise = (global.protractor && protractor.promise.USE_PROMISE_MANAGER !== false) | ||
? protractor.promise | ||
: Q; | ||
@@ -28,58 +30,104 @@ var deferred = promise.defer(); | ||
var sendRequest = function (host, port, path, jsonBody, resolveCallback) { | ||
var downloadCACert = function () { | ||
// https://raw.githubusercontent.com/jamesdbloom/mockserver/master/mockserver-core/src/main/resources/org/mockserver/socket/CertificateAuthorityCertificate.pem | ||
var deferred = defer(); | ||
var dest = "CertificateAuthorityCertificate.pem"; | ||
if (glob.sync('**/' + dest).length === 0) { | ||
var options = { | ||
protocol: 'https:', | ||
method: 'GET', | ||
host: "raw.githubusercontent.com", | ||
path: "/jamesdbloom/mockserver/master/mockserver-core/src/main/resources/org/mockserver/socket/CertificateAuthorityCertificate.pem", | ||
port: 443, | ||
}; | ||
var req = require('https').request(options); | ||
var body = (typeof jsonBody === "string" ? jsonBody : JSON.stringify(jsonBody || "")); | ||
var options = { | ||
method: 'PUT', | ||
host: host, | ||
path: path, | ||
port: port, | ||
headers: { | ||
'Content-Type': "application/json; charset=utf-8" | ||
} | ||
}; | ||
req.once('error', function (error) { | ||
console.error('Fetching ' + JSON.stringify(options, null, 2) + ' failed with error ' + error); | ||
}); | ||
var req = http.request(options); | ||
req.once('response', function (res) { | ||
if (res.statusCode < 200 || res.statusCode >= 300) { | ||
console.error('Fetching ' + JSON.stringify(options, null, 2) + ' failed with HTTP status code ' + res.statusCode); | ||
} else { | ||
var writeStream = fs.createWriteStream(dest); | ||
res.pipe(writeStream); | ||
req.once('response', function (response) { | ||
var data = ''; | ||
response.on('data', function (chunk) { | ||
data += chunk; | ||
writeStream.on('error', function (error) { | ||
console.error('Saving ' + dest + ' failed with error ' + error); | ||
}); | ||
writeStream.on('close', function () { | ||
console.log('Saved ' + dest + ' from ' + JSON.stringify(options, null, 2)); | ||
}); | ||
} | ||
}); | ||
response.on('end', function () { | ||
if (resolveCallback) { | ||
deferred.resolve(resolveCallback(data)); | ||
} else { | ||
if (response.statusCode >= 400 && response.statusCode < 600) { | ||
if (response.statusCode === 404) { | ||
deferred.reject("404 Not Found"); | ||
req.end(); | ||
} | ||
return dest; | ||
}; | ||
var sendRequest = function (tls, caCertPath) { | ||
var http = tls ? require('https') : require('http'); | ||
var ca = tls ? [fs.readFileSync(caCertPath || "./" + downloadCACert(), {encoding: 'utf-8'})] : []; | ||
return function (host, port, path, jsonBody, resolveCallback) { | ||
var deferred = defer(); | ||
var body = (typeof jsonBody === "string" ? jsonBody : JSON.stringify(jsonBody || "")); | ||
var options = { | ||
protocol: tls ? 'https:' : 'http:', | ||
method: 'PUT', | ||
host: host, | ||
path: path, | ||
port: port, | ||
ca: ca, | ||
headers: { | ||
'Content-Type': "application/json; charset=utf-8" | ||
}, | ||
}; | ||
var req = http.request(options); | ||
req.once('response', function (response) { | ||
var data = ''; | ||
response.on('data', function (chunk) { | ||
data += chunk; | ||
}); | ||
response.on('end', function () { | ||
if (resolveCallback) { | ||
deferred.resolve(resolveCallback(data)); | ||
} else { | ||
if (response.statusCode >= 400 && response.statusCode < 600) { | ||
if (response.statusCode === 404) { | ||
deferred.reject("404 Not Found"); | ||
} else { | ||
deferred.reject(data); | ||
} | ||
} else { | ||
deferred.reject(data); | ||
deferred.resolve({ | ||
statusCode: response.statusCode, | ||
body: data | ||
}); | ||
} | ||
} else { | ||
deferred.resolve({ | ||
statusCode: response.statusCode, | ||
body: data | ||
}); | ||
} | ||
}); | ||
}); | ||
req.once('error', function (error) { | ||
if (error.code && error.code === "ECONNREFUSED") { | ||
deferred.reject("Can't connect to MockServer running on host: \"" + host + "\" and port: \"" + port + "\""); | ||
} else { | ||
deferred.reject(JSON.stringify(error)); | ||
} | ||
}); | ||
}); | ||
req.once('error', function (error) { | ||
if (error.code && error.code === "ECONNREFUSED") { | ||
deferred.reject("Can't connect to MockServer running on host: \"" + host + "\" and port: \"" + port + "\""); | ||
} else { | ||
deferred.reject(JSON.stringify(error)); | ||
} | ||
}); | ||
req.write(body); | ||
req.end(); | ||
req.write(body); | ||
req.end(); | ||
return deferred.promise; | ||
return deferred.promise; | ||
}; | ||
}; | ||
@@ -86,0 +134,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
15
192
3
59754
8
867
Updatedwebsocket@^1.0.30