Skip to content

Commit

Permalink
breaking out int tests, fixes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
dbashford committed Jul 10, 2015
1 parent 4aabb9a commit 69f16e3
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 156 deletions.
118 changes: 118 additions & 0 deletions test/integrations/misc_configuration_test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
});
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -428,5 +274,4 @@ describe("pre", function() {

});
});
})

});
39 changes: 39 additions & 0 deletions test/integrations/validation_test.js
Original file line number Diff line number Diff line change
@@ -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()]);
});

});

0 comments on commit 69f16e3

Please sign in to comment.