Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add route tests #28

Merged
merged 2 commits into from
Dec 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions test/REQUIRE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FactCheck
Requests
77 changes: 77 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1 +1,78 @@
using Morsel
using FactCheck
import Requests
req = Requests

app = Morsel.app()

get(app, "/") do req, res
"root_get"
end

@async start(app, 8000)

# make sure the app has run
response = req.get("http://localhost:8000/")
while (response.status != 200)
response = req.get("http://localhost:8000/")
end

facts("route") do
facts("get, post, post, put, delete, request") do
# patch doesn't support...
route(app, POST | PUT | DELETE , "/") do req, res
"root"
end

context("get") do
response = req.get("http://localhost:8000/")
@fact response.status => 200
@fact response.data => "root_get"
end

context("post, put, delete") do
@fact req.post("http://localhost:8000/").data => "root"
@fact req.put("http://localhost:8000/").data => "root"
@fact req.delete("http://localhost:8000/").data => "root"
end
end

facts("Add route when the app is running") do
get(app, "/running") do req, res
"running"
end
@fact req.get("http://localhost:8000/running").status => 200
end

facts("dynamic route") do
context("the request will succes if the type is right") do
route(app, GET, "/users/<id::Int>" ) do req, res
string("User id is:", req.params[:id])
end

@fact req.get("http://localhost:8000/users/10").status => 200
@fact req.get("http://localhost:8000/users/abc").status => 404 # wrong type
end
context("can get the value by req.params[:id]") do
@fact req.get("http://localhost:8000/users/10").data => "User id is:10"
end
context("regex route") do
route(app, GET, "/name/<name::%[a-z]{3}[0-9]{2}>") do req, res
req.params[:name]
end

@fact req.get("http://localhost:8000/name/abc33").data => "abc33"
@fact req.get("http://localhost:8000/name/abc333").status => 404 # not match the regex
end
end

facts("namespace") do
namespace(app, "/namespace") do app
get(app, "/hello/") do req, res
"hello namespace"
end
end

@fact req.get("http://localhost:8000/namespace/hello").data => "hello namespace"
end
end