From 7a0538be69d41535afd68734054d094ff7e853f0 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 31 Aug 2013 07:39:59 -0400 Subject: [PATCH] jshint fixes --- lib/plugin.js | 8 +- lib/server.js | 90 +++++++++++----------- test/plugin.js | 40 +++++----- test/server.js | 201 +++++++++++++++++++++++++------------------------ 4 files changed, 173 insertions(+), 166 deletions(-) diff --git a/lib/plugin.js b/lib/plugin.js index 335a789..e1c2f4a 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -2,12 +2,12 @@ var plugin = function(){}; plugin.prototype.on = function(event, handler){ if(event == "not-found" || event == "404"){ - this.notFound = handler; + this.notFound = handler; } else if(event == "setup"){ - this.setup = this.setup || []; - this.setup.push(handler); + this.setup = this.setup || []; + this.setup.push(handler); } else{ - throw new Error("YAPS, cannot add handler for unknown event: " + event); + throw new Error("YAPS, cannot add handler for unknown event: " + event); } }; diff --git a/lib/server.js b/lib/server.js index 9ece9c3..674a97e 100644 --- a/lib/server.js +++ b/lib/server.js @@ -5,32 +5,32 @@ var yaps_plugin = require("./plugin.js"); var handler_chain = function(handlers, request, server, response, not_found){ if(handlers.length){ - handlers[0](request, server, function(status, body, headers){ - if(status == null || status == undefined){ - handler_chain(handlers.slice(1), request, server, response, not_found); - } else{ - response.writeHead(status, headers); - response.end(body); - } - }); + handlers[0](request, server, function(status, body, headers){ + if(status === null || status === undefined){ + handler_chain(handlers.slice(1), request, server, response, not_found); + } else{ + response.writeHead(status, headers); + response.end(body); + } + }); } else{ - not_found(); + not_found(); } }; var server = function(settings, http_server){ this.settings = settings || {}; - this.bind = this.settings["bind"] || "0.0.0.0:8000"; + this.bind = this.settings.bind || "0.0.0.0:8000"; this.enabledPlugins = []; this.routes = {}; this.notFound = []; this.setup = []; // they gave us an http server, lets use that - if(http_server != null || http_server != undefined){ - this.server = http_server; + if(http_server !== null || http_server !== undefined){ + this.server = http_server; } else{ - this.server = new http.Server(); + this.server = new http.Server(); } this.server.yaps = this; }; @@ -40,29 +40,29 @@ server.prototype.rootHandler = function(request, response){ var handlers = []; var method = request.method.toUpperCase(); for(var route in self.routes[method]){ - if(!self.routes[method][route].length){ - continue; - } - var pattern = new RegExp(route); - if(pattern.test(request.url)){ - handlers = handlers.concat(self.routes[method][route]); - } + if(!self.routes[method][route].length){ + continue; + } + var pattern = new RegExp(route); + if(pattern.test(request.url)){ + handlers = handlers.concat(self.routes[method][route]); + } } handlers = self.setup.concat(handlers); if(handlers.length){ handler_chain(handlers, request, self, response, function(){ - handler_chain(self.notFound, request, self, response, function(){ - response.writeHead(404); - response.end("Not Found"); - }); + handler_chain(self.notFound, request, self, response, function(){ + response.writeHead(404); + response.end("Not Found"); + }); }); } else{ - handler_chain(self.notFound, request, self, response, function(){ - response.writeHead(404); - response.end("Not Found"); - }); + handler_chain(self.notFound, request, self, response, function(){ + response.writeHead(404); + response.end("Not Found"); + }); } }; @@ -75,17 +75,17 @@ server.prototype.registerPlugin = function(plugin_constructor){ var plugin = new plugin_constructor(this.settings); this.enabledPlugins.push(plugin); for(var method in plugin.routes){ - this.routes[method] = this.routes[method] || {}; - for(var route in plugin.routes[method]){ - this.routes[method][route] = this.routes[method][route] || []; - this.routes[method][route] = this.routes[method][route].concat(plugin.routes[method][route]); - } + this.routes[method] = this.routes[method] || {}; + for(var route in plugin.routes[method]){ + this.routes[method][route] = this.routes[method][route] || []; + this.routes[method][route] = this.routes[method][route].concat(plugin.routes[method][route]); + } } if(plugin.notFound){ - this.notFound.push(plugin.notFound); + this.notFound.push(plugin.notFound); } if(plugin.setup && plugin.setup.length){ - this.setup = this.setup.concat(plugin.setup); + this.setup = this.setup.concat(plugin.setup); } }; @@ -94,17 +94,17 @@ server.prototype.start = function(){ this.server.on("request", this.rootHandler); var bind_parts = [undefined, undefined]; - if(this.bind.indexOf("unix://") == 0){ - bind_parts[1] = this.bind; + if(this.bind.indexOf("unix://") === 0){ + bind_parts[1] = this.bind; } else{ - if(!~this.bind.indexOf(":")){ - bind_parts = ["0.0.0.0", this.bind]; - } else{ - bind_parts = this.bind.split(":"); - if(bind_parts.length > 2){ - throw new Error("YAPS, Malformed bind parameter:", this.bind); - } - } + if(!~this.bind.indexOf(":")){ + bind_parts = ["0.0.0.0", this.bind]; + } else{ + bind_parts = this.bind.split(":"); + if(bind_parts.length > 2){ + throw new Error("YAPS, Malformed bind parameter:", this.bind); + } + } } this.server.listen(bind_parts[1], bind_parts[0]); diff --git a/test/plugin.js b/test/plugin.js index 729efe7..577910c 100644 --- a/test/plugin.js +++ b/test/plugin.js @@ -13,9 +13,9 @@ suite("Plugin.GET", function(){ 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]); + assert.ok(plugin.routes.GET); + assert.ok(plugin.routes.GET["/test"]); + assert.deepEqual(plugin.routes.GET["/test"], [handler]); }); }); @@ -27,9 +27,9 @@ suite("Plugin.POST", function(){ 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]); + assert.ok(plugin.routes.POST); + assert.ok(plugin.routes.POST["/test"]); + assert.deepEqual(plugin.routes.POST["/test"], [handler]); }); }); @@ -41,9 +41,9 @@ suite("Plugin.PUT", function(){ 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]); + assert.ok(plugin.routes.PUT); + assert.ok(plugin.routes.PUT["/test"]); + assert.deepEqual(plugin.routes.PUT["/test"], [handler]); }); }); @@ -55,9 +55,9 @@ suite("Plugin.HEAD", function(){ 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]); + assert.ok(plugin.routes.HEAD); + assert.ok(plugin.routes.HEAD["/test"]); + assert.deepEqual(plugin.routes.HEAD["/test"], [handler]); }); }); @@ -69,9 +69,9 @@ suite("Plugin.DELETE", function(){ 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]); + assert.ok(plugin.routes.DELETE); + assert.ok(plugin.routes.DELETE["/test"]); + assert.deepEqual(plugin.routes.DELETE["/test"], [handler]); }); }); @@ -83,9 +83,9 @@ suite("Plugin.PATCH", function(){ 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]); + assert.ok(plugin.routes.PATCH); + assert.ok(plugin.routes.PATCH["/test"]); + assert.deepEqual(plugin.routes.PATCH["/test"], [handler]); }); }); @@ -130,8 +130,8 @@ suite("Plugin.on", function(){ test("on with unknown event should raise Error", function(){ var plugin = new yaps.plugin(); - assert.throws(function(){ - plugin.on("uknown-event", false); + assert.throws(function(){ + plugin.on("uknown-event", false); }, Error); }); diff --git a/test/server.js b/test/server.js index 3719ed1..e53a6ae 100644 --- a/test/server.js +++ b/test/server.js @@ -6,138 +6,145 @@ 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); + 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", - }); + 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"); + 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); + 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(); + 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); + var server = new yaps.server(); + assert.throws(function(){ + server.errorHandler("test error message"); + }, Error); }); }); +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.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(); + 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(); + 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(); + 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 = sinon.stub(); - 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); + 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(); + 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(); }); });