Permalink
Browse files

changed JsonBody helper to onJson helper with req/res format.

  • Loading branch information...
cainus committed Mar 8, 2013
1 parent ed68f01 commit 50eced1d72f614fb15ca92114396f46d5925d1af
View
@@ -14,7 +14,7 @@ exports.AuthenticateHelper = require(helperDir + 'Authenticate');
exports.BasicAuthenticateHelper = require(helperDir + 'BasicAuthenticate');
exports.ObjectHelper = require(helperDir + 'Object');
exports.onBodyHelper = require(helperDir + 'onBody');
-exports.JsonBodyContextHelper = require(helperDir + 'JsonBody');
+exports.onJsonHelper = require(helperDir + 'onJson');
exports.FetchHelper = require(helperDir + 'Fetch');
exports.ContextFaker = require(dir + 'ContextFaker');
exports.Percolator = require(dir + 'Percolator');
@@ -27,7 +27,9 @@ var onBodyHelper = function(req, res, handler, cb){
return onBodyCB(null, body);
});
};
- cb();
+ if (cb){
+ cb();
+ }
};
@@ -1,5 +1,5 @@
/*
- This context helper just adds an onJson function to the
+ This helper just adds an onJson function to the
context and calls back when done.
As a last or only parameter, the onJson function takes a
@@ -16,8 +16,8 @@
var _ = require('underscore');
var JSV = require('JSV').JSV;
-var JsonBodyContextHelper = function($, handler, cb){
- $.req.onJson = function(){
+var onJsonHelper = function(req, res, handler, cb){
+ req.onJson = function(){
var args = _.toArray(arguments);
var schema, onBodyCB;
@@ -31,28 +31,28 @@ var JsonBodyContextHelper = function($, handler, cb){
onBodyCB = args[1];
break;
- default : throw "$.req.onJson() was called with the wrong number of properties.";
+ default : throw "req.onJson() was called with the wrong number of properties.";
}
var body = '';
- $.req.on('data', function(data){
+ req.on('data', function(data){
body += data;
});
- $.req.on('error', function(err){
+ req.on('error', function(err){
return onBodyCB(err, body);
});
- $.req.on('end', function(){
+ req.on('end', function(){
var obj;
try {
obj = JSON.parse(body);
} catch(ex) {
// if it's not valid JSON...
- return $.res.status.badRequest('invalid json.', body);
+ return res.status.badRequest('invalid json.', body);
}
if (!!schema){
var report = JSV.createEnvironment().validate(obj, schema);
if (report.errors.length > 0){
- return $.res.status.badRequest('json failed schema validation.', report.errors);
+ return res.status.badRequest('json failed schema validation.', report.errors);
}
}
@@ -64,7 +64,7 @@ var JsonBodyContextHelper = function($, handler, cb){
};
-module.exports = JsonBodyContextHelper;
+module.exports = onJsonHelper;
View
@@ -3,7 +3,7 @@ var EventEmitter = require('events').EventEmitter;
var index = require('../index');
var JsonResponder = index.JsonResponder;
var onBodyHelper = index.onBodyHelper;
-var jsonBodyContextHelper = index.JsonBodyContextHelper;
+var onJsonHelper = index.onJsonHelper;
var fetchHelper = index.FetchHelper;
var AuthenticateHelper = index.AuthenticateHelper;
var ObjectHelper = index.ObjectHelper;
@@ -108,7 +108,7 @@ Percolator.prototype.defaultContextHandler = function(context, handler, cb){
});
} else {
context.req.onBody = onBodyHelper(req, res, handler, function(){
- context.req.onJson = jsonBodyContextHelper(context, handler, function(){
+ context.req.onJson = onJsonHelper(req, res, handler, function(){
req.resume();
cb(null, context);
});
@@ -1,32 +1,35 @@
var should = require('should');
-var jbch = require('../index').JsonBodyContextHelper;
+var jbch = require('../index').onJsonHelper;
// TODO test bad schema, successful schema, json parse error
-describe("JsonBodyContextHelper", function(){
+describe("onJsonHelper", function(){
it ("sets onJson on the object", function(done){
- var $ = { req : {} };
+ var req = {};
+ var res = {};
var handler = {};
- jbch($, handler, function(){
- (typeof $.req.onJson).should.equal('function');
+ jbch(req, res, handler, function(){
+ (typeof req.onJson).should.equal('function');
done();
});
});
it ("throws an error when called with 2+ params", function(done){
var handler = {};
- var $ = { req : {} };
- jbch($, handler, function(){
+ var req = {};
+ var res = {};
+ jbch(req, res, handler, function(){
try {
- $.req.onJson({}, {}, function(err, obj){
+ req.onJson({}, {}, function(err, obj){
err.should.equal('some error');
});
} catch(ex){
- ex.should.equal('$.req.onJson() was called with the wrong number of properties.');
+ ex.should.equal('req.onJson() was called with the wrong number of properties.');
done();
}
});
});
it ("sets the error param when there's an error", function(done){
- var fakeReq = {
+ var res = {};
+ var req = {
on : function(type, cb){
switch(type){
case 'error' : return cb('some error');
@@ -35,16 +38,16 @@ describe("JsonBodyContextHelper", function(){
}
};
var handler = {};
- var $ = { req : fakeReq };
- jbch($, handler, function(){
- $.req.onJson(function(err, obj){
+ jbch(req, res, handler, function(){
+ req.onJson(function(err, obj){
err.should.equal('some error');
done();
});
});
});
it ("sets an obj param when successful", function(done){
- var fakeReq = {
+ var res = {};
+ var req = {
on : function(type, cb){
switch(type){
case 'data' : return cb('{"asdf":"asdf"}');
@@ -53,16 +56,15 @@ describe("JsonBodyContextHelper", function(){
}
};
var handler = {};
- var $ = { req : fakeReq };
- jbch($, handler, function(){
- $.req.onJson(function(err, obj){
+ jbch(req, res, handler, function(){
+ req.onJson(function(err, obj){
obj.should.eql({asdf:"asdf"});
done();
});
});
});
it ("responds with an error if json doesn't parse", function(done){
- var fakeReq = {
+ var req = {
on : function(type, cb){
switch(type){
case 'data' : return cb('{"age":37,}');
@@ -71,19 +73,17 @@ describe("JsonBodyContextHelper", function(){
}
};
var handler = {};
- var $ = { req : fakeReq,
- res : {
+ var res = {
status : {
badRequest : function(message, detail){
message.should.equal('invalid json.');
detail.should.equal('{"age":37,}');
done();
}
}
- }
};
- jbch($, handler, function(){
- $.req.onJson(function(err, obj){
+ jbch(req, res, handler, function(){
+ req.onJson(function(err, obj){
should.fail('should not get here');
});
});
@@ -101,7 +101,7 @@ describe("JsonBodyContextHelper", function(){
},
"additionalProperties" : false
};
- var fakeReq = {
+ var req = {
on : function(type, cb){
switch(type){
case 'data' : return cb('{"age":37, "name":"GDizzle", "wrong" : "wrong"}');
@@ -115,8 +115,7 @@ describe("JsonBodyContextHelper", function(){
}
};
var handler = {};
- var $ = { req : fakeReq,
- res : {
+ var res = {
status : {
badRequest : function(message, detail){
message.should.equal('json failed schema validation.');
@@ -131,10 +130,9 @@ describe("JsonBodyContextHelper", function(){
done();
}
}
- }
};
- jbch($, handler, function(){
- $.req.onJson(schema, function(err, obj){
+ jbch(req, res, handler, function(){
+ req.onJson(schema, function(err, obj){
should.fail('should not get here');
});
});
@@ -150,7 +148,7 @@ describe("JsonBodyContextHelper", function(){
}
}
};
- var fakeReq = {
+ var req = {
on : function(type, cb){
switch(type){
case 'data' : return cb('{"age":"37", "name":"GDizzle"}');
@@ -160,8 +158,7 @@ describe("JsonBodyContextHelper", function(){
}
};
var handler = {};
- var $ = { req : fakeReq,
- res : {
+ var res = {
status : {
badRequest : function(message, detail){
message.should.equal('json failed schema validation.');
@@ -175,10 +172,9 @@ describe("JsonBodyContextHelper", function(){
done();
}
}
- }
};
- jbch($, handler, function(){
- $.req.onJson(schema, function(err, obj){
+ jbch(req, res, handler, function(){
+ req.onJson(schema, function(err, obj){
should.fail('should not get here');
});
});
@@ -37,9 +37,9 @@ module.exports = new CRUDCollection({
cb(null, $.req.app.teas, 'name');
},
- fetch : function($, cb){
- var id = $.req.uri.child();
- var row = $.req.app.teas[id];
+ fetch : function(req, res, cb){
+ var id = req.uri.child();
+ var row = req.app.teas[id];
console.log("row was: ", row);
if (!!row){
cb(null, row);

0 comments on commit 50eced1

Please sign in to comment.