-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
267 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
}; |