node.js - How can this function be tested with vows? -


how can following function, intended add routes express.js app based on object hierarchy, tested using vows.js cleanly without breaking vows' separation of topic , vow?

var addroutes = function(routeobject, app, path) {      var httpverbs = ['get', 'post', 'put', 'delete'];      path = path || '';      for(var property in routeobject){         var routesadded = false;         (var verbindex in httpverbs) {             var verb = httpverbs[verbindex];             var completepath, handler;             if (property === verb) {                 if (typeof(routeobject[verb]) === 'function') {                     handler = routeobject[verb];                     completepath = path;                 } else {                     handler = routeobject[verb].handler;                     completepath = path + (routeobject[verb].params || '');                 }                 app[verb](completepath, handler);                 routesadded = true;             }         }         if (!routesadded) {             addroutes(routeobject[property], app, path + '/' + property);         }     } }; 

the test should confirm functions corresponding http verbs called on app object. e.g. if routeobject is:

{     tracker: {         message: {             get: {                 handler: function (req, res) {                     res.send("respond resource");                 }             }         }     }  } 

then

app.get('/tracker/message', function (req, res) {                             res.send("respond resource");                         }); 

should called.

something should work:

var assert = require('assert'),     request = require('request');  var verbs = ['get', 'post', 'put', 'delete'],     app = require('your/app.js'),     port = 8080;  // // route // var example = {   tracker: {     message: {       get: {         status: 200,         body: 'respond resource'       }     }   } };  function testroutes(suite, route, app, path) {   path = path || '/';   object.keys(route).foreach(function (key) {     if (~verbs.indexof(key)) {       return testroutes(suite, route[key], app, [path, key].join('/'));     }      var test = [key, path].join(' ');     suite[test] = {       topic: function () {         request({           method: key,           uri: 'http://localhost' + port + path         }, this.callback);       },       'should respond correctly': function (err, res, body) {         assert.isnull(err);         assert.equal(res.statuscode, route[key].status);         assert.equal(body, route[key].body);       }     }   });    return suite; }  var suite = vows.describe('api'); testroutes(suite, example, app); suite.export(module); 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -