Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updates to be able to use whisper for integration testing

  • Loading branch information...
commit 891ee66cb687227a29db56dcdafbc439000dfd92 1 parent eba2382
Jim Snodgrass authored
Showing with 80 additions and 67 deletions.
  1. +43 −30 index.js
  2. +37 −37 test.js
View
73 index.js
@@ -7,7 +7,7 @@
* Returns the response from the controller using the supplied callback
*
* @author Jim Snodgrass jim@skookum.com
- *
+ *
*/
@@ -24,12 +24,12 @@ function Whisper() {}
Whisper.prototype.init = function(server, options) {
this.options = options || {};
this.allroutes = server.routes.routes;
-}
+};
/**
*
- */
+ */
Whisper.prototype.send = function(requests, callback) {
var self = this;
@@ -42,13 +42,13 @@ Whisper.prototype.send = function(requests, callback) {
async.forEach(requests, processRequest, function(err) {
if (err) {
- callback(err)
+ callback(err);
}
else {
- // send back results
+ // send back results
callback(null, results);
}
- })
+ });
// recursive function to loop through results
@@ -74,15 +74,15 @@ Whisper.prototype.send = function(requests, callback) {
// call done
done();
- })
+ });
}
}
// only one request
else {
- this.makeRequest(requests, callback)
+ this.makeRequest(requests, callback);
}
-}
+};
@@ -115,7 +115,15 @@ Whisper.prototype.makeRequest = function(data, callback) {
// fill out req params, body, query
var req = {};
+
+ // if req user supplied add to request
+ if (data.user) req.user = data.user;
+ // req flash placeholder
+ req.flash = function(type, message){
+ // console.log(type, message);
+ };
+
// req.query
req.query = {};
data.path.replace(
@@ -124,16 +132,16 @@ Whisper.prototype.makeRequest = function(data, callback) {
);
// req.body
- if (typeof data.body == "string" && data.body != "") {
+ if (typeof data.body == "string" && data.body !== "") {
try {
req.body = JSON.parse(data.body);
}
catch (e) {
- return callback("Error parsing JSON String: " + e)
+ return callback("Error parsing JSON String: " + e);
}
}
else {
- req.body = data.body;
+ req.body = data.body;
}
// req.params
@@ -148,38 +156,43 @@ Whisper.prototype.makeRequest = function(data, callback) {
// res object
var res = {
- send: function(props){
- callback(null, props);
+ send: function(props, code){
+ callback(null, props, code);
},
- render: function() {
- callback(null, {})
- }
- }
+ render: function(view, props) {
+ callback(null, 'render', view, props);
+ },
+ redirect: function(url) {
+ callback(null, 'redirect', url);
+ }
+ };
// middleware iterator counter
var currentMiddleware = 0;
+ var route_middleware = route.middleware || route.callbacks;
+ var route_callback = route.callback || route_middleware[route_middleware.length - 1];
// start middleware iterations
nextReq();
// recursive function to move through middleware until final callback
function nextReq() {
-
+
// all middleware has been processed, proceed to final callback
- if (currentMiddleware === route.middleware.length) {
+ if (currentMiddleware === route_middleware.length) {
// call controller with alternate res.send function
- route.callback(req, res)
+ route_callback(req, res);
}
// run current middleware function
else {
// pass request through next route middlewar
- route.middleware[currentMiddleware](req, res, function(){
- currentMiddleware++;
- nextReq();
- })
+ route_middleware[currentMiddleware](req, res, function(){
+ currentMiddleware++;
+ nextReq();
+ });
}
}
}
@@ -189,8 +202,8 @@ Whisper.prototype.makeRequest = function(data, callback) {
callback("Route and Path splits don't match");
}
}
- })
-}
+ });
+};
/**
@@ -215,13 +228,13 @@ Whisper.prototype.findRoute = function(method, path, callback) {
}
}
- if (route == null) {
+ if (route === null) {
//console.log("couldn't find -> " + method + "::" + path)
//console.log(routes);
}
callback(route);
-}
+};
-exports = module.exports = new Whisper()
+exports = module.exports = new Whisper();
View
74 test.js
@@ -7,39 +7,39 @@
*/
var vows = require('vows'),
- assert = require('assert')
+ assert = require('assert');
-var testReturn = "test return"
+var testReturn = "test return";
-var server = {
+var server = {
routes: {
routes : {
get: [
- { callback: function(req, res){res.send(testReturn)},
+ { callback: function(req, res){res.send(testReturn);},
path: '/route/check',
method: 'get',
regexp: /^\/route\/check\/?$/i,
- middleware: [ function(req, res, next){next()}, function(req, res, next){next()} ] }
+ middleware: [ function(req, res, next){next();}, function(req, res, next){next();} ] }
]
- }
- }
-}
+ }
+ }
+};
// initialize whisper module
-require('./index.js').init(server)
+require('./index.js').init(server);
// define whisper object
-var Whisper = require('./index.js')
+var Whisper = require('./index.js');
// setup tests
vows.describe('whisper module test').addBatch({
'make single request through makeRequest method': {
topic: function() {
- var request = {method: 'get', path: '/route/check', body: null}
- Whisper.makeRequest(request, this.callback)
+ var request = {method: 'get', path: '/route/check', body: null};
+ Whisper.makeRequest(request, this.callback);
},
'should not be any errors': function(err, result) {
- assert.isNull(err)
+ assert.isNull(err);
},
'return should match setup server route': function(err, result) {
assert.equal(result, testReturn);
@@ -47,21 +47,21 @@ vows.describe('whisper module test').addBatch({
},
'send single request with wrong path': {
topic: function() {
- var request = {method: 'get', path: '/wrong/path', body: null}
- Whisper.makeRequest(request, this.callback)
+ var request = {method: 'get', path: '/wrong/path', body: null};
+ Whisper.makeRequest(request, this.callback);
},
'should be an error': function(err, result) {
- assert.isNotNull(err)
+ assert.isNotNull(err);
}
}
}).addBatch({
'send bundle request with one item NOT in array': {
topic: function() {
- var request = {method: 'get', path: '/route/check', body: null}
- Whisper.send(request, this.callback)
+ var request = {method: 'get', path: '/route/check', body: null};
+ Whisper.send(request, this.callback);
},
'should not be any errors': function(err, result) {
- assert.isNull(err)
+ assert.isNull(err);
},
'return should match setup server route': function(err, result) {
assert.equal(result, testReturn);
@@ -69,55 +69,55 @@ vows.describe('whisper module test').addBatch({
},
'send bundle request with invalid json in body': {
topic: function() {
- var request = [{method: 'get', path: '/route/check', body: "shouldn't work"}]
- Whisper.send(request, this.callback)
+ var request = [{method: 'get', path: '/route/check', body: "shouldn't work"}];
+ Whisper.send(request, this.callback);
},
'should return error': function(err, result) {
- assert.equal(result[0].indexOf("Error") > -1, true)
+ assert.equal(result[0].indexOf("Error") > -1, true);
}
},
'send bundle request with one item in array': {
topic: function() {
- var request = [{method: 'get', path: '/route/check', body: null, sequence:0}]
- Whisper.send(request, this.callback)
+ var request = [{method: 'get', path: '/route/check', body: null, sequence:0}];
+ Whisper.send(request, this.callback);
},
'result should be object with sequence property': function(err, result) {
- assert.equal(result[0], testReturn)
+ assert.equal(result[0], testReturn);
}
},
'send bundle request with two items in array': {
topic: function() {
var request = [{method: 'get', path: '/route/check', body: null, sequence:0},
- {method: 'get', path: '/wrong/path', body: null, sequence:1}]
- Whisper.send(request, this.callback)
+ {method: 'get', path: '/wrong/path', body: null, sequence:1}];
+ Whisper.send(request, this.callback);
},
'check first item in result object': function(err, result) {
- assert.equal(result[0], testReturn)
+ assert.equal(result[0], testReturn);
},
'check second item in result object': function(err, result) {
- assert.notEqual(result[1], testReturn)
+ assert.notEqual(result[1], testReturn);
}
},
'send bundle request with one item in array with no sequence property': {
topic: function() {
- var request = [{method: 'get', path: '/route/check', body: null}]
- Whisper.send(request, this.callback)
+ var request = [{method: 'get', path: '/route/check', body: null}];
+ Whisper.send(request, this.callback);
},
'result should be object with sequence property': function(err, result) {
- assert.equal(result[0], testReturn)
+ assert.equal(result[0], testReturn);
}
},
'send bundle request with two items in array with no sequence property': {
topic: function() {
var request = [{method: 'get', path: '/route/check', body: null},
- {method: 'get', path: '/wrong/path', body: null}]
- Whisper.send(request, this.callback)
+ {method: 'get', path: '/wrong/path', body: null}];
+ Whisper.send(request, this.callback);
},
'check first item in result object': function(err, result) {
- assert.equal(result[0], testReturn)
+ assert.equal(result[0], testReturn);
},
'check second item in result object': function(err, result) {
- assert.notEqual(result[1], testReturn)
+ assert.notEqual(result[1], testReturn);
}
}
-}).export(module);
+})['export'](module);
Please sign in to comment.
Something went wrong with that request. Please try again.