mockserver-client
Advanced tools
Comparing version 5.8.0 to 5.8.1
@@ -29,3 +29,3 @@ /* | ||
var body = (typeof jsonBody === "string" ? jsonBody : JSON.stringify(jsonBody || "")); | ||
var url = 'http://' + host + ':' + port + path; | ||
var url = (tls ? 'https' : 'http') + '://' + host + ':' + port + path; | ||
@@ -108,3 +108,3 @@ return { | ||
} else if (!Array.isArray(arrayTarget) && Array.isArray(arraySource)) { | ||
arraySource.forEach(function(entry) { | ||
arraySource.forEach(function (entry) { | ||
arrayTarget[entry["name"]] = entry["values"]; | ||
@@ -118,3 +118,3 @@ }); | ||
} | ||
} else if (!Array.isArray(arrayTarget) && !Array.isArray(arraySource)) { | ||
} else if (!Array.isArray(arrayTarget) && !Array.isArray(arraySource)) { | ||
arrayTarget = Object.assign(arrayTarget, arraySource); | ||
@@ -177,3 +177,3 @@ } | ||
var WebSocketClient = (typeof require !== 'undefined' ? require('./webSocketClient').webSocketClient : function (host, port, contextPath) { | ||
var WebSocketClient = (typeof require !== 'undefined' ? require('./webSocketClient').webSocketClient(tls, caCertPemFilePath) : function (host, port, contextPath) { | ||
var clientId; | ||
@@ -184,55 +184,62 @@ var clientIdHandler; | ||
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."; | ||
} | ||
} | ||
return { | ||
then: function (sucess, error) { | ||
try { | ||
if (typeof (window) !== "undefined") { | ||
if (window.WebSocket) { | ||
browserWebSocket = window.WebSocket; | ||
} else if (window.MozWebSocket) { | ||
browserWebSocket = window.MozWebSocket; | ||
} else { | ||
error("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 (browserWebSocket) { | ||
var webSocketLocation = (tls ? "wss" : "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.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) { | ||
}; | ||
} | ||
} else if (message.type === "org.mockserver.serialization.model.WebSocketClientIdDTO") { | ||
var registration = JSON.parse(message.value); | ||
if (registration.clientId) { | ||
clientId = registration.clientId; | ||
if (clientIdHandler) { | ||
clientIdHandler(clientId); | ||
sucess({ | ||
requestCallback: function requestCallback(callback) { | ||
requestHandler = callback; | ||
}, | ||
clientIdCallback: function clientIdCallback(callback) { | ||
clientIdHandler = callback; | ||
if (clientId) { | ||
clientIdHandler(clientId); | ||
} | ||
} | ||
}); | ||
} catch (e) { | ||
if (error) { | ||
error(e); | ||
} | ||
} | ||
}; | ||
socket.onopen = function (event) { | ||
}; | ||
socket.onclose = function (event) { | ||
}; | ||
} | ||
function requestCallback(callback) { | ||
requestHandler = callback; | ||
} | ||
function clientIdCallback(callback) { | ||
clientIdHandler = callback; | ||
if (clientId) { | ||
clientIdHandler(clientId); | ||
} | ||
} | ||
return { | ||
requestCallback: requestCallback, | ||
clientIdCallback: clientIdCallback | ||
}; | ||
@@ -312,3 +319,3 @@ }); | ||
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) { | ||
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); | ||
@@ -319,3 +326,3 @@ } | ||
expectation.httpRequest = addDefaultRequestMatcherHeaders(expectation.httpRequest); | ||
if(!expectation.httpResponseTemplate && !expectation.httpResponseClassCallback && !expectation.httpResponseObjectCallback && !expectation.httpForward && !expectation.httpForwardTemplate && !expectation.httpForwardClassCallback && !expectation.httpForwardObjectCallback && !expectation.httpOverrideForwardedRequest && !expectation.httpError) { | ||
if (!expectation.httpResponseTemplate && !expectation.httpResponseClassCallback && !expectation.httpResponseObjectCallback && !expectation.httpForward && !expectation.httpForwardTemplate && !expectation.httpForwardClassCallback && !expectation.httpForwardObjectCallback && !expectation.httpOverrideForwardedRequest && !expectation.httpError) { | ||
expectation.httpResponse = addDefaultResponseMatcherHeaders(expectation.httpResponse); | ||
@@ -395,16 +402,21 @@ } | ||
try { | ||
var webSocketClient = new WebSocketClient(host, port, cleanedContextPath); | ||
webSocketClient.requestCallback(function (request) { | ||
var response = requestHandler(request); | ||
response.headers = headersUniqueConcatenate(response.headers, [ | ||
{"name": "WebSocketCorrelationId", "values": request.headers["WebSocketCorrelationId"] || request.headers["websocketcorrelationid"]} | ||
]); | ||
return { | ||
type: "org.mockserver.model.HttpResponse", | ||
value: JSON.stringify(response) | ||
}; | ||
}); | ||
webSocketClient.clientIdCallback(function (clientId) { | ||
return makeRequest(host, port, "/expectation", createExpectationWithCallback(requestMatcher, clientId, times)).then(sucess, error); | ||
}); | ||
var webSocketClientPromise = new WebSocketClient(host, port, cleanedContextPath); | ||
webSocketClientPromise.then(function (webSocketClient) { | ||
webSocketClient.requestCallback(function (request) { | ||
var response = requestHandler(request); | ||
response.headers = headersUniqueConcatenate(response.headers, [ | ||
{ | ||
"name": "WebSocketCorrelationId", | ||
"values": request.headers["WebSocketCorrelationId"] || request.headers["websocketcorrelationid"] | ||
} | ||
]); | ||
return { | ||
type: "org.mockserver.model.HttpResponse", | ||
value: JSON.stringify(response) | ||
}; | ||
}); | ||
webSocketClient.clientIdCallback(function (clientId) { | ||
return makeRequest(host, port, "/expectation", createExpectationWithCallback(requestMatcher, clientId, times)).then(sucess, error); | ||
}); | ||
}, error); | ||
} catch (e) { | ||
@@ -411,0 +423,0 @@ if (error) { |
{ | ||
"name": "mockserver-client", | ||
"description": "A node client for the MockServer", | ||
"version": "5.8.0", | ||
"version": "5.8.1", | ||
"homepage": "http://mock-server.com", | ||
@@ -45,3 +45,3 @@ "author": { | ||
"karma-xvfb-chrome-launcher": "0.0.1", | ||
"mockserver-node": "5.8.0", | ||
"mockserver-node": "5.8.1", | ||
"nodeunit": "^0.11.3" | ||
@@ -48,0 +48,0 @@ }, |
@@ -194,2 +194,3 @@ # mockserver-client-node | ||
2019-12-01 | v5.8.0 | Upgrading MockServer to 5.8.0 | ||
2019-12-24 | v5.8.1 | Upgrading MockServer to 5.8.1 | ||
@@ -196,0 +197,0 @@ --- |
@@ -14,3 +14,2 @@ /* | ||
var fs = require('fs'); | ||
var glob = require('glob'); | ||
@@ -17,0 +16,0 @@ var defer = function () { |
@@ -13,62 +13,131 @@ /* | ||
var WebSocketClient = require('websocket').client; | ||
var Q = require('q'); | ||
var fs = require('fs'); | ||
var webSocketClient = function (host, port, contextPath) { | ||
var defer = function () { | ||
var promise = (global.protractor && protractor.promise.USE_PROMISE_MANAGER !== false) | ||
? protractor.promise | ||
: Q; | ||
var deferred = promise.defer(); | ||
var clientId; | ||
var clientIdHandler; | ||
var requestHandler; | ||
var webSocketLocation = "ws://" + host + ":" + port + contextPath + "/_mockserver_callback_websocket"; | ||
if (deferred.fulfill && !deferred.resolve) { | ||
deferred.resolve = deferred.fulfill; | ||
} | ||
return deferred; | ||
}; | ||
var client = new WebSocketClient({ | ||
maxReceivedFrameSize: 64 * 1024 * 1024, // 64MiB | ||
maxReceivedMessageSize: 64 * 1024 * 1024, // 64MiB | ||
fragmentOutgoingMessages: false | ||
}); | ||
var downloadCACert = function (tls, caCertPath, callback) { | ||
// https://raw.githubusercontent.com/mock-server/mockserver/master/mockserver-core/src/main/resources/org/mockserver/socket/CertificateAuthorityCertificate.pem | ||
client.on('connectFailed', function (error) { | ||
console.log('Connect Error: ' + error.toString()); | ||
}); | ||
var dest = "CertificateAuthorityCertificate.pem"; | ||
if (!fs.existsSync('./' + dest)) { | ||
var options = { | ||
protocol: 'https:', | ||
method: 'GET', | ||
host: "raw.githubusercontent.com", | ||
path: "/mock-server/mockserver/master/mockserver-core/src/main/resources/org/mockserver/socket/CertificateAuthorityCertificate.pem", | ||
port: 443, | ||
}; | ||
var req = require('https').request(options); | ||
client.on('connect', function (connection) { | ||
connection.on('error', function (error) { | ||
console.log('Connection Error: ' + error.toString()); | ||
req.once('error', function (error) { | ||
console.error('Fetching ' + JSON.stringify(options, null, 2) + ' failed with error ' + error); | ||
}); | ||
connection.on('message', function (message) { | ||
if (message.type === 'utf8') { | ||
var payload = JSON.parse(message.utf8Data); | ||
if (payload.type === "org.mockserver.model.HttpRequest") { | ||
var request = JSON.parse(payload.value); | ||
var response = requestHandler(request); | ||
connection.sendUTF(JSON.stringify(response)); | ||
} else if (payload.type === "org.mockserver.serialization.model.WebSocketClientIdDTO") { | ||
var registration = JSON.parse(payload.value); | ||
if (registration.clientId) { | ||
clientId = registration.clientId; | ||
if (clientIdHandler) { | ||
clientIdHandler(clientId); | ||
} | ||
} | ||
} | ||
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 { | ||
console.log('Incorrect message format: ' + JSON.parse(message)); | ||
var writeStream = fs.createWriteStream(dest); | ||
res.pipe(writeStream); | ||
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)); | ||
callback(tls ? [fs.readFileSync(caCertPath || "./" + dest, {encoding: 'utf-8'})] : []); | ||
}); | ||
} | ||
}); | ||
}); | ||
client.connect(webSocketLocation, []); | ||
function requestCallback(callback) { | ||
requestHandler = callback; | ||
req.end(); | ||
} else { | ||
callback(tls ? [fs.readFileSync(caCertPath || "./" + dest, {encoding: 'utf-8'})] : []); | ||
} | ||
}; | ||
function clientIdCallback(callback) { | ||
clientIdHandler = callback; | ||
if (clientId) { | ||
clientIdHandler(clientId); | ||
} | ||
} | ||
var webSocketClient = function (tls, caCertPath) { | ||
return function (host, port, contextPath) { | ||
var deferred = defer(); | ||
downloadCACert(tls, caCertPath, function (ca) { | ||
return { | ||
requestCallback: requestCallback, | ||
clientIdCallback: clientIdCallback | ||
var clientId; | ||
var clientIdHandler; | ||
var requestHandler; | ||
var webSocketLocation = (tls ? "wss" : "ws") + "://" + host + ":" + port + contextPath + "/_mockserver_callback_websocket"; | ||
var client = new WebSocketClient({ | ||
maxReceivedFrameSize: 64 * 1024 * 1024, // 64MiB | ||
maxReceivedMessageSize: 64 * 1024 * 1024, // 64MiB | ||
fragmentOutgoingMessages: false, | ||
tlsOptions: { | ||
ca: ca, | ||
port: port | ||
} | ||
}); | ||
client.on('connectFailed', 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)); | ||
} | ||
}); | ||
client.on('connect', function (connection) { | ||
connection.on('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)); | ||
} | ||
}); | ||
connection.on('message', function (message) { | ||
if (message.type === 'utf8') { | ||
var payload = JSON.parse(message.utf8Data); | ||
if (payload.type === "org.mockserver.model.HttpRequest") { | ||
var request = JSON.parse(payload.value); | ||
var response = requestHandler(request); | ||
connection.sendUTF(JSON.stringify(response)); | ||
} else if (payload.type === "org.mockserver.serialization.model.WebSocketClientIdDTO") { | ||
var registration = JSON.parse(payload.value); | ||
if (registration.clientId) { | ||
clientId = registration.clientId; | ||
if (clientIdHandler) { | ||
clientIdHandler(clientId); | ||
} | ||
} | ||
} | ||
} else { | ||
console.log('Incorrect message format: ' + JSON.parse(message)); | ||
} | ||
}); | ||
}); | ||
client.connect(webSocketLocation, []); | ||
deferred.resolve({ | ||
requestCallback: function requestCallback(callback) { | ||
requestHandler = callback; | ||
}, | ||
clientIdCallback: function clientIdCallback(callback) { | ||
clientIdHandler = callback; | ||
if (clientId) { | ||
clientIdHandler(clientId); | ||
} | ||
} | ||
}); | ||
}); | ||
return deferred.promise; | ||
}; | ||
@@ -75,0 +144,0 @@ }; |
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
65923
941
201
4
4