diff --git a/lib/index.js b/lib/index.js index e69de29..9e3797f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -0,0 +1,4 @@ +module.exports = { + plugin: require("./plugin.js"), + server: require("./server.js"), +}; diff --git a/lib/plugin.js b/lib/plugin.js new file mode 100644 index 0000000..5394225 --- /dev/null +++ b/lib/plugin.js @@ -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; diff --git a/lib/server.js b/lib/server.js new file mode 100644 index 0000000..35d4a27 --- /dev/null +++ b/lib/server.js @@ -0,0 +1,5 @@ +var server = function(settings){ + this.settings = settings || {}; +}; + +module.exports = server; diff --git a/test/plugin.js b/test/plugin.js new file mode 100644 index 0000000..9ec36bd --- /dev/null +++ b/test/plugin.js @@ -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); + }); + +});