Browse Source

jshint fixes

master
Brett Langdon 12 years ago
parent
commit
7a0538be69
4 changed files with 173 additions and 166 deletions
  1. +4
    -4
      lib/plugin.js
  2. +45
    -45
      lib/server.js
  3. +20
    -20
      test/plugin.js
  4. +104
    -97
      test/server.js

+ 4
- 4
lib/plugin.js View File

@ -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);
}
};


+ 45
- 45
lib/server.js View File

@ -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]);


+ 20
- 20
test/plugin.js View File

@ -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);
});


+ 104
- 97
test/server.js View File

@ -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();
});
});

Loading…
Cancel
Save