Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mockserver-client

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mockserver-client - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

13

bower.json
{
"name": "mockserver-client",
"version": "1.0.1",
"version": "1.0.2",
"homepage": "https://github.com/jamesdbloom/mockserver-client-node",

@@ -9,3 +9,3 @@ "authors": [

"description": "A node client for the MockServer",
"main": "mockserver-client.js",
"main": "index.js",
"keywords": [

@@ -24,4 +24,11 @@ "mockserver",

"Gruntfile.js",
"*.iml"
"*.iml",
"*.yml"
],
"dependencies": {
"q": "~1.0"
},
"moduleType": [
"node"
]
}

@@ -14,2 +14,5 @@ /*

grunt.initConfig({
exec: {
stop_existing_mockservers: './stop_MockServer.sh'
},
jshint: {

@@ -23,4 +26,4 @@ options: {

'!js/lib/**/*.js',
'<%= nodeunit.no_proxy %>'//,
// '<%= nodeunit.with_proxy %>'
'<%= nodeunit.no_proxy %>',
'<%= nodeunit.with_proxy %>'
]

@@ -47,5 +50,5 @@ },

],
// with_proxy: [
// 'test/with_proxy/*_test.js'
// ],
with_proxy: [
'test/with_proxy/*_test.js'
],
options: {

@@ -57,2 +60,3 @@ reporter: 'nested'

grunt.loadNpmTasks('grunt-exec');
grunt.loadNpmTasks('mockserver-grunt');

@@ -65,3 +69,4 @@ grunt.loadNpmTasks('grunt-contrib-jshint');

grunt.registerTask('default', ['jshint', 'test']);
grunt.registerTask('wrecker', ['jshint', 'test']);
grunt.registerTask('default', ['exec', 'wrecker']);
};

@@ -13,18 +13,38 @@ /*

var Q = require('q');
var request = require('request');
var http = require('http');
function sendRequest(url, jsonBody) {
function sendRequest(host, port, path, jsonBody) {
var deferred = Q.defer();
var body = (typeof jsonBody === "string" ? jsonBody : JSON.stringify(jsonBody || ""));
var options = {
method: 'PUT',
url: url,
json: jsonBody
host: host,
path: path,
port: port
};
request(options, function (error, response, body) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(body && JSON.parse(body));
var callback = function (response) {
var body = '';
if (response.statusCode === 400 || response.statusCode === 404) {
deferred.reject(response.statusCode);
}
});
response.on('data', function (chunk) {
body += chunk;
});
response.on('end', function () {
deferred.resolve({
statusCode: response.statusCode,
body: body
});
});
};
var req = http.request(options, callback);
req.write(body);
req.end();
return deferred.promise;

@@ -44,165 +64,190 @@ }

var mockServerUrl = "http://" + host + ":" + port,
/**
* The default headers added to to the mocked response when using mockSimpleResponse(...)
*/
defaultResponseHeaders = [
{"name": "Content-Type", "values": ["application/json; charset=utf-8"]},
{"name": "Cache-Control", "values": ["no-cache, no-store"]}
],
createResponseMatcher = function (path) {
return {
method: "",
path: path,
body: "",
headers: [],
/**
* 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 createResponseMatcher = function (path) {
return {
method: "",
path: path,
body: "",
headers: [],
cookies: [],
parameters: []
};
};
var createExpectation = function (path, responseBody, statusCode) {
return {
httpRequest: createResponseMatcher(path),
httpResponse: {
statusCode: statusCode || 200,
body: JSON.stringify(responseBody),
cookies: [],
parameters: []
};
},
createExpectation = function (path, responseBody, statusCode) {
var headers = [];
return {
httpRequest: createResponseMatcher(path),
httpResponse: {
statusCode: statusCode || 200,
body: JSON.stringify(responseBody),
cookies: [],
headers: defaultResponseHeaders,
delay: {
timeUnit: "MICROSECONDS",
value: 0
}
},
times: {
remainingTimes: 1,
unlimited: false
headers: defaultResponseHeaders,
delay: {
timeUnit: "MICROSECONDS",
value: 0
}
};
},
/**
* 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
*/
mockAnyResponse = function (expectation) {
return sendRequest(mockServerUrl + "/expectation", expectation);
},
/**
* 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
*/
mockSimpleResponse = function (path, responseBody, statusCode) {
return mockAnyResponse(createExpectation(path, responseBody, statusCode));
},
/**
* Override the default headers that are used to specify the response headers in mockSimpleResponse(...)
* (note: if you use mockAnyResponse(...) the default headers are not used)
* for example:
*
* mockServerClient("localhost", 1080).setDefaultHeaders([
* {"name": "Content-Type", "values": ["application/json; charset=utf-8"]},
* {"name": "Cache-Control", "values": ["no-cache, no-store"]}
* ])
*
* @param headers the path to match requests against
*/
setDefaultHeaders = function (headers) {
defaultResponseHeaders = headers;
},
butFoundAssertionErrorMessage = function (expectedMessage) {
sendRequest(mockServerUrl + "/retrieve").then(function (expectations) {
throw expectedMessage + " but " + (expectations ? "only found " + expectations : "found no requests");
});
},
retrieve = function (request) {
return sendRequest(mockServerUrl + "/retrieve", request);
},
/**
* 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"
*/
verify = function (request, count, exact) {
return retrieve(request)
.then(function (expectations) {
if (!expectations) {
butFoundAssertionErrorMessage("Expected " + JSON.stringify(request));
}
if (exact) {
if (expectations.length !== count) {
butFoundAssertionErrorMessage("Expected " + JSON.stringify(request));
}
} else {
if (expectations.length < count) {
butFoundAssertionErrorMessage("Expected " + JSON.stringify(request));
}
}
return _this;
});
},
/**
* Reset MockServer by clearing all expectations
*/
reset = function () {
return sendRequest(mockServerUrl + "/reset");
},
/**
* Clear all expectations that match the specified path
*
* @param path the path to decide which expectations to cleared
*/
clear = function (path) {
return sendRequest(mockServerUrl + "/clear", createResponseMatcher(path || ".*"));
},
/**
* Pretty-print the json for all expectations for the specified path.
* This is particularly helpful when debugging expectations. The expectation
* are printed into a dedicated log called mockserver_request.log
*
* @param path the path to decide which expectations to dump to the log
*/
dumpToLogs = function (path) {
return sendRequest(mockServerUrl + "/dumpToLog", createResponseMatcher(path || ".*"));
},
times: {
remainingTimes: 1,
unlimited: false
}
};
};
/**
* 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 sendRequest(host, port, "/expectation", expectation);
};
/**
* 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 the default headers that are used to specify the response headers in mockSimpleResponse(...)
* (note: if you use mockAnyResponse(...) the default headers are not used)
* for example:
*
* mockServerClient("localhost", 1080).setDefaultHeaders([
* {"name": "Content-Type", "values": ["application/json; charset=utf-8"]},
* {"name": "Cache-Control", "values": ["no-cache, no-store"]}
* ])
*
* @param headers the path to match requests against
*/
var setDefaultHeaders = function (headers) {
defaultResponseHeaders = headers;
};
/**
* 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 sendRequest(host, port, "/verify", {
"httpRequest": request,
"times": {
"count": count,
"exact": exact
}
}).then(function (result) {
if (result.statusCode !== 202) {
console && console.error && console.error(result.body);
throw result.body;
}
return _this;
});
};
/**
* 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 sendRequest(host, port, "/verifySequence", {
"httpRequests": requestSequence
}).then(function (result) {
if (result.statusCode !== 202) {
console && console.error && console.error(result.body);
throw result.body;
}
return _this;
});
};
/**
* Reset MockServer by clearing all expectations
*/
var reset = function () {
return sendRequest(host, port, "/reset");
};
/**
* Clear all expectations that match the specified path
*
* @param path the path to decide which expectations to cleared
*/
var clear = function (path) {
return sendRequest(host, port, "/clear", createResponseMatcher(path || ".*"));
};
/**
* Pretty-print the json for all expectations for the specified path.
* This is particularly helpful when debugging expectations. The expectation
* are printed into a dedicated log called mockserver_request.log
*
* @param path the path to decide which expectations to dump to the log
*/
var dumpToLogs = function (path) {
return sendRequest(host, port, "/dumpToLog", createResponseMatcher(path || ".*"));
};

@@ -214,2 +259,3 @@ var _this = {

verify: verify,
verifySequence: verifySequence,
reset: reset,

@@ -232,86 +278,106 @@ clear: clear,

var proxyUrl = "http://" + host + ":" + port,
createResponseMatcher = function (path) {
return {
method: "",
path: path,
body: "",
headers: [],
cookies: [],
parameters: []
};
},
butFoundAssertionErrorMessage = function (expectedMessage) {
sendRequest(proxyUrl + "/retrieve").then(function (requests) {
throw expectedMessage + " but " + (requests ? "only found " + requests : "found no requests");
});
},
/**
* Retrieve the recorded requests that match the httpRequest parameter as a JSON array, use null for the parameter to retrieve all requests
*
* @param request the http request that is matched against when deciding whether to return each expectation, use null for the parameter to retrieve for all requests
* @return a JSON array of all expectations that have been recorded by the proxy
*/
retrieve = function (request) {
return sendRequest(proxyUrl + "/retrieve", request);
},
/**
* 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"
*/
verify = function (request, count, exact) {
return retrieve(request)
.then(function (requests) {
if (!requests) {
butFoundAssertionErrorMessage("Expected " + JSON.stringify(request));
}
if (exact) {
if (requests.length !== count) {
butFoundAssertionErrorMessage("Expected " + JSON.stringify(request));
}
} else {
if (requests.length < count) {
butFoundAssertionErrorMessage("Expected " + JSON.stringify(request));
}
}
return _this;
});
},
/**
* Reset the proxy by clearing all recorded requests
*/
reset = function () {
return sendRequest(proxyUrl + "/reset");
},
/**
* Clear all recorded requests that match the specified path
*
* @param path the path to decide which expectations to cleared
*/
clear = function (path) {
return sendRequest(proxyUrl + "/clear", createResponseMatcher(path || ".*"));
},
/**
* Pretty-print the json for all requests / responses that match the specified path
* as Expectations to the log. They are printed into a dedicated log called mockserver_request.log
*
* @param path the path to decide which expectations to dump to the log
*/
dumpToLogs = function (path) {
return sendRequest(proxyUrl + "/dumpToLog", createResponseMatcher(path || ".*", ""));
var createResponseMatcher = function (path) {
return {
method: "",
path: path,
body: "",
headers: [],
cookies: [],
parameters: []
};
};
/**
* 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 sendRequest(host, port, "/verify", JSON.stringify({
"httpRequest": request,
"times": {
"count": count,
"exact": exact
}
})).then(function (result) {
if (result.statusCode !== 202) {
console && console.error && console.error(result.body);
throw result.body;
}
return _this;
});
};
/**
* 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 sendRequest(host, port, "/verifySequence", JSON.stringify({
"httpRequests": requestSequence
})).then(function (result) {
if (result.statusCode !== 202) {
console && console.error && console.error(result.body);
throw result.body;
}
return _this;
});
};
/**
* Reset the proxy by clearing all recorded requests
*/
var reset = function () {
return sendRequest(host, port, "/reset");
};
/**
* Clear all recorded requests that match the specified path
*
* @param path the path to decide which expectations to cleared
*/
var clear = function (path) {
return sendRequest(host, port, "/clear", createResponseMatcher(path || ".*"));
};
/**
* Pretty-print the json for all requests / responses that match the specified path
* as Expectations to the log. They are printed into a dedicated log called mockserver_request.log
*
* @param path the path to decide which expectations to dump to the log
*/
var dumpToLogs = function (path) {
return sendRequest(host, port, "/dumpToLog", createResponseMatcher(path || ".*", ""));
};
var _this = {
retrieve: retrieve,
verify: verify,
verifySequence: verifySequence,
reset: reset,

@@ -318,0 +384,0 @@ clear: clear,

{
"name": "mockserver-client",
"description": "A node client for the MockServer",
"version": "1.0.1",
"version": "1.0.2",
"homepage": "https://github.com/jamesdbloom/mockserver",

@@ -36,5 +36,5 @@ "author": {

"grunt-run-node": "~0.1",
"mockserver-grunt": "~1.0",
"nodeunit": "~0.9",
"sinon": "~1.10"
"grunt-exec": "~0.4",
"mockserver-grunt": "~1",
"nodeunit": "~0.9"
},

@@ -50,5 +50,4 @@ "peerDependencies": {

"dependencies": {
"q": "^1.0.1",
"request": "^2.47.0"
"q": "~1.1"
}
}

@@ -96,3 +96,4 @@ # mockserver-client-node

* 2014-01-11   v1.0.0   Cleaned code & removed duplication
* 2014-02-11   v1.0.1   Added wercher build process
* 2014-02-11   v1.0.1   Added wercker build process
* 2014-02-21   v1.0.2   Fixed asynchronous errors

@@ -99,0 +100,0 @@ ---

@@ -5,33 +5,61 @@ (function () {

var mockServer = require('../../'),
mockServerClient = mockServer.mockServerClient,
proxyClient = mockServer.proxyClient,
Q = require('q'),
request = require('request'),
sendRequest = function (method, url, body) {
var deferred = Q.defer();
var options = {
method: method,
url: url,
body: body
};
request(options, function (error, response) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(response);
}
var mockServer = require('../../');
var mockServerClient = mockServer.mockServerClient;
var proxyClient = mockServer.proxyClient;
var Q = require('q');
var http = require('http');
function sendRequest(method, host, port, path, jsonBody) {
var deferred = Q.defer();
var body = (typeof jsonBody === "string" ? jsonBody : JSON.stringify(jsonBody || ""));
var options = {
method: method,
host: host,
path: path,
port: port
};
var callback = function (response) {
var body = '';
if (response.statusCode === 400 || response.statusCode === 404) {
deferred.reject(response.statusCode);
}
response.on('data', function (chunk) {
body += chunk;
});
return deferred.promise;
response.on('end', function () {
deferred.resolve({
statusCode: response.statusCode,
headers: response.headers,
body: body
});
});
};
var req = http.request(options, callback);
req.write(body);
req.end();
return deferred.promise;
}
exports.mock_server_started = {
setUp: function (callback) {
mockServerClient("localhost", 1080).reset();
proxyClient("localhost", 1090).reset();
callback();
mockServerClient("localhost", 1080).reset().then(function () {
proxyClient("localhost", 1090).reset().then(function () {
callback();
}, function (error) {
throw 'Failed with error ' + JSON.stringify(error);
});
}, function (error) {
throw 'Failed with error ' + JSON.stringify(error);
});
},
'should create full expectation with string body': function (test) {
// when
// given - a client and expectation
mockServerClient("localhost", 1080).mockAnyResponse(

@@ -67,35 +95,39 @@ {

}
);
).then(function () {
// then - non matching request
sendRequest("GET", "http://localhost:1080/otherPath")
.then(function (response) {
test.equal(response.statusCode, 404);
}, function (error) {
test.ok(false, error);
});
// then - non matching request
sendRequest("GET", "localhost", 1080, "/otherPath")
.then(function (response) {
test.ok(false, "should not match expectation");
}, function (error) {
test.equal(error, 404);
}).then(function () {
// then - matching request
sendRequest("POST", "http://localhost:1080/somePath?test=true", "someBody")
.then(function (response) {
test.equal(response.statusCode, 200);
test.equal(response.body, '{"name":"value"}');
}, function (error) {
test.ok(false, error);
});
// then - matching request
sendRequest("POST", "localhost", 1080, "/somePath?test=true", "someBody")
.then(function (response) {
test.equal(response.statusCode, 200);
test.equal(response.body, '{"name":"value"}');
}, function (error) {
test.ok(false, "should match expectation");
}).then(function () {
// then - matching request, but no times remaining
sendRequest("POST", "http://localhost:1080/somePath?test=true", "someBody")
.then(function (response) {
test.equal(response.statusCode, 404);
}, function (error) {
test.ok(false, error);
// then - matching request, but no times remaining
sendRequest("POST", "localhost", 1080, "/somePath?test=true", "someBody")
.then(function (response) {
test.ok(false, "should match expectation but no times remaining");
}, function (error) {
test.equal(error, 404);
}).then(function () {
test.done();
});
});
});
});
},
// end
test.done();
},
'should match on body only': function (test) {
// when
// given - a client
var client = mockServerClient("localhost", 1080);
// and - an expectation
client.mockAnyResponse(

@@ -123,316 +155,417 @@ {

}
);
client.mockAnyResponse(
{
'httpRequest': {
'path': '/somePath',
'body': {
'type': "REGEX",
'value': 'someOtherBody'
).then(function () {
// and - another expectation
client.mockAnyResponse(
{
'httpRequest': {
'path': '/somePath',
'body': {
'type': "REGEX",
'value': 'someOtherBody'
}
},
'httpResponse': {
'statusCode': 200,
'body': JSON.stringify({ name: 'second_body' }),
'delay': {
'timeUnit': 'MILLISECONDS',
'value': 250
}
},
'times': {
'remainingTimes': 1,
'unlimited': false
}
}
},
'httpResponse': {
'statusCode': 200,
'body': JSON.stringify({ name: 'second_body' }),
'delay': {
'timeUnit': 'MILLISECONDS',
'value': 250
}
},
'times': {
'remainingTimes': 1,
'unlimited': false
}
}
);
).then(function () {
// then - non matching request
sendRequest("POST", "http://localhost:1080/otherPath", "someIncorrectBody")
.then(function (response) {
test.equal(response.statusCode, 404);
}, function (error) {
test.ok(false, error);
});
// then - non matching request
sendRequest("POST", "localhost", 1080, "/otherPath", "someIncorrectBody")
.then(function (response) {
test.ok(false, "should not match expectation");
}, function (error) {
test.equal(error, 404);
}).then(function () {
// then - matching request
sendRequest("POST", "http://localhost:1080/otherPath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 200);
test.equal(response.body, '{"name":"first_body"}');
}, function (error) {
test.ok(false, error);
});
// then - request that matches first expectation
sendRequest("POST", "localhost", 1080, "/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 200);
test.equal(response.body, '{"name":"first_body"}');
}, function () {
test.ok(false, "should match expectation");
}).then(function () {
// then - matches second expectation as body different
sendRequest("POST", "http://localhost:1080/otherPath", "someOtherBody")
.then(function (response) {
test.equal(response.statusCode, 200);
test.equal(response.body, '{"name":"second_body"}');
}, function (error) {
test.ok(false, error);
// then - request that matches second expectation
sendRequest("POST", "localhost", 1080, "/somePath", "someOtherBody")
.then(function (response) {
test.equal(response.statusCode, 200);
test.equal(response.body, '{"name":"second_body"}');
}, function (error) {
test.ok(false, "should match expectation");
}).then(function () {
test.done();
});
});
});
});
});
// end
test.done();
},
'should create simple response expectation': function (test) {
// when
mockServerClient("localhost", 1080).mockSimpleResponse('/somePath', { name: 'value' }, 203);
// given - a client and expectation
mockServerClient("localhost", 1080).mockSimpleResponse('/somePath', { name: 'value' }, 203).then(function () {
// then - non matching request
sendRequest("POST", "http://localhost:1080/otherPath")
.then(function (response) {
test.equal(response.statusCode, 404);
}, function (error) {
test.ok(false, error);
});
// then - non matching request
sendRequest("POST", "localhost", 1080, "/otherPath")
.then(function (response) {
test.ok(false, "should not match expectation");
}, function (error) {
test.equal(error, 404);
}).then(function () {
// then - matching request
sendRequest("POST", "http://localhost:1080/somePath?test=true", "someBody")
.then(function (response) {
test.equal(response.statusCode, 200);
test.equal(response.body, '{"name":"value"}');
}, function (error) {
test.ok(false, error);
});
// then - matching request
sendRequest("POST", "localhost", 1080, "/somePath?test=true", "someBody")
.then(function (response) {
test.equal(response.statusCode, 203);
test.equal(response.body, '{"name":"value"}');
}, function () {
test.ok(false, "should match expectation");
}).then(function () {
// then - matching request, but no times remaining
sendRequest("POST", "http://localhost:1080/somePath?test=true", "someBody")
.then(function (response) {
test.equal(response.statusCode, 404);
}, function (error) {
test.ok(false, error);
});
// end
test.done();
// then - matching request, but no times remaining
sendRequest("POST", "localhost", 1080, "/somePath?test=true", "someBody")
.then(function (response) {
test.ok(false, "should match expectation but no times remaining");
}, function (error) {
test.equal(error, 404);
}).then(function () {
test.done();
});
});
});
});
},
'should update default headers for simple response expectation': function (test) {
// when
// given - a client
var client = mockServerClient("localhost", 1080);
// and - default headers
client.setDefaultHeaders([
{"name": "Content-Type", "values": ["application/json; charset=utf-8"]},
{"name": "X-Test", "values": ["test-value"]}
{"name": "content-type", "values": ["application/json; charset=utf-8"]},
{"name": "x-test", "values": ["test-value"]}
]);
client.mockSimpleResponse('/somePath', { name: 'value' }, 203);
// and - an expectation
client.mockSimpleResponse('/somePath', { name: 'value' }, 203).then(function () {
// then - matching request
sendRequest("POST", "http://localhost:1080/somePath?test=true", "someBody")
.then(function (response) {
test.equal(response.statusCode, 203);
test.equal(response.body, '{"name":"value"}');
test.equal(response.headers, '{"X-Test":"test-value"}');
}, function (error) {
test.ok(false, error);
});
// end
test.done();
// then - matching request
sendRequest("POST", "localhost", 1080, "/somePath?test=true", "someBody")
.then(function (response) {
test.equal(response.statusCode, 203);
test.equal(response.body, '{"name":"value"}');
test.equal(response.headers["content-type"], "application/json; charset=utf-8");
test.equal(response.headers["x-test"], "test-value");
test.done();
}, function (error) {
test.ok(false, "should match expectation");
test.done();
});
});
},
'should verify exact number of requests have been sent': function (test) {
// given
// given - a client
var client = mockServerClient("localhost", 1080);
client.mockSimpleResponse('/somePath', { name: 'value' }, 203);
sendRequest("POST", "http://localhost:1080/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 203);
}, function (error) {
test.ok(false, error);
});
// and - an expectation
client.mockSimpleResponse('/somePath', { name: 'value' }, 203).then(function () {
// and - a request
sendRequest("POST", "localhost", 1080, "/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 203);
}, function (error) {
test.ok(false, error);
}).then(function () {
// when
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 1, true);
// end
test.done();
// when - verify at least one request
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 1, true).then(function () {
test.done();
}, function () {
test.ok(false, "verification should pass");
test.done();
});
});
});
},
'should verify at least a number of requests have been sent': function (test) {
// given
// given - a client
var client = mockServerClient("localhost", 1080);
client.mockSimpleResponse('/somePath', { name: 'value' }, 203);
client.mockSimpleResponse('/somePath', { name: 'value' }, 203);
sendRequest("POST", "http://localhost:1080/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 203);
}, function (error) {
test.ok(false, error);
// and - an expectation
client.mockSimpleResponse('/somePath', { name: 'value' }, 203).then(function () {
// and - another expectation
client.mockSimpleResponse('/somePath', { name: 'value' }, 203).then(function () {
// and - a request
sendRequest("POST", "localhost", 1080, "/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 203);
}, function (error) {
test.ok(false, error);
}).then(function () {
// and - another request
sendRequest("POST", "localhost", 1080, "/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 203);
}, function (error) {
test.ok(false, error);
}).then(function () {
// when - verify at least one request
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 1).then(function () {
test.done();
}, function () {
test.ok(false, "verification should pass");
test.done();
});
});
});
});
sendRequest("POST", "http://localhost:1080/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 203);
}, function (error) {
test.ok(false, error);
});
});
},
// when
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 1);
'should fail when no requests have been sent': function (test) {
// given - a client
var client = mockServerClient("localhost", 1080);
// and - an expectation
client.mockSimpleResponse('/somePath', { name: 'value' }, 203).then(function () {
// and - a request
sendRequest("POST", "localhost", 1080, "/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 203);
}, function (error) {
test.ok(false, error);
}).then(function () {
// end
test.done();
// when - verify at least one request (should fail)
client.verify(
{
'path': '/someOtherPath'
}, 1)
.then(function () {
test.ok(false, "verification should have failed");
test.done();
}, function (message) {
test.equals(message, "Request not found at least once, expected:<{\n" +
" \"path\" : \"/someOtherPath\"\n" +
"}> but was:<{\n" +
" \"method\" : \"POST\",\n" +
" \"path\" : \"/somePath\",\n" +
" \"body\" : \"someBody\",\n" +
" \"headers\" : [ {\n" +
" \"name\" : \"Host\",\n" +
" \"values\" : [ \"localhost:1080\" ]\n" +
" }, {\n" +
" \"name\" : \"Content-Length\",\n" +
" \"values\" : [ \"8\" ]\n" +
" } ]\n" +
"}>");
test.done();
});
});
});
},
// 'should fail when no requests have been sent': function (test) {
// // given
// var client = mockServerClient("localhost", 1080);
// client.mockSimpleResponse('/somePath', { name: 'value' }, 203);
// sendRequest("POST", "http://localhost:1080/somePath", "someBody")
// .then(function (response) {
// test.equal(response.statusCode, 203);
// }, function (error) {
// test.ok(false, error);
// });
//
// // when
// test.throws(function () {
// client.verify(
// {
// 'path': '/someOtherPath'
// }, 1);
// });
//
// // end
// test.done();
// },
'should fail when not enough exact requests have been sent': function (test) {
// given - a client
var client = mockServerClient("localhost", 1080);
// and - an expectation
client.mockSimpleResponse('/somePath', { name: 'value' }, 203).then(function () {
// and - a request
sendRequest("POST", "localhost", 1080, "/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 203);
}, function (error) {
test.ok(false, error);
}).then(function () {
// 'should fail when not enough exact requests have been sent': function (test) {
// // given
// var client = mockServerClient("localhost", 1080);
// client.mockSimpleResponse('/somePath', { name: 'value' }, 203);
// sendRequest("POST", "http://localhost:1080/somePath", "someBody")
// .then(function (response) {
// test.equal(response.statusCode, 203);
// }, function (error) {
// test.ok(false, error);
// });
//
// // when
// test.throws(function () {
// client.verify(
// {
// 'method': 'POST',
// 'path': '/somePath',
// 'body': 'someBody'
// }, 2, true);
// });
//
// // end
// test.done();
// },
// when - verify exact two requests (should fail)
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 2, true)
.then(function () {
test.ok(false, "verification should have failed");
test.done();
}, function (message) {
test.equals(message, "Request not found exactly 2 times, expected:<{\n" +
" \"method\" : \"POST\",\n" +
" \"path\" : \"/somePath\",\n" +
" \"body\" : \"someBody\"\n" +
"}> but was:<{\n" +
" \"method\" : \"POST\",\n" +
" \"path\" : \"/somePath\",\n" +
" \"body\" : \"someBody\",\n" +
" \"headers\" : [ {\n" +
" \"name\" : \"Host\",\n" +
" \"values\" : [ \"localhost:1080\" ]\n" +
" }, {\n" +
" \"name\" : \"Content-Length\",\n" +
" \"values\" : [ \"8\" ]\n" +
" } ]\n" +
"}>");
test.done();
});
});
});
},
// 'should fail when not enough at least requests have been sent': function (test) {
// // given
// var client = mockServerClient("localhost", 1080);
// client.mockSimpleResponse('/somePath', { name: 'value' }, 203);
// sendRequest("POST", "http://localhost:1080/somePath", "someBody")
// .then(function (response) {
// test.equal(response.statusCode, 203);
// }, function (error) {
// test.ok(false, error);
// });
//
// // when
// test.throws(function () {
// client.verify(
// {
// 'method': 'POST',
// 'path': '/somePath',
// 'body': 'someBody'
// }, 2);
// });
//
// // end
// test.done();
// },
'should fail when not enough at least requests have been sent': function (test) {
// given - a client
var client = mockServerClient("localhost", 1080);
// and - an expectation
client.mockSimpleResponse('/somePath', { name: 'value' }, 203).then(function () {
// and - a request
sendRequest("POST", "localhost", 1080, "/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 203);
}, function (error) {
test.ok(false, error);
}).then(function () {
// when - verify at least two requests (should fail)
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 2)
.then(function () {
test.ok(false, "verification should have failed");
test.done();
}, function (message) {
test.equals(message, "Request not found at least 2 times, expected:<{\n" +
" \"method\" : \"POST\",\n" +
" \"path\" : \"/somePath\",\n" +
" \"body\" : \"someBody\"\n" +
"}> but was:<{\n" +
" \"method\" : \"POST\",\n" +
" \"path\" : \"/somePath\",\n" +
" \"body\" : \"someBody\",\n" +
" \"headers\" : [ {\n" +
" \"name\" : \"Host\",\n" +
" \"values\" : [ \"localhost:1080\" ]\n" +
" }, {\n" +
" \"name\" : \"Content-Length\",\n" +
" \"values\" : [ \"8\" ]\n" +
" } ]\n" +
"}>");
test.done();
});
});
});
},
'should clear expectations': function (test) {
// when
// given - a client
var client = mockServerClient("localhost", 1080);
client.mockSimpleResponse('/somePathOne', { name: 'value' }, 200);
client.mockSimpleResponse('/somePathOne', { name: 'value' }, 200);
client.mockSimpleResponse('/somePathTwo', { name: 'value' }, 200);
// and - an expectation
client.mockSimpleResponse('/somePathOne', { name: 'value' }, 200).then(function () {
// and - another expectation
client.mockSimpleResponse('/somePathOne', { name: 'value' }, 200).then(function () {
// and - another expectation
client.mockSimpleResponse('/somePathTwo', { name: 'value' }, 200).then(function () {
// and - a matching request (that returns 200)
sendRequest("GET", "localhost", 1080, "/somePathOne")
.then(function (response) {
test.equal(response.statusCode, 200);
test.equal(response.body, '{"name":"value"}');
}, function (error) {
test.ok(false, error);
}).then(function () {
// then - matching request
sendRequest("GET", "http://localhost:1080/somePathOne")
.then(function (response) {
test.equal(response.statusCode, 200);
test.equal(response.body, '{"name":"value"}');
}, function (error) {
test.ok(false, error);
});
// when - some expectations cleared
client.clear('/somePathOne').then(function () {
// when
client.clear('/somePathOne');
// then - request matching cleared expectation should return 404
sendRequest("GET", "localhost", 1080, "/somePathOne")
.then(function (response) {
test.ok(false, "should clear matching expectations");
}, function (error) {
test.equals(404, error);
}).then(function () {
// then - matching request but cleared
sendRequest("GET", "http://localhost:1080/somePathOne")
.then(function (response) {
test.equal(response.statusCode, 404);
}, function (error) {
test.ok(false, error);
// and - request matching non-cleared expectation should return 200
sendRequest("GET", "localhost", 1080, "/somePathTwo")
.then(function (response) {
test.equal(response.statusCode, 200);
test.equal(response.body, '{"name":"value"}');
test.done();
}, function (error) {
test.ok(false, "should not clear non-matching expectations");
test.done();
});
});
});
});
});
});
// then - matching request and not cleared
sendRequest("GET", "http://localhost:1080/somePathTwo")
.then(function (response) {
test.equal(response.statusCode, 200);
test.equal(response.body, '{"name":"value"}');
}, function (error) {
test.ok(false, error);
});
// end
test.done();
});
},
'should reset expectations': function (test) {
// when
// given - a client
var client = mockServerClient("localhost", 1080);
client.mockSimpleResponse('/somePathOne', { name: 'value' }, 200);
client.mockSimpleResponse('/somePathOne', { name: 'value' }, 200);
client.mockSimpleResponse('/somePathTwo', { name: 'value' }, 200);
// and - an expectation
client.mockSimpleResponse('/somePathOne', { name: 'value' }, 200).then(function () {
// and - another expectation
client.mockSimpleResponse('/somePathOne', { name: 'value' }, 200).then(function () {
// and - another expectation
client.mockSimpleResponse('/somePathTwo', { name: 'value' }, 200).then(function () {
// and - a matching request (that returns 200)
sendRequest("GET", "localhost", 1080, "/somePathOne")
.then(function (response) {
test.equal(response.statusCode, 200);
test.equal(response.body, '{"name":"value"}');
}, function (error) {
test.ok(false, "should match expectation");
}).then(function () {
// then - matching request
sendRequest("GET", "http://localhost:1080/somePathOne")
.then(function (response) {
test.equal(response.statusCode, 200);
test.equal(response.body, '{"name":"value"}');
}, function (error) {
test.ok(false, error);
});
// when - all expectations reset
client.reset().then(function () {
// when
client.reset();
// then - request matching one reset expectation should return 404
sendRequest("GET", "localhost", 1080, "/somePathOne")
.then(function () {
test.ok(false, "should clear all expectations");
}, function (error) {
test.equals(404, error);
}).then(function () {
// then - matching request but cleared
sendRequest("GET", "http://localhost:1080/somePathOne")
.then(function (response) {
test.equal(response.statusCode, 404);
}, function (error) {
test.ok(false, error);
});
// then - request matching other reset expectation should return 404
sendRequest("GET", "localhost", 1080, "/somePathTwo")
.then(function () {
test.ok(false, "should clear all expectations");
test.done();
}, function (error) {
test.equals(404, error);
test.done();
});
// then - matching request but also cleared
sendRequest("GET", "http://localhost:1080/somePathTwo")
.then(function (response) {
test.equal(response.statusCode, 404);
}, function (error) {
test.ok(false, error);
});
});
});
});
});
// end
test.done();
});
}

@@ -439,0 +572,0 @@ };

@@ -5,219 +5,386 @@ (function () {

var mockServer = require('../../'),
mockServerClient = mockServer.mockServerClient,
proxyClient = mockServer.proxyClient,
Q = require('q'),
request = require('request'),
sendRequest = function (method, url, body) {
var deferred = Q.defer();
var options = {
method: method,
url: url,
body: body
};
request(options, function (error, response) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(response);
}
var mockServer = require('../../');
var mockServerClient = mockServer.mockServerClient;
var proxyClient = mockServer.proxyClient;
var Q = require('q');
var http = require('http');
function sendRequestViaProxy(destinationUrl, jsonBody) {
var deferred = Q.defer();
var body = (typeof jsonBody === "string" ? jsonBody : JSON.stringify(jsonBody || ""));
var options = {
method: "POST",
host: "localhost",
port: 1090,
headers: {
Host: "localhost:1080"
},
path: destinationUrl
};
var callback = function (response) {
var body = '';
if (response.statusCode === 400 || response.statusCode === 404) {
deferred.reject(response.statusCode);
}
response.on('data', function (chunk) {
body += chunk;
});
return deferred.promise;
response.on('end', function () {
deferred.resolve({
statusCode: response.statusCode,
headers: response.headers,
body: body
});
});
};
var req = http.request(options, callback);
req.write(body);
req.end();
return deferred.promise;
}
exports.mock_server_started = {
setUp: function (callback) {
mockServerClient("localhost", 1080).reset();
proxyClient("localhost", 1090).reset();
callback();
mockServerClient("localhost", 1080).reset().then(function () {
proxyClient("localhost", 1090).reset().then(function () {
callback();
}, function (error) {
throw 'Failed with error ' + JSON.stringify(error);
});
}, function (error) {
throw 'Failed with error ' + JSON.stringify(error);
});
},
'should verify exact number of requests have been sent': function (test) {
// given
// given - a client
var client = proxyClient("localhost", 1090);
mockServerClient("localhost", 1080).mockSimpleResponse('/somePath', { name: 'value' }, 203);
mockServerClient("localhost", 1080).mockSimpleResponse('/somePath', { name: 'value' }, 203);
sendRequest("POST", "http://localhost:1080/somePath", "someBody")
// and - a request
sendRequestViaProxy("http://localhost:1080/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 203);
test.ok(false, "expecting 404 response");
}, function (error) {
test.ok(false, error);
test.equal(error, 404);
}).then(function () {
// and - another request
sendRequestViaProxy("http://localhost:1080/somePath", "someBody")
.then(function (response) {
test.ok(false, "expecting 404 response");
}, function (error) {
test.equal(error, 404);
}).then(function () {
// and - a verification that passes
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 2, true).then(function () {
test.done();
}, function () {
test.ok(false, "verification should pass");
test.done();
});
});
});
sendRequest("POST", "http://localhost:1080/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 203);
}, function (error) {
test.ok(false, error);
});
// when
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 2, true);
},
'should verify at least a number of requests have been sent': function (test) {
// given
// given - a client
var client = proxyClient("localhost", 1090);
sendRequest("POST", "http://localhost:1080/somePath", "someBody")
// and - a request
sendRequestViaProxy("http://localhost:1080/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 404);
test.ok(false, "expecting 404 response");
}, function (error) {
test.ok(false, error);
test.equal(error, 404);
}).then(function () {
// and - another request
sendRequestViaProxy("http://localhost:1080/someOtherPath", "someBody")
.then(function (response) {
test.ok(false, "expecting 404 response");
}, function (error) {
test.equal(error, 404);
}).then(function () {
// and - a verification that passes
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 1).then(function () {
test.done();
}, function () {
test.ok(false, "verification should pass");
test.done();
});
});
});
sendRequest("POST", "http://localhost:1080/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 404);
}, function (error) {
test.ok(false, error);
});
// when
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 1);
},
'should fail when no requests have been sent': function (test) {
// given
// given - a client
var client = proxyClient("localhost", 1090);
sendRequest("POST", "http://localhost:1080/somePath", "someBody")
// and - a request
sendRequestViaProxy("http://localhost:1080/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 404);
test.ok(false, "expecting 404 response");
}, function (error) {
test.ok(false, error);
test.equal(error, 404);
}).then(function () {
// when - a verification that should fail
client.verify(
{
'path': '/someOtherPath'
}, 1).then(function () {
test.ok(false, "verification should have failed");
}, function (message) {
test.equals(message, "Request not found at least once, expected:<{\n" +
" \"path\" : \"/someOtherPath\"\n" +
"}> but was:<{\n" +
" \"method\" : \"POST\",\n" +
" \"path\" : \"/somePath\",\n" +
" \"body\" : \"someBody\",\n" +
" \"headers\" : [ {\n" +
" \"name\" : \"Host\",\n" +
" \"values\" : [ \"localhost:1080\" ]\n" +
" }, {\n" +
" \"name\" : \"Content-Length\",\n" +
" \"values\" : [ \"8\" ]\n" +
" } ]\n" +
"}>");
}).then(function () {
test.done();
});
});
// when
test.throws(function () {
client.verify(
{
'path': '/someOtherPath'
}, 1);
});
},
'should fail when not enough exact requests have been sent': function (test) {
// given
// given - a client
var client = proxyClient("localhost", 1090);
sendRequest("POST", "http://localhost:1080/somePath", "someBody")
// and - a request
sendRequestViaProxy("http://localhost:1080/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 404);
test.ok(false, "expecting 404 response");
}, function (error) {
test.ok(false, error);
test.equal(error, 404);
}).then(function () {
// when - a verification that should fail
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 2, true).then(function () {
test.ok(false, "verification should have failed");
}, function (message) {
test.equals(message, "Request not found exactly 2 times, expected:<{\n" +
" \"method\" : \"POST\",\n" +
" \"path\" : \"/somePath\",\n" +
" \"body\" : \"someBody\"\n" +
"}> but was:<{\n" +
" \"method\" : \"POST\",\n" +
" \"path\" : \"/somePath\",\n" +
" \"body\" : \"someBody\",\n" +
" \"headers\" : [ {\n" +
" \"name\" : \"Host\",\n" +
" \"values\" : [ \"localhost:1080\" ]\n" +
" }, {\n" +
" \"name\" : \"Content-Length\",\n" +
" \"values\" : [ \"8\" ]\n" +
" } ]\n" +
"}>");
}).then(function () {
test.done();
});
});
// when
test.throws(function () {
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 2, true);
});
},
'should fail when not enough at least requests have been sent': function (test) {
// given
// given - a client
var client = proxyClient("localhost", 1090);
sendRequest("POST", "http://localhost:1080/somePath", "someBody")
// and - a request
sendRequestViaProxy("http://localhost:1080/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 404);
test.ok(false, "expecting 404 response");
}, function (error) {
test.ok(false, error);
test.equal(error, 404);
}).then(function () {
// when - a verification that should fail
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 2).then(function () {
test.ok(false, "verification should have failed");
}, function (message) {
test.equals(message, "Request not found at least 2 times, expected:<{\n" +
" \"method\" : \"POST\",\n" +
" \"path\" : \"/somePath\",\n" +
" \"body\" : \"someBody\"\n" +
"}> but was:<{\n" +
" \"method\" : \"POST\",\n" +
" \"path\" : \"/somePath\",\n" +
" \"body\" : \"someBody\",\n" +
" \"headers\" : [ {\n" +
" \"name\" : \"Host\",\n" +
" \"values\" : [ \"localhost:1080\" ]\n" +
" }, {\n" +
" \"name\" : \"Content-Length\",\n" +
" \"values\" : [ \"8\" ]\n" +
" } ]\n" +
"}>");
}).then(function () {
test.done();
});
});
// when
test.throws(function () {
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 2);
});
},
'should clear proxy': function (test) {
// given
// given - a client
var client = proxyClient("localhost", 1090);
sendRequest("POST", "http://localhost:1080/somePath", "someBody")
// and - a request
sendRequestViaProxy("http://localhost:1080/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 404);
test.ok(false, "expecting 404 response");
}, function (error) {
test.ok(false, error);
});
sendRequest("POST", "http://localhost:1080/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 404);
}, function (error) {
test.ok(false, error);
});
test.equal(error, 404);
}).then(function () {
// and - another request
sendRequestViaProxy("http://localhost:1080/someOtherPath", "someBody")
.then(function (response) {
test.ok(false, "expecting 404 response");
}, function (error) {
test.equal(error, 404);
}).then(function () {
// then
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 1);
// when
client.clear('/somePath');
// and - a verification that passes
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 1).then(function () {
// when - matching requests cleared
client.clear('/somePath').then(function () {
// then
test.throws(function () {
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 1);
});
// then - the verification should fail requests that were cleared
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 1).then(function () {
test.ok(false, "verification should have failed");
}, function (message) {
test.equals(message, "Request not found at least once, expected:<{\n" +
" \"method\" : \"POST\",\n" +
" \"path\" : \"/somePath\",\n" +
" \"body\" : \"someBody\"\n" +
"}> but was:<{\n" +
" \"method\" : \"POST\",\n" +
" \"path\" : \"/someOtherPath\",\n" +
" \"body\" : \"someBody\",\n" +
" \"headers\" : [ {\n" +
" \"name\" : \"Host\",\n" +
" \"values\" : [ \"localhost:1080\" ]\n" +
" }, {\n" +
" \"name\" : \"Content-Length\",\n" +
" \"values\" : [ \"8\" ]\n" +
" } ]\n" +
"}>");
}).then(function () {
// then - the verification should pass for other requests
client.verify(
{
'method': 'POST',
'path': '/someOtherPath',
'body': 'someBody'
}, 1).then(function () {
test.done();
}, function () {
test.ok(false, "verification should have passed");
test.done();
});
});
}, function () {
test.ok(false, "client should not fail when resetting");
});
}, function () {
test.ok(false, "verification should pass");
});
});
});
},
'should reset proxy': function (test) {
// given
// given - a client
var client = proxyClient("localhost", 1090);
sendRequest("POST", "http://localhost:1080/somePath", "someBody")
// and - a request
sendRequestViaProxy("http://localhost:1080/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 404);
test.ok(false, "expecting 404 response");
}, function (error) {
test.ok(false, error);
});
sendRequest("POST", "http://localhost:1080/somePath", "someBody")
.then(function (response) {
test.equal(response.statusCode, 404);
}, function (error) {
test.ok(false, error);
});
test.equal(error, 404);
}).then(function () {
// and - another request
sendRequestViaProxy("http://localhost:1080/somePath", "someBody")
.then(function (response) {
test.ok(false, "expecting 404 response");
}, function (error) {
test.equal(error, 404);
}).then(function () {
// then
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 1);
// and - a verification that passes
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 1).then(function () {
// when - all recorded requests reset
client.reset().then(function () {
// when
client.reset();
// then
test.throws(function () {
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 1);
});
// then - the verification should fail
client.verify(
{
'method': 'POST',
'path': '/somePath',
'body': 'someBody'
}, 1).then(function () {
test.ok(false, "verification should have failed");
}, function (message) {
test.equals(message, "Request not found at least once, expected:<{\n" +
" \"method\" : \"POST\",\n" +
" \"path\" : \"/somePath\",\n" +
" \"body\" : \"someBody\"\n" +
"}> but was:<>");
}).then(function () {
test.done();
});
}, function () {
test.ok(false, "client should not fail when resetting");
});
}, function () {
test.ok(false, "verification should pass");
});
});
});
}

@@ -224,0 +391,0 @@ };

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc