From dc704ea0e01cc1d225e594d5f86093a6d3a84d85 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 31 Aug 2013 08:05:53 -0400 Subject: [PATCH] break tests into smaller scripts --- test/plugin.addRoute.js | 6 + test/{plugin.js => plugin.methods.js} | 51 ----- test/plugin.on.js | 51 +++++ test/server.constructor.js | 42 +++++ test/server.errorHandler.js | 12 ++ test/server.js | 260 -------------------------- test/server.registerPlugins.js | 122 ++++++++++++ test/server.start.js | 80 ++++++++ test/server.stop.js | 18 ++ 9 files changed, 331 insertions(+), 311 deletions(-) create mode 100644 test/plugin.addRoute.js rename test/{plugin.js => plugin.methods.js} (62%) create mode 100644 test/plugin.on.js create mode 100644 test/server.constructor.js create mode 100644 test/server.errorHandler.js delete mode 100644 test/server.js create mode 100644 test/server.registerPlugins.js create mode 100644 test/server.start.js create mode 100644 test/server.stop.js diff --git a/test/plugin.addRoute.js b/test/plugin.addRoute.js new file mode 100644 index 0000000..ffd878e --- /dev/null +++ b/test/plugin.addRoute.js @@ -0,0 +1,6 @@ +var assert = require("assert"); + +var yaps = require("../"); + +suite("Plugin.addRoute", function(){ +}); diff --git a/test/plugin.js b/test/plugin.methods.js similarity index 62% rename from test/plugin.js rename to test/plugin.methods.js index 577910c..fbb0360 100644 --- a/test/plugin.js +++ b/test/plugin.methods.js @@ -2,9 +2,6 @@ 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(); @@ -88,51 +85,3 @@ suite("Plugin.PATCH", function(){ 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, respond){}; - plugin.on("not-found", handler); - - assert.ok(plugin.notFound); - assert.equal(plugin.notFound, handler); - }); - - test("calling on not-found twice should overwrite not-found handler", function(){ - var plugin = new yaps.plugin(); - var handler = function(request, server, respond){}; - plugin.on("not-found", false); - plugin.on("not-found", handler); - - assert.ok(plugin.notFound); - assert.equal(plugin.notFound, handler); - }); - - test("on 404 should set not-found handler", function(){ - var plugin = new yaps.plugin(); - var handler = function(request, server, respond){}; - plugin.on("404", handler); - - assert.ok(plugin.notFound); - assert.equal(plugin.notFound, handler); - }); - - test("on with unknown event should raise Error", function(){ - var plugin = new yaps.plugin(); - assert.throws(function(){ - plugin.on("uknown-event", false); - }, Error); - }); - -}); diff --git a/test/plugin.on.js b/test/plugin.on.js new file mode 100644 index 0000000..44b3877 --- /dev/null +++ b/test/plugin.on.js @@ -0,0 +1,51 @@ +var assert = require("assert"); + +var yaps = require("../"); + +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, respond){}; + plugin.on("not-found", handler); + + assert.ok(plugin.notFound); + assert.equal(plugin.notFound, handler); + }); + + test("calling on not-found twice should overwrite not-found handler", function(){ + var plugin = new yaps.plugin(); + var handler = function(request, server, respond){}; + plugin.on("not-found", false); + plugin.on("not-found", handler); + + assert.ok(plugin.notFound); + assert.equal(plugin.notFound, handler); + }); + + test("on 404 should set not-found handler", function(){ + var plugin = new yaps.plugin(); + var handler = function(request, server, respond){}; + plugin.on("404", handler); + + assert.ok(plugin.notFound); + assert.equal(plugin.notFound, handler); + }); + + test("on with unknown event should raise Error", function(){ + var plugin = new yaps.plugin(); + assert.throws(function(){ + plugin.on("uknown-event", false); + }, Error); + }); + +}); diff --git a/test/server.constructor.js b/test/server.constructor.js new file mode 100644 index 0000000..56d1c9b --- /dev/null +++ b/test/server.constructor.js @@ -0,0 +1,42 @@ +var assert = require("assert"); + +var yaps = require("../"); + +suite("Server.Constructor", function(){ + test("should setup default attributes", function(){ + var server = new yaps.server(); + assert.deepEqual(server.settings, {}); + assert.equal(server.bind, "0.0.0.0:8000"); + assert.deepEqual(server.enabledPlugins, []); + assert.deepEqual(server.routes, {}); + assert.deepEqual(server.notFound, []); + assert.deepEqual(server.setup, []); + assert.ok(server.server.yaps); + }); + + test("should take our provided settings", function(){ + var server = new yaps.server({ + test: true, + bind: "127.0.0.1:9090", + }); + + assert.deepEqual(server.settings, { + test: true, + bind: "127.0.0.1:9090", + }); + assert.equal(server.bind, "127.0.0.1:9090"); + }); + + test("should take out provided http server", function(){ + var server = new yaps.server({}, { + test: true, + }); + var keys = []; + for(var key in server.server){ + keys.push(key); + } + assert.deepEqual(keys, ["test", "yaps"]); + assert.equal(server.server.test, true); + assert.ok(server.server.yaps); + }); +}); diff --git a/test/server.errorHandler.js b/test/server.errorHandler.js new file mode 100644 index 0000000..e905a90 --- /dev/null +++ b/test/server.errorHandler.js @@ -0,0 +1,12 @@ +var assert = require("assert"); + +var yaps = require("../"); + +suite("Server.ErrorHandler", function(){ + test("should raise Error when errorHandler is called", function(){ + var server = new yaps.server(); + assert.throws(function(){ + server.errorHandler("test error message"); + }, Error); + }); +}); diff --git a/test/server.js b/test/server.js deleted file mode 100644 index 78e653f..0000000 --- a/test/server.js +++ /dev/null @@ -1,260 +0,0 @@ -var assert = require("assert"); - -var sinon = require("sinon"); - -var yaps = require("../"); - -suite("Server.Constructor", function(){ - test("should setup default attributes", function(){ - var server = new yaps.server(); - assert.deepEqual(server.settings, {}); - assert.equal(server.bind, "0.0.0.0:8000"); - assert.deepEqual(server.enabledPlugins, []); - assert.deepEqual(server.routes, {}); - assert.deepEqual(server.notFound, []); - assert.deepEqual(server.setup, []); - assert.ok(server.server.yaps); - }); - - test("should take our provided settings", function(){ - var server = new yaps.server({ - test: true, - bind: "127.0.0.1:9090", - }); - - assert.deepEqual(server.settings, { - test: true, - bind: "127.0.0.1:9090", - }); - assert.equal(server.bind, "127.0.0.1:9090"); - }); - - test("should take out provided http server", function(){ - var server = new yaps.server({}, { - test: true, - }); - var keys = []; - for(var key in server.server){ - keys.push(key); - } - assert.deepEqual(keys, ["test", "yaps"]); - assert.equal(server.server.test, true); - assert.ok(server.server.yaps); - }); -}); - -suite("Server.Stop", function(){ - test("should call http_server.close on stop", function(){ - var http_server_api = { - close: function(){} - }; - var http_server = sinon.mock(http_server_api); - http_server.expects("close").once(); - var server = new yaps.server({}, http_server_api); - server.stop(); - http_server.verify(); - }); -}); - -suite("Server.ErrorHandler", function(){ - test("should raise Error when errorHandler is called", function(){ - var server = new yaps.server(); - assert.throws(function(){ - server.errorHandler("test error message"); - }, Error); - }); -}); - -suite("Server.RegisterPlugin", function(){ - test("should push an instance of the plugin to server.enabledPlugins", function(){ - var plugin = function(options){ - this.test = true; - this.settings = options; - }; - var server = new yaps.server({ - some: "setting", - }); - server.registerPlugin(plugin); - assert.equal(server.enabledPlugins.length, 1); - assert.equal(server.enabledPlugins[0].test, true); - assert.deepEqual(server.enabledPlugins[0].settings, { - some: "setting", - }); - }); - - test("should transfer routes from plugin to server", function(){ - var plugin = function(options){ - this.routes = { - GET: { - "/test": [ - "handler_1", - "handler_2", - ] - }, - TEST: { - "/test": [ - "handler_3", - "handler_4", - ] - } - }; - }; - var server = new yaps.server(); - server.registerPlugin(plugin); - - assert.ok(server.routes.GET); - assert.ok(server.routes.TEST); - assert.deepEqual(server.routes.GET["/test"], ["handler_1", "handler_2"]); - assert.deepEqual(server.routes.TEST["/test"], ["handler_3", "handler_4"]); - }); - - test("should transfer routes from multiple plugins without overwriting any", function(){ - var plugin_1 = function(options){ - this.routes = { - GET: { - "/test": [ - "handler_1", - "handler_2", - ] - }, - }; - }; - var plugin_2 = function(options){ - this.routes = { - GET: { - "/test": [ - "handler_3", - "handler_4", - ] - }, - }; - }; - - var server = new yaps.server(); - server.registerPlugin(plugin_1); - server.registerPlugin(plugin_2); - - assert.ok(server.routes.GET); - assert.deepEqual(server.routes.GET["/test"], ["handler_1", "handler_2", "handler_3", "handler_4"]); - }); - - test("should append plugins notFound to server", function(){ - var plugin = function(options){ - this.notFound = "handler"; - }; - var server = new yaps.server(); - server.registerPlugin(plugin); - assert.deepEqual(server.notFound, ["handler"]); - }); - - test("should append notFound from multiple plugins", function(){ - var plugin_1 = function(options){ - this.notFound = "handler_1"; - }; - - var plugin_2 = function(options){ - this.notFound = "handler_2"; - }; - var server = new yaps.server(); - server.registerPlugin(plugin_1); - server.registerPlugin(plugin_2); - assert.deepEqual(server.notFound, ["handler_1", "handler_2"]); - }); - - test("should append plugins setup to server", function(){ - var plugin = function(options){ - this.setup = ["handler_1", "handler_2"]; - }; - var server = new yaps.server(); - server.registerPlugin(plugin); - assert.deepEqual(server.setup, ["handler_1", "handler_2"]); - }); - - test("should append setup from multiple plugins", function(){ - var plugin_1 = function(options){ - this.setup = ["handler_1", "handler_2"]; - }; - var plugin_2 = function(options){ - this.setup = ["handler_3", "handler_4"]; - }; - var server = new yaps.server(); - server.registerPlugin(plugin_1); - server.registerPlugin(plugin_2); - assert.deepEqual(server.setup, ["handler_1", "handler_2", "handler_3", "handler_4"]); - }); -}); - -suite("Server.Start", function(){ - test("should call http_server.on and http_server.listen", function(){ - var http_server_api = { - on: function(){}, - listen: function(){} - }; - var http_server = sinon.mock(http_server_api); - http_server.expects("listen").withArgs("8000", "0.0.0.0").once(); - http_server.expects("on").twice(); - var server = new yaps.server({}, http_server_api); - server.start(); - http_server.verify(); - }); - - test("should call http_server.listen with different http bind", function(){ - var http_server_api = { - on: function(){}, - listen: function(){} - }; - var http_server = sinon.mock(http_server_api); - http_server.expects("listen").withArgs("9090", "127.0.0.1").once(); - http_server.expects("on").twice(); - var server = new yaps.server({ - bind: "127.0.0.1:9090", - }, http_server_api); - server.start(); - http_server.verify(); - }); - - test("should call http_server.listen with unix socket", function(){ - var http_server_api = { - on: function(){}, - listen: function(){} - }; - var http_server = sinon.mock(http_server_api); - http_server.expects("listen").withArgs("unix://tmp/my.sock").once(); - http_server.expects("on").twice(); - var server = new yaps.server({ - bind: "unix://tmp/my.sock", - }, http_server_api); - server.start(); - http_server.verify(); - }); - - test("should raise error when bind has more than 1 :", function(){ - var http_server_api = { - on: function(){}, - listen: function(){} - }; - var http_server = sinon.mock(http_server_api); - var server = new yaps.server({ - bind: "127.0.0.1:90:90", - }, http_server_api); - assert.throws(function(){ - server.start(); - }, Error); - }); - - test("should call http_server.listen properly with only port number", function(){ - var http_server_api = { - on: function(){}, - listen: function(){} - }; - var http_server = sinon.mock(http_server_api); - http_server.expects("listen").withArgs("9090", "0.0.0.0").once(); - http_server.expects("on").twice(); - var server = new yaps.server({ - bind: "9090", - }, http_server_api); - server.start(); - http_server.verify(); - }); - -}); diff --git a/test/server.registerPlugins.js b/test/server.registerPlugins.js new file mode 100644 index 0000000..8e9d6c0 --- /dev/null +++ b/test/server.registerPlugins.js @@ -0,0 +1,122 @@ +var assert = require("assert"); + +var yaps = require("../"); + +suite("Server.RegisterPlugin", function(){ + test("should push an instance of the plugin to server.enabledPlugins", function(){ + var plugin = function(options){ + this.test = true; + this.settings = options; + }; + var server = new yaps.server({ + some: "setting", + }); + server.registerPlugin(plugin); + assert.equal(server.enabledPlugins.length, 1); + assert.equal(server.enabledPlugins[0].test, true); + assert.deepEqual(server.enabledPlugins[0].settings, { + some: "setting", + }); + }); + + test("should transfer routes from plugin to server", function(){ + var plugin = function(options){ + this.routes = { + GET: { + "/test": [ + "handler_1", + "handler_2", + ] + }, + TEST: { + "/test": [ + "handler_3", + "handler_4", + ] + } + }; + }; + var server = new yaps.server(); + server.registerPlugin(plugin); + + assert.ok(server.routes.GET); + assert.ok(server.routes.TEST); + assert.deepEqual(server.routes.GET["/test"], ["handler_1", "handler_2"]); + assert.deepEqual(server.routes.TEST["/test"], ["handler_3", "handler_4"]); + }); + + test("should transfer routes from multiple plugins without overwriting any", function(){ + var plugin_1 = function(options){ + this.routes = { + GET: { + "/test": [ + "handler_1", + "handler_2", + ] + }, + }; + }; + var plugin_2 = function(options){ + this.routes = { + GET: { + "/test": [ + "handler_3", + "handler_4", + ] + }, + }; + }; + + var server = new yaps.server(); + server.registerPlugin(plugin_1); + server.registerPlugin(plugin_2); + + assert.ok(server.routes.GET); + assert.deepEqual(server.routes.GET["/test"], ["handler_1", "handler_2", "handler_3", "handler_4"]); + }); + + test("should append plugins notFound to server", function(){ + var plugin = function(options){ + this.notFound = "handler"; + }; + var server = new yaps.server(); + server.registerPlugin(plugin); + assert.deepEqual(server.notFound, ["handler"]); + }); + + test("should append notFound from multiple plugins", function(){ + var plugin_1 = function(options){ + this.notFound = "handler_1"; + }; + + var plugin_2 = function(options){ + this.notFound = "handler_2"; + }; + var server = new yaps.server(); + server.registerPlugin(plugin_1); + server.registerPlugin(plugin_2); + assert.deepEqual(server.notFound, ["handler_1", "handler_2"]); + }); + + test("should append plugins setup to server", function(){ + var plugin = function(options){ + this.setup = ["handler_1", "handler_2"]; + }; + var server = new yaps.server(); + server.registerPlugin(plugin); + assert.deepEqual(server.setup, ["handler_1", "handler_2"]); + }); + + test("should append setup from multiple plugins", function(){ + var plugin_1 = function(options){ + this.setup = ["handler_1", "handler_2"]; + }; + var plugin_2 = function(options){ + this.setup = ["handler_3", "handler_4"]; + }; + var server = new yaps.server(); + server.registerPlugin(plugin_1); + server.registerPlugin(plugin_2); + assert.deepEqual(server.setup, ["handler_1", "handler_2", "handler_3", "handler_4"]); + }); +}); diff --git a/test/server.start.js b/test/server.start.js new file mode 100644 index 0000000..e1d7ba6 --- /dev/null +++ b/test/server.start.js @@ -0,0 +1,80 @@ +var assert = require("assert"); + +var sinon = require("sinon"); + +var yaps = require("../"); + +suite("Server.Start", function(){ + test("should call http_server.on and http_server.listen", function(){ + var http_server_api = { + on: function(){}, + listen: function(){} + }; + var http_server = sinon.mock(http_server_api); + http_server.expects("listen").withArgs("8000", "0.0.0.0").once(); + http_server.expects("on").twice(); + var server = new yaps.server({}, http_server_api); + server.start(); + http_server.verify(); + }); + + test("should call http_server.listen with different http bind", function(){ + var http_server_api = { + on: function(){}, + listen: function(){} + }; + var http_server = sinon.mock(http_server_api); + http_server.expects("listen").withArgs("9090", "127.0.0.1").once(); + http_server.expects("on").twice(); + var server = new yaps.server({ + bind: "127.0.0.1:9090", + }, http_server_api); + server.start(); + http_server.verify(); + }); + + test("should call http_server.listen with unix socket", function(){ + var http_server_api = { + on: function(){}, + listen: function(){} + }; + var http_server = sinon.mock(http_server_api); + http_server.expects("listen").withArgs("unix://tmp/my.sock").once(); + http_server.expects("on").twice(); + var server = new yaps.server({ + bind: "unix://tmp/my.sock", + }, http_server_api); + server.start(); + http_server.verify(); + }); + + test("should raise error when bind has more than 1 :", function(){ + var http_server_api = { + on: function(){}, + listen: function(){} + }; + var http_server = sinon.mock(http_server_api); + var server = new yaps.server({ + bind: "127.0.0.1:90:90", + }, http_server_api); + assert.throws(function(){ + server.start(); + }, Error); + }); + + test("should call http_server.listen properly with only port number", function(){ + var http_server_api = { + on: function(){}, + listen: function(){} + }; + var http_server = sinon.mock(http_server_api); + http_server.expects("listen").withArgs("9090", "0.0.0.0").once(); + http_server.expects("on").twice(); + var server = new yaps.server({ + bind: "9090", + }, http_server_api); + server.start(); + http_server.verify(); + }); + +}); diff --git a/test/server.stop.js b/test/server.stop.js new file mode 100644 index 0000000..d5977d1 --- /dev/null +++ b/test/server.stop.js @@ -0,0 +1,18 @@ +var assert = require("assert"); + +var sinon = require("sinon"); + +var yaps = require("../"); + +suite("Server.Stop", function(){ + test("should call http_server.close on stop", function(){ + var http_server_api = { + close: function(){} + }; + var http_server = sinon.mock(http_server_api); + http_server.expects("close").once(); + var server = new yaps.server({}, http_server_api); + server.stop(); + http_server.verify(); + }); +});