From 69f16e311d36c29c90e398c17113f68e2c8546e4 Mon Sep 17 00:00:00 2001 From: "david.bashford" Date: Thu, 9 Jul 2015 22:45:48 -0400 Subject: [PATCH] breaking out int tests, fixes #10 --- test/integrations/misc_configuration_test.js | 118 +++++++++++++ ...server_integration_test.js => pre_test.js} | 157 +----------------- test/integrations/validation_test.js | 39 +++++ 3 files changed, 158 insertions(+), 156 deletions(-) create mode 100644 test/integrations/misc_configuration_test.js rename test/integrations/{server_integration_test.js => pre_test.js} (65%) create mode 100644 test/integrations/validation_test.js diff --git a/test/integrations/misc_configuration_test.js b/test/integrations/misc_configuration_test.js new file mode 100644 index 0000000..9b692dc --- /dev/null +++ b/test/integrations/misc_configuration_test.js @@ -0,0 +1,118 @@ +var TestServer = new require("../util").TestServer + ; + +describe("routes with method will call handler", function() { + + var test = function(meth) { + it(meth.toUpperCase(), function(done) { + var config = new RouteBuilder().path("/api/test_path")[meth]().handler(function(request, reply) { + // get does not have payload + if (meth === "get") { + request.payload = { foo: "bar"}; + } + var returned = request.payload.foo + "_returned"; + reply({foo:returned}); + }).build(); + + new TestServer({routeConfig: config, done: done}).andTest(function(server, stop) { + server.inject({ + method: meth.toUpperCase(), + url: "/api/test_path", + payload: {foo:"bar"} + }, function(res) { + expect(res.result.foo).to.equal("bar_returned"); + stop(); + }); + }); + }); + }; + + test("post"); + test("delete"); + test("get"); + test("put"); + test("patch"); + test("options"); +}); + +var basicallyShitWorks = function(done) { + var path = "/api/test_path"; + var config = new RouteBuilder().path(path).post().handler(function(request, reply) { + reply(); + }).build(); + + new TestServer({routeConfig: config, done: done}).andTest(function(server, stop) { + server.inject({ + method: "POST", + url: path, + payload: {foo:"bar"} + }, function(res) { + expect(res.statusCode).to.equal(200); + stop(); + }); + }); +}; + +describe("paths configured", function() { + it("will be reached", function(done) { + basicallyShitWorks(done); + }); +}); + +describe("handlers configured", function() { + it("will be called", function(done) { + basicallyShitWorks(done); + }); +}); + +describe("vhost configured", function() { + + var path = "/api/test_path"; + var config = new RouteBuilder() + .path(path) + .get() + .vhost("foo.example.com") + .handler(function(request, reply) { + reply("foooooo"); + }) + .build(); + + var opts = { + routeConfig: config, + host: "example.com" + }; + + it("will be reached", function(done) { + opts.done = done; + new TestServer(opts).andTest(function(server, stop) { + server.inject({ + method: 'GET', + url: path, + headers: { + 'Set-Cookie': 'mycookie=test', + 'Host': 'foo.example.com' + } + }, function(res) { + expect(res.statusCode).to.equal(200); + expect(res.payload).to.equal("foooooo") + stop(); + }); + }); + }); + + it("will not be reached when not hitting vhost", function(done) { + opts.done = done; + new TestServer(opts).andTest(function(server, stop) { + server.inject({ + method: 'GET', + url: path, + headers: { + 'Set-Cookie': 'mycookie=test' + } + }, function(res) { + expect(res.statusCode).to.equal(404); + stop(); + }); + }); + }); +}); \ No newline at end of file diff --git a/test/integrations/server_integration_test.js b/test/integrations/pre_test.js similarity index 65% rename from test/integrations/server_integration_test.js rename to test/integrations/pre_test.js index 47e8875..9ff0204 100644 --- a/test/integrations/server_integration_test.js +++ b/test/integrations/pre_test.js @@ -1,160 +1,6 @@ var TestServer = new require("../util").TestServer - , Joi = require("joi") ; -describe("routes with method will call handler", function() { - - var test = function(meth) { - it(meth.toUpperCase(), function(done) { - var config = new RouteBuilder().path("/api/test_path")[meth]().handler(function(request, reply) { - // get does not have payload - if (meth === "get") { - request.payload = { foo: "bar"}; - } - var returned = request.payload.foo + "_returned"; - reply({foo:returned}); - }).build(); - - new TestServer({routeConfig: config, done: done}).andTest(function(server, stop) { - server.inject({ - method: meth.toUpperCase(), - url: "/api/test_path", - payload: {foo:"bar"} - }, function(res) { - expect(res.result.foo).to.equal("bar_returned"); - stop(); - }); - }); - }); - }; - - test("post"); - test("delete"); - test("get"); - test("put"); - test("patch"); - test("options"); -}); - -var basicallyShitWorks = function(done) { - var path = "/api/test_path"; - var config = new RouteBuilder().path(path).post().handler(function(request, reply) { - reply(); - }).build(); - - new TestServer({routeConfig: config, done: done}).andTest(function(server, stop) { - server.inject({ - method: "POST", - url: path, - payload: {foo:"bar"} - }, function(res) { - expect(res.statusCode).to.equal(200); - stop(); - }); - }); -}; - - describe("paths configured", function() { - it("will be reached", function(done) { - basicallyShitWorks(done); - }); -}); - -describe("vhost configured", function() { - - var path = "/api/test_path"; - var config = new RouteBuilder() - .path(path) - .get() - .vhost("foo.example.com") - .handler(function(request, reply) { - reply("foooooo"); - }) - .build(); - - var opts = { - routeConfig: config, - host: "example.com" - }; - - it("will be reached", function(done) { - opts.done = done; - new TestServer(opts).andTest(function(server, stop) { - server.inject({ - method: 'GET', - url: path, - headers: { - 'Set-Cookie': 'mycookie=test', - 'Host': 'foo.example.com' - } - }, function(res) { - expect(res.statusCode).to.equal(200); - expect(res.payload).to.equal("foooooo") - stop(); - }); - }); - }); - - it("will not be reached when not hitting vhost", function(done) { - opts.done = done; - new TestServer(opts).andTest(function(server, stop) { - server.inject({ - method: 'GET', - url: path, - headers: { - 'Set-Cookie': 'mycookie=test' - } - }, function(res) { - expect(res.statusCode).to.equal(404); - stop(); - }); - }); - }); -}); - - -describe("handlers configured", function() { - it("will be called", function(done) { - basicallyShitWorks(done); - }); -}); - -describe("valiation", function() { - var test= function(done, func, payload) { - var builder = new RouteBuilder() - .path("/api/foo") - .post() - - var config = builder[func].apply(builder, payload) - .handler(function(request, reply) { - reply(); - }) - .build(); - - new TestServer({routeConfig: config, done: done}).andTest(function(server, stop) { - server.inject({ - method: "POST", - url: "/api/foo", - payload: {name:5} - }, function(res) { - expect(res.statusCode).to.equal(400); - expect(res.result.message).to.equal("child \"name\" fails because [\"name\" must be a string]"); - stop(); - }); - }); - }; - - - it("will be executed via payload", function(done) { - test(done, "validatePayload", [{ name: Joi.string().required() }]); - }); - - it("will be executed via payload key", function(done){ - test(done, "validatePayloadKey", ["name", Joi.string().required()]); - }); - -}); - describe("pre", function() { var called = false; @@ -428,5 +274,4 @@ describe("pre", function() { }); }); -}) - +}); \ No newline at end of file diff --git a/test/integrations/validation_test.js b/test/integrations/validation_test.js new file mode 100644 index 0000000..ac0c15c --- /dev/null +++ b/test/integrations/validation_test.js @@ -0,0 +1,39 @@ +var Joi = require("joi") + , TestServer = new require("../util").TestServer + ; + +describe("validation", function() { + var test= function(done, func, payload) { + var builder = new RouteBuilder() + .path("/api/foo") + .post() + + var config = builder[func].apply(builder, payload) + .handler(function(request, reply) { + reply(); + }) + .build(); + + new TestServer({routeConfig: config, done: done}).andTest(function(server, stop) { + server.inject({ + method: "POST", + url: "/api/foo", + payload: {name:5} + }, function(res) { + expect(res.statusCode).to.equal(400); + expect(res.result.message).to.equal("child \"name\" fails because [\"name\" must be a string]"); + stop(); + }); + }); + }; + + + it("will be executed via payload", function(done) { + test(done, "validatePayload", [{ name: Joi.string().required() }]); + }); + + it("will be executed via payload key", function(done){ + test(done, "validatePayloadKey", ["name", Joi.string().required()]); + }); + +}); \ No newline at end of file