-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
158 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]); | ||
}); | ||
|
||
}); |