Browse Source

start base of plugin

master
Brett Langdon 12 years ago
parent
commit
cc242ba443
4 changed files with 194 additions and 0 deletions
  1. +4
    -0
      lib/index.js
  2. +47
    -0
      lib/plugin.js
  3. +5
    -0
      lib/server.js
  4. +138
    -0
      test/plugin.js

+ 4
- 0
lib/index.js View File

@ -0,0 +1,4 @@
module.exports = {
plugin: require("./plugin.js"),
server: require("./server.js"),
};

+ 47
- 0
lib/plugin.js View File

@ -0,0 +1,47 @@
var plugin = function(){};
plugin.prototype.on = function(event, handler){
if(event == "not-found" || event == "404"){
this.not_found = handler;
} else if(event == "setup"){
this.setup = this.setup || [];
this.setup.push(handler);
} else{
throw new Error("YAPS, cannot add handler for unknown event: " + event);
}
};
plugin.prototype.addRoute = function(method, route, handler){
this.routes = this.routes || {};
method = method.toUpperCase();
this.routes[method] = this.routes[method] || {};
this.routes[method][route] = this.routes[method][route] || [];
this.routes[method][route].push(handler);
};
plugin.prototype.GET = function(route, handler){
this.addRoute("GET", route, handler);
};
plugin.prototype.POST = function(route, handler){
this.addRoute("POST", route, handler);
};
plugin.prototype.PUT = function(route, handler){
this.addRoute("PUT", route, handler);
};
plugin.prototype.HEAD = function(route, handler){
this.addRoute("HEAD", route, handler);
};
plugin.prototype.DELETE = function(route, handler){
this.addRoute("DELETE", route, handler);
};
plugin.prototype.PATCH = function(route, handler){
this.addRoute("PATCH", route, handler);
};
module.exports = plugin;

+ 5
- 0
lib/server.js View File

@ -0,0 +1,5 @@
var server = function(settings){
this.settings = settings || {};
};
module.exports = server;

+ 138
- 0
test/plugin.js View File

@ -0,0 +1,138 @@
var assert = require("assert");
var yaps = require("../");
suite("Plugin.addRoute", function(){
});
suite("Plugin.GET", function(){
test("should add a route for GET method", function(){
var plugin = new yaps.plugin();
var handler = function(request, server, respond){};
plugin.GET("/test", handler);
assert.ok(plugin.routes);
assert.ok(plugin.routes["GET"]);
assert.ok(plugin.routes["GET"]["/test"]);
assert.deepEqual(plugin.routes["GET"]["/test"], [handler]);
});
});
suite("Plugin.POST", function(){
test("should add a route for POST method", function(){
var plugin = new yaps.plugin();
var handler = function(request, server, respond){};
plugin.POST("/test", handler);
assert.ok(plugin.routes);
assert.ok(plugin.routes["POST"]);
assert.ok(plugin.routes["POST"]["/test"]);
assert.deepEqual(plugin.routes["POST"]["/test"], [handler]);
});
});
suite("Plugin.PUT", function(){
test("should add a route for PUT method", function(){
var plugin = new yaps.plugin();
var handler = function(request, server, respond){};
plugin.PUT("/test", handler);
assert.ok(plugin.routes);
assert.ok(plugin.routes["PUT"]);
assert.ok(plugin.routes["PUT"]["/test"]);
assert.deepEqual(plugin.routes["PUT"]["/test"], [handler]);
});
});
suite("Plugin.HEAD", function(){
test("should add a route for HEAD method", function(){
var plugin = new yaps.plugin();
var handler = function(request, server, respond){};
plugin.HEAD("/test", handler);
assert.ok(plugin.routes);
assert.ok(plugin.routes["HEAD"]);
assert.ok(plugin.routes["HEAD"]["/test"]);
assert.deepEqual(plugin.routes["HEAD"]["/test"], [handler]);
});
});
suite("Plugin.DELETE", function(){
test("should add a route for DELETE method", function(){
var plugin = new yaps.plugin();
var handler = function(request, server, respond){};
plugin.DELETE("/test", handler);
assert.ok(plugin.routes);
assert.ok(plugin.routes["DELETE"]);
assert.ok(plugin.routes["DELETE"]["/test"]);
assert.deepEqual(plugin.routes["DELETE"]["/test"], [handler]);
});
});
suite("Plugin.PATCH", function(){
test("should add a route for PATCH method", function(){
var plugin = new yaps.plugin();
var handler = function(request, server, respond){};
plugin.PATCH("/test", handler);
assert.ok(plugin.routes);
assert.ok(plugin.routes["PATCH"]);
assert.ok(plugin.routes["PATCH"]["/test"]);
assert.deepEqual(plugin.routes["PATCH"]["/test"], [handler]);
});
});
suite("Plugin.on", function(){
test("on setup should add setup handler", function(){
var plugin = new yaps.plugin();
var handler = function(request, server, done){};
plugin.on("setup", handler);
assert.ok(plugin.setup);
assert.equal(plugin.setup.length, 1);
assert.equal(plugin.setup[0], handler);
});
test("on not-found should set not-found handler", function(){
var plugin = new yaps.plugin();
var handler = function(request, server, done){};
plugin.on("not-found", handler);
assert.ok(plugin.not_found);
assert.equal(plugin.not_found, handler);
});
test("calling on not-found twice should overwrite not-found handler", function(){
var plugin = new yaps.plugin();
var handler = function(request, server, done){};
plugin.on("not-found", false);
plugin.on("not-found", handler);
assert.ok(plugin.not_found);
assert.equal(plugin.not_found, handler);
});
test("on 404 should set not-found handler", function(){
var plugin = new yaps.plugin();
var handler = function(request, server, done){};
plugin.on("404", handler);
assert.ok(plugin.not_found);
assert.equal(plugin.not_found, handler);
});
test("on with unknown event should raise Error", function(){
var plugin = new yaps.plugin();
assert.throws(function(){
plugin.on("uknown-event", false);
}, Error);
});
});

Loading…
Cancel
Save