Skip to content

Commit

Permalink
initial checkin of http module
Browse files Browse the repository at this point in the history
  • Loading branch information
jchris committed Jan 6, 2013
1 parent 83e9409 commit 244b959
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 17 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# hoax

ReSTful JSON client with path currying by pax.
Couch client using pax for path currying and request for HTTP.

## Getting Started
Install the module with: `npm install hoax`
Expand All @@ -23,5 +23,5 @@ In lieu of a formal styleguide, take care to maintain the existing coding style.
_(Nothing yet)_

## License
Copyright (c) 2013 Chris Anderson
Licensed under the Apache2 license.
Copyright (c) 2013 Chris Anderson
Licensed under the Apache license.
77 changes: 74 additions & 3 deletions lib/hoax.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,80 @@
* https://github.com/jchris/hoax
*
* Copyright (c) 2013 Chris Anderson
* Licensed under the Apache2 license.
* Licensed under the Apache license.
*/
var pax = require("pax"),
request = require("request"),
jreq = request.defaults({json:true});

exports.awesome = function() {
return 'awesome';
function makeHoaxCallback(cb) {
return function(err, res, body){
if (err) {
cb(err, res, body);
} else {
if (res.statusCode >= 400) {
cb(body || res.statusCode, res);
} else {
cb(null, body);
}
}
};
}

function callPaxOrArgs(myPax, path) {
// if path is array and last is query, couchify it
if (myPax.uri) {
return myPax.uri(path);
} else {
return myPax(path);
}
}

function processArguments(myPax, urlOrOpts, cb) {
if (urlOrOpts.uri || urlOrOpts.url) {
// it's options
urlOrOpts.uri = callPaxOrArgs(myPax, (urlOrOpts.uri || urlOrOpts.url));
return [urlOrOpts, cb];
} else if (typeof urlOrOpts === 'function') {
return [myPax, urlOrOpts];
} else {
return [callPaxOrArgs(myPax, urlOrOpts), cb];
}
}

function makeHoax(myPax, verb) {
var newHoax = function(url, cb) {
var args = processArguments(myPax, url, cb);
if (args[1]) {
if (verb) {
return jreq[verb](args[0].toString(), makeHoaxCallback(args[1]));
} else {
return jreq(args[0].toString(), makeHoaxCallback(args[1]));
}
} else {
return makeHoax(args[0], verb);
}
};
if (!verb) {
"get put post head del".split(" ").forEach(function(v){
newHoax[v] = makeHoax(myPax, v);
});
}
return newHoax;
}

var hoaxPax = pax();

hoaxPax.getQuery = function(params) {
params = JSON.parse(JSON.stringify(params));
var key, keys = ["key", "startkey", "endkey", "start_key", "end_key"];
for (var i = 0; i < keys.length; i++) {
key = keys[i];
if (params[key]) {
params[key] = JSON.stringify(params[key]);
}
}
return params;
};

var Hoax = module.exports = makeHoax(hoaxPax());
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hoax",
"description": "ReSTful JSON client with path currying by pax.",
"version": "0.1.0",
"description": "Couch client using pax for path currying and request for HTTP.",
"version": "0.2.0",
"homepage": "https://github.com/jchris/hoax",
"author": {
"name": "Chris Anderson",
Expand All @@ -17,8 +17,8 @@
},
"licenses": [
{
"type": "Apache2",
"url": "https://github.com/jchris/hoax/blob/master/LICENSE-Apache2"
"type": "Apache",
"url": "https://github.com/jchris/hoax/blob/master/LICENSE-Apache"
}
],
"main": "lib/hoax",
Expand All @@ -33,4 +33,4 @@
"grunt": "~0.3.6"
},
"keywords": []
}
}
191 changes: 185 additions & 6 deletions test/hoax_test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,193 @@
var hoax = require('../lib/hoax.js');
var hoax = require('../lib/hoax.js'),
request = require("request");

exports['awesome'] = {
var http = require("http"), url = require("url");

var handlers = {};

var testServer = function() {
http.createServer(function(req, res){
// your custom error-handling logic:
function error(status, err) {
res.statusCode = status || 500;
res.end(err.toString());
}
var path = url.parse(req.url).pathname;
console.log("test server", req.method, req.url);
if (handlers[path]) {
handlers[path](req, res);
} else {
error(404, "no handler for "+path);
}
}).listen(3001);
};

testServer();

exports['/awesome'] = {
setUp: function(done) {
// setup here
handlers['/awesome'] = function(req, res) {
res.statusCode = 200;
res.end(JSON.stringify({awesome:true}));
};
handlers['/very/awesome'] = function(req, res) {
res.statusCode = 200;
res.end(JSON.stringify({awesome:true, method : req.method}));
};
handlers['/very/awesome/coat'] = function(req, res) {
res.statusCode = 200;
res.end(JSON.stringify({coat:true, method : req.method}));
};
done();
},
'200 get': function(test) {
test.expect(2);
// tests here
hoax("http://localhost:3001/awesome", function(err, json){
// console.log(ok.statusCode, body);
test.equal(err, null);
test.equal(json.awesome, true, 'should be awesome.');
test.done();
});
},
'200 array' : function(test) {
// test.expect()
hoax(["http://localhost:3001/","very","awesome"], function(err, json){
test.equal(err, null);
test.equal(json.awesome, true, 'should be awesome.');
test.equal(json.method, 'GET', 'should be get.');
test.done();
});
},
'200 put' : function(test) {
// test.expect()
hoax.put("http://localhost:3001/very/awesome", function(err, json){
test.equal(err, null);
test.equal(json.awesome, true, 'should be awesome.');
test.equal(json.method, 'PUT', 'should be put.');
test.done();
});
},
'200 array put' : function(test) {
// test.expect()
hoax.put(["http://localhost:3001/","very","awesome"], function(err, json){
test.equal(err, null);
test.equal(json.awesome, true, 'should be awesome.');
test.equal(json.method, 'PUT', 'should be put.');
test.done();
});
},
'200 post' : function(test) {
test.expect(3);
hoax.post("http://localhost:3001/very/awesome", function(err, json){
test.equal(err, null);
test.equal(json.awesome, true, 'should be awesome.');
test.equal(json.method, 'POST', 'should be put.');
test.done();
});
},
'200 array curry' : function(test) {
// test.expect()
var host = hoax("http://localhost:3001/"),
resource = host(["very","awesome"]);
resource("coat", function(err, json){
test.equal(err, null);
test.equal(json.coat, true, 'should be coat.');
test.equal(json.method, 'GET', 'should be get.');
test.done();
});
},
'200 array no path' : function(test) {
// test.expect()
var host = hoax("http://localhost:3001/"),
resource = host(["very","awesome"]);
resource(function(err, json){
test.equal(err, null);
test.equal(json.awesome, true, 'should be awesome.');
test.equal(json.method, 'GET', 'should be get.');
test.done();
});
},
'200 array curry put' : function(test) {
// test.expect()
var host = hoax("http://localhost:3001/"),
resource = host(["very","awesome"]);
resource.put(function(err, json){
test.equal(err, null);
test.equal(json.awesome, true, 'should be awesome.');
test.equal(json.method, 'PUT', 'should be put.');
test.done();
});
},
'put curry' : function(test) {
// test.expect()
var host = hoax("http://localhost:3001/"),
resource = host.put(["very","awesome"]);
resource("coat",function(err, json){
test.equal(err, null);
test.equal(json.coat, true, 'should be awesome.');
test.equal(json.method, 'PUT', 'should be put.');
test.done();
});
}
};

var query = hoax("http://localhost:3001/query");
exports['/query'] = {
setUp: function(done) {
// setup here
handlers['/query'] = function(req, res) {
res.statusCode = 200;
res.end(JSON.stringify({url:req.url, method : req.method}));
};
done();
},
'get': function(test) {
// test.expect(2);
// tests here
query({myquery:"exciting"}, function(err, json){
// console.log(ok.statusCode, body);
test.equal(err, null);
test.ok(json.url, 'should have query');
// test.ok(json.query.myquery, 'should have query');
test.deepEqual(json.url, "/query?myquery=exciting", 'should be "exciting".');
test.done();
});
},
'get startkey etc': function(test) {
// test.expect(2);
// tests here

query({startkey:[1,2], endkey:1, key:"ok",other:"ok"}, function(err, json){
// console.log(ok.statusCode, body);
test.equal(err, null);
test.ok(json.url, 'should have query');
// test.ok(json.query.myquery, 'should have query');
test.deepEqual(json.url, "/query?startkey=%5B1%2C2%5D&endkey=1&key=%22ok%22&other=ok", 'should be "[1,2]".');
test.done();
});
}
};

exports['/error'] = {
setUp: function(done) {
// setup here
handlers['/error'] = function(req, res) {
res.statusCode = 404;
res.end(JSON.stringify({error:true}));
};
done();
},
'no args': function(test) {
test.expect(1);
'404': function(test) {
// test.expect(2);
// tests here
test.equal(hoax.awesome(), 'awesome', 'should be awesome.');
test.done();
hoax("http://localhost:3001/error?status=404",
function(errJSON, res){
// console.log(ok.statusCode, body);
test.equal(res.statusCode, 404, 'response is second argument on error');
test.equal(errJSON.error, true, 'error should be body when parsed json and error code');
test.done();
});
}
};

0 comments on commit 244b959

Please sign in to comment.