mockserver-client
Advanced tools
Comparing version 1.0.16 to 2.0.0
@@ -110,3 +110,3 @@ /* | ||
grunt.loadNpmTasks('grunt-exec'); | ||
grunt.loadNpmTasks('mockserver-grunt'); | ||
grunt.loadNpmTasks('mockserver-node'); | ||
grunt.loadNpmTasks('grunt-contrib-jshint'); | ||
@@ -113,0 +113,0 @@ grunt.loadNpmTasks('grunt-contrib-clean'); |
@@ -23,7 +23,13 @@ /* | ||
return function () { | ||
sucess && sucess({ | ||
statusCode: this.status, | ||
description: this.status + " " + this.statusText, | ||
body: this.responseText | ||
}); | ||
if (error && this.status >= 400 && this.status < 600) { | ||
error({ | ||
statusCode: this.status, | ||
body: this.responseText | ||
}); | ||
} else { | ||
sucess && sucess({ | ||
statusCode: this.status, | ||
body: this.responseText | ||
}); | ||
} | ||
}; | ||
@@ -398,9 +404,14 @@ })(sucess, error)); | ||
} | ||
}).then(function (result) { | ||
if (result.statusCode !== 202) { | ||
error && error(result.body); | ||
} else { | ||
}).then( | ||
function () { | ||
sucess && sucess(); | ||
}, | ||
function (result) { | ||
if (result.statusCode !== 202) { | ||
error && error(result.body); | ||
} else { | ||
error && sucess(result); | ||
} | ||
} | ||
}); | ||
); | ||
} | ||
@@ -440,9 +451,14 @@ }; | ||
"httpRequests": requestSequence | ||
}).then(function (result) { | ||
if (result.statusCode !== 202) { | ||
error && error(result.body); | ||
} else { | ||
}).then( | ||
function () { | ||
sucess && sucess(); | ||
}, | ||
function (result) { | ||
if (result.statusCode !== 202) { | ||
error && error(result.body); | ||
} else { | ||
error && sucess(result); | ||
} | ||
} | ||
}); | ||
); | ||
} | ||
@@ -449,0 +465,0 @@ }; |
{ | ||
"name": "mockserver-client", | ||
"description": "A node client for the MockServer", | ||
"version": "1.0.16", | ||
"version": "2.0.0", | ||
"homepage": "https://github.com/jamesdbloom/mockserver", | ||
@@ -27,11 +27,11 @@ "author": { | ||
"grunt-contrib-nodeunit": "~1.0", | ||
"grunt-exec": "~2.0", | ||
"grunt-exec": "~3.0", | ||
"grunt-jasmine-runner": "~0.6", | ||
"grunt-karma": "~2.0", | ||
"grunt-webdriver-jasmine-runner": "~0.3", | ||
"jasmine-core": "~2.5", | ||
"jasmine-core": "~2.8", | ||
"karma": "~1.3", | ||
"karma-chrome-launcher": "~2.0", | ||
"karma-chrome-launcher": "~2.2", | ||
"karma-firefox-launcher": "~1.0", | ||
"karma-jasmine": "~1.0", | ||
"karma-jasmine": "~1.1", | ||
"karma-phantomjs-launcher": "~1.0", | ||
@@ -41,3 +41,3 @@ "karma-safari-launcher": "~1.0", | ||
"karma-xvfb-chrome-launcher": "0.0.1", | ||
"mockserver-grunt": "~1.0", | ||
"mockserver-node": "~2.1", | ||
"nodeunit": "~0.11" | ||
@@ -44,0 +44,0 @@ }, |
@@ -23,7 +23,13 @@ /* | ||
return function () { | ||
sucess && sucess({ | ||
statusCode: this.status, | ||
description: this.status + " " + this.statusText, | ||
body: this.responseText | ||
}); | ||
if (error && this.status >= 400 && this.status < 600) { | ||
error({ | ||
statusCode: this.status, | ||
body: this.responseText | ||
}); | ||
} else { | ||
sucess && sucess({ | ||
statusCode: this.status, | ||
body: this.responseText | ||
}); | ||
} | ||
}; | ||
@@ -88,9 +94,14 @@ })(sucess, error)); | ||
} | ||
}).then(function (result) { | ||
if (result.statusCode !== 202) { | ||
error && error(result.body); | ||
} else { | ||
}).then( | ||
function () { | ||
sucess && sucess(); | ||
}, | ||
function (result) { | ||
if (result.statusCode !== 202) { | ||
error && error(result.body); | ||
} else { | ||
error && sucess(result); | ||
} | ||
} | ||
}); | ||
); | ||
} | ||
@@ -128,9 +139,14 @@ }; | ||
"httpRequests": requestSequence | ||
}).then(function (result) { | ||
if (result.statusCode !== 202) { | ||
error && error(result.body); | ||
} else { | ||
}).then( | ||
function () { | ||
sucess && sucess(); | ||
}, | ||
function (result) { | ||
if (result.statusCode !== 202) { | ||
error && error(result.body); | ||
} else { | ||
error && sucess(result); | ||
} | ||
} | ||
}); | ||
); | ||
} | ||
@@ -137,0 +153,0 @@ }; |
173
README.md
@@ -33,4 +33,12 @@ # mockserver-client-node | ||
```js | ||
mockServerClient("localhost", 1080). | ||
mockSimpleResponse('/somePath', { name: 'value' }, 203); | ||
mockServerClient("localhost", 1080) | ||
.mockSimpleResponse('/somePath', { name: 'value' }, 203) | ||
.then( | ||
function(result) { | ||
// do something next | ||
}, | ||
function(error) { | ||
// handle error | ||
} | ||
); | ||
``` | ||
@@ -41,32 +49,41 @@ | ||
```js | ||
mockServerClient("localhost", 1080).mockAnyResponse( | ||
{ | ||
'httpRequest': { | ||
'method': 'POST', | ||
'path': '/somePath', | ||
'queryStringParameters': [ | ||
{ | ||
'name': 'test', | ||
'values': [ 'true' ] | ||
mockServerClient("localhost", 1080) | ||
.mockAnyResponse( | ||
{ | ||
'httpRequest': { | ||
'method': 'POST', | ||
'path': '/somePath', | ||
'queryStringParameters': [ | ||
{ | ||
'name': 'test', | ||
'values': [ 'true' ] | ||
} | ||
], | ||
'body': { | ||
'type': "STRING", | ||
'value': 'someBody' | ||
} | ||
], | ||
'body': { | ||
'type': "STRING", | ||
'value': 'someBody' | ||
}, | ||
'httpResponse': { | ||
'statusCode': 200, | ||
'body': JSON.stringify({ name: 'value' }), | ||
'delay': { | ||
'timeUnit': 'MILLISECONDS', | ||
'value': 250 | ||
} | ||
}, | ||
'times': { | ||
'remainingTimes': 1, | ||
'unlimited': false | ||
} | ||
}, | ||
'httpResponse': { | ||
'statusCode': 200, | ||
'body': JSON.stringify({ name: 'value' }), | ||
'delay': { | ||
'timeUnit': 'MILLISECONDS', | ||
'value': 250 | ||
} | ||
}, | ||
'times': { | ||
'remainingTimes': 1, | ||
'unlimited': false | ||
} | ||
} | ||
); | ||
) | ||
.then( | ||
function(result) { | ||
// do something next | ||
}, | ||
function(error) { | ||
// handle error | ||
} | ||
); | ||
``` | ||
@@ -81,9 +98,49 @@ | ||
```js | ||
mockServerClient("localhost", 1080).verify( | ||
{ | ||
'method': 'POST', | ||
'path': '/somePath', | ||
'body': 'someBody' | ||
}, 1, true); | ||
mockServerClient("localhost", 1080) | ||
.verify( | ||
{ | ||
'method': 'POST', | ||
'path': '/somePath', | ||
'body': 'someBody' | ||
}, | ||
1, true | ||
) | ||
.then( | ||
function() { | ||
// do something next | ||
}, | ||
function(failure) { | ||
// handle verification failure | ||
} | ||
); | ||
``` | ||
It is also possible to verify that sequences of requests were made in a specific order as follows: | ||
```js | ||
mockServerClient("localhost", 1080) | ||
.verifySequence( | ||
{ | ||
'method': 'POST', | ||
'path': '/somePathOne', | ||
'body': 'someBody' | ||
}, | ||
{ | ||
'method': 'GET', | ||
'path': '/somePathTwo' | ||
}, | ||
{ | ||
'method': 'GET', | ||
'path': '/somePathThree' | ||
} | ||
) | ||
.then( | ||
function() { | ||
// do something next | ||
}, | ||
function(failure) { | ||
// handle verification failure | ||
} | ||
); | ||
``` | ||
## Contributing | ||
@@ -93,26 +150,30 @@ In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/). | ||
## Release History | ||
* 2014-28-10 v0.0.1 Initial release | ||
* 2014-28-10 v0.0.2 Fixed module naming | ||
* 2014-28-10 v0.0.3 Fixing module loading | ||
* 2014-28-10 v0.0.4 Fixing badges | ||
* 2014-01-11 v1.0.0 Cleaned code & removed duplication | ||
* 2014-02-11 v1.0.1 Added wercker build process | ||
* 2014-21-11 v1.0.2 Fixed asynchronous errors | ||
* 2014-04-12 v1.0.3 Improved connection error handling | ||
* 2014-04-12 v1.0.4 Supporting protractor based promises | ||
* 2015-04-06 v1.0.5 Upgrading dependencies | ||
* 2015-06-02 v1.0.6 Improved clear and dumpToLog options | ||
* 2015-09-27 v1.0.7 Fixed error with query parameter handling | ||
* 2015-09-27 v1.0.8 Fixed documentation | ||
* 2015-09-28 v1.0.9 Fixed documentation again | ||
* 2015-10-11 v1.0.10 Added retrieve requests or expectations | ||
* 2016-09-27 v1.0.11 Updated dependencies | ||
* 2016-10-09 v1.0.12 Resolved issues with dependencies | ||
* 2017-04-30 v1.0.13 Added websocket (i.e. method callbacks) | ||
* 2017-05-03 v1.0.14 Backward compatibility for mockAnyResponse | ||
* 2017-05-03 v1.0.15 Improving promise logic for protractor | ||
* 2017-05-04 v1.0.16 Removed grunt peer dependencies | ||
Date | Version | Description | ||
:--------- |:------- |:--------------------------------------- | ||
2014-28-10 | v0.0.1 | Initial release | ||
2014-28-10 | v0.0.2 | Fixed module naming | ||
2014-28-10 | v0.0.3 | Fixing module loading | ||
2014-28-10 | v0.0.4 | Fixing badges | ||
2014-01-11 | v1.0.0 | Cleaned code & removed duplication | ||
2014-02-11 | v1.0.1 | Added wercker build process | ||
2014-21-11 | v1.0.2 | Fixed asynchronous errors | ||
2014-04-12 | v1.0.3 | Improved connection error handling | ||
2014-04-12 | v1.0.4 | Supporting protractor based promises | ||
2015-04-06 | v1.0.5 | Upgrading dependencies | ||
2015-06-02 | v1.0.6 | Improved clear and dumpToLog options | ||
2015-09-27 | v1.0.7 | Fixed error with query parameter handling | ||
2015-09-27 | v1.0.8 | Fixed documentation | ||
2015-09-28 | v1.0.9 | Fixed documentation again | ||
2015-10-11 | v1.0.10 | Added retrieve requests or expectations | ||
2016-09-27 | v1.0.11 | Updated dependencies | ||
2016-10-09 | v1.0.12 | Resolved issues with dependencies | ||
2017-04-30 | v1.0.13 | Added websocket (i.e. method callbacks) | ||
2017-05-03 | v1.0.14 | Backward compatibility for mockAnyResponse | ||
2017-05-03 | v1.0.15 | Improving promise logic for protractor | ||
2017-05-04 | v1.0.16 | Removed grunt peer dependencies | ||
2017-18-11 | v2.0.0 | Improved error handling for server validation | ||
--- | ||
Task submitted by [James D Bloom](http://blog.jamesdbloom.com) |
@@ -44,6 +44,2 @@ /* | ||
if (response.statusCode === 400 || response.statusCode === 404) { | ||
deferred.reject(response.statusCode); | ||
} | ||
response.on('data', function (chunk) { | ||
@@ -57,6 +53,13 @@ data += chunk; | ||
} else { | ||
deferred.resolve({ | ||
statusCode: response.statusCode, | ||
body: data | ||
}); | ||
if (response.statusCode >= 400 && response.statusCode < 600) { | ||
deferred.reject({ | ||
statusCode: response.statusCode, | ||
body: data | ||
}); | ||
} else { | ||
deferred.resolve({ | ||
statusCode: response.statusCode, | ||
body: data | ||
}); | ||
} | ||
} | ||
@@ -63,0 +66,0 @@ }); |
@@ -16,27 +16,29 @@ (function () { | ||
try { | ||
var body = (typeof jsonBody === "string" ? jsonBody : JSON.stringify(jsonBody || "")); | ||
var options = { | ||
method: method || "POST", | ||
host: "localhost", | ||
port: proxyPort, | ||
headers: { | ||
Host: "localhost:" + mockServerPort, | ||
Connection: "keep-alive" | ||
}, | ||
path: destinationUrl | ||
}; | ||
var body = (typeof jsonBody === "string" ? jsonBody : JSON.stringify(jsonBody || "")); | ||
var options = { | ||
method: method || "POST", | ||
host: "localhost", | ||
port: proxyPort, | ||
headers: { | ||
Host: "localhost:" + mockServerPort, | ||
Connection: "keep-alive" | ||
}, | ||
path: destinationUrl | ||
}; | ||
var callback = function (response) { | ||
var 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('data', function (chunk) { | ||
body += chunk; | ||
}); | ||
response.on('end', function () { | ||
response.on('end', function () { | ||
if (response.statusCode >= 400 && response.statusCode < 600) { | ||
deferred.reject({ | ||
statusCode: response.statusCode, | ||
headers: response.headers, | ||
body: body | ||
}); | ||
} else { | ||
deferred.resolve({ | ||
@@ -47,13 +49,11 @@ statusCode: response.statusCode, | ||
}); | ||
}); | ||
}; | ||
} | ||
}); | ||
}; | ||
var req = http.request(options, callback); | ||
if (options.method === "POST") { | ||
req.write(body); | ||
} | ||
req.end(); | ||
} catch (e) { | ||
deferred.reject(e); | ||
var req = http.request(options, callback); | ||
if (options.method === "POST") { | ||
req.write(body); | ||
} | ||
req.end(); | ||
@@ -85,3 +85,3 @@ return deferred.promise; | ||
}, function (error) { | ||
test.equal(error, 404); | ||
test.equal(error.statusCode, 404); | ||
@@ -94,3 +94,3 @@ // and - another request | ||
}, function (error) { | ||
test.equal(error, 404); | ||
test.equal(error.statusCode, 404); | ||
@@ -122,3 +122,3 @@ // and - a verification that passes | ||
}, function (error) { | ||
test.equal(error, 404); | ||
test.equal(error.statusCode, 404); | ||
@@ -131,3 +131,3 @@ // and - another request | ||
}, function (error) { | ||
test.equal(error, 404); | ||
test.equal(error.statusCode, 404); | ||
@@ -159,3 +159,3 @@ // and - a verification that passes | ||
}, function (error) { | ||
test.equal(error, 404); | ||
test.equal(error.statusCode, 404); | ||
@@ -180,2 +180,5 @@ // when - a verification that should fail | ||
" }, {\n" + | ||
" \"name\" : \"Connection\",\n" + | ||
" \"values\" : [ \"keep-alive\" ]\n" + | ||
" }, {\n" + | ||
" \"name\" : \"content-length\",\n" + | ||
@@ -203,3 +206,3 @@ " \"values\" : [ \"8\" ]\n" + | ||
}, function (error) { | ||
test.equal(error, 404); | ||
test.equal(error.statusCode, 404); | ||
@@ -228,2 +231,5 @@ // when - a verification that should fail | ||
" }, {\n" + | ||
" \"name\" : \"Connection\",\n" + | ||
" \"values\" : [ \"keep-alive\" ]\n" + | ||
" }, {\n" + | ||
" \"name\" : \"content-length\",\n" + | ||
@@ -250,3 +256,3 @@ " \"values\" : [ \"8\" ]\n" + | ||
}, function (error) { | ||
test.equal(error, 404); | ||
test.equal(error.statusCode, 404); | ||
@@ -275,2 +281,5 @@ // when - a verification that should fail | ||
" }, {\n" + | ||
" \"name\" : \"Connection\",\n" + | ||
" \"values\" : [ \"keep-alive\" ]\n" + | ||
" }, {\n" + | ||
" \"name\" : \"content-length\",\n" + | ||
@@ -295,4 +304,4 @@ " \"values\" : [ \"8\" ]\n" + | ||
test.done(); | ||
}, function (statusCode) { | ||
test.equal(statusCode, 404); | ||
}, function (error) { | ||
test.equal(error.statusCode, 404); | ||
sendRequestViaProxy("http://localhost:" + mockServerPort + "/two", undefined, "GET") | ||
@@ -302,4 +311,4 @@ .then(function () { | ||
test.done(); | ||
}, function (statusCode) { | ||
test.equal(statusCode, 404); | ||
}, function (error) { | ||
test.equal(error.statusCode, 404); | ||
sendRequestViaProxy("http://localhost:" + mockServerPort + "/three", undefined, "GET") | ||
@@ -309,4 +318,4 @@ .then(function () { | ||
test.done(); | ||
}, function (statusCode) { | ||
test.equal(statusCode, 404); | ||
}, function (error) { | ||
test.equal(error.statusCode, 404); | ||
@@ -348,4 +357,4 @@ // when | ||
test.done(); | ||
}, function (statusCode) { | ||
test.equal(statusCode, 404); | ||
}, function (error) { | ||
test.equal(error.statusCode, 404); | ||
@@ -356,4 +365,4 @@ sendRequestViaProxy("http://localhost:" + mockServerPort + "/two", undefined, "GET") | ||
test.done(); | ||
}, function (statusCode) { | ||
test.equal(statusCode, 404); | ||
}, function (error) { | ||
test.equal(error.statusCode, 404); | ||
@@ -364,4 +373,4 @@ sendRequestViaProxy("http://localhost:" + mockServerPort + "/three", undefined, "GET") | ||
test.done(); | ||
}, function (statusCode) { | ||
test.equal(statusCode, 404); | ||
}, function (error) { | ||
test.equal(error.statusCode, 404); | ||
@@ -425,3 +434,3 @@ // when - wrong order | ||
}, function (error) { | ||
test.equal(error, 404); | ||
test.equal(error.statusCode, 404); | ||
@@ -434,3 +443,3 @@ // and - another request | ||
}, function (error) { | ||
test.equal(error, 404); | ||
test.equal(error.statusCode, 404); | ||
@@ -469,2 +478,5 @@ // and - a verification that passes | ||
" }, {\n" + | ||
" \"name\" : \"Connection\",\n" + | ||
" \"values\" : [ \"keep-alive\" ]\n" + | ||
" }, {\n" + | ||
" \"name\" : \"content-length\",\n" + | ||
@@ -513,3 +525,3 @@ " \"values\" : [ \"8\" ]\n" + | ||
}, function (error) { | ||
test.equal(error, 404); | ||
test.equal(error.statusCode, 404); | ||
@@ -522,3 +534,3 @@ // and - another request | ||
}, function (error) { | ||
test.equal(error, 404); | ||
test.equal(error.statusCode, 404); | ||
@@ -559,2 +571,5 @@ // and - a verification that passes | ||
" }, {\n" + | ||
" \"name\" : \"Connection\",\n" + | ||
" \"values\" : [ \"keep-alive\" ]\n" + | ||
" }, {\n" + | ||
" \"name\" : \"content-length\",\n" + | ||
@@ -602,3 +617,3 @@ " \"values\" : [ \"8\" ]\n" + | ||
}, function (error) { | ||
test.equal(error, 404); | ||
test.equal(error.statusCode, 404); | ||
@@ -611,3 +626,3 @@ // and - another request | ||
}, function (error) { | ||
test.equal(error, 404); | ||
test.equal(error.statusCode, 404); | ||
@@ -650,2 +665,5 @@ // and - a verification that passes | ||
" }, {\n" + | ||
" \"name\" : \"Connection\",\n" + | ||
" \"values\" : [ \"keep-alive\" ]\n" + | ||
" }, {\n" + | ||
" \"name\" : \"content-length\",\n" + | ||
@@ -693,3 +711,3 @@ " \"values\" : [ \"8\" ]\n" + | ||
}, function (error) { | ||
test.equal(error, 404); | ||
test.equal(error.statusCode, 404); | ||
@@ -702,3 +720,3 @@ // and - another request | ||
}, function (error) { | ||
test.equal(error, 404); | ||
test.equal(error.statusCode, 404); | ||
@@ -766,4 +784,4 @@ // and - a verification that passes | ||
test.done(); | ||
}, function (statusCode) { | ||
test.equal(statusCode, 404); | ||
}, function (error) { | ||
test.equal(error.statusCode, 404); | ||
@@ -845,4 +863,4 @@ sendRequestViaProxy("http://localhost:" + mockServerPort + "/somePathTwo", undefined, "GET") | ||
test.done(); | ||
}, function (statusCode) { | ||
test.equal(statusCode, 404); | ||
}, function (error) { | ||
test.equal(error.statusCode, 404); | ||
@@ -849,0 +867,0 @@ sendRequestViaProxy("http://localhost:" + mockServerPort + "/somePathTwo", undefined, "GET") |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
542114
32
10503
176