Skip to content

Commit

Permalink
Query based routing.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarvghotra committed Mar 10, 2017
1 parent c83c057 commit 48b91db
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/routing.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using HttpCommon

export method, GET, route, page, probabilty
export method, GET, route, page, probabilty, query, print

# Request type

Expand All @@ -19,6 +19,7 @@ function matchpath(target, path)
params = d()
for i = 1:length(target)
if startswith(target[i], ":")
params = d()
params[Symbol(target[i][2:end])] = path[i]
else
target[i] == path[i] || return
Expand All @@ -44,6 +45,30 @@ page(p::AbstractString, app...) = page(splitpath(p), app...)
page(app...) = page([], app...)
page(app::Function, p) = page(p, app)

# Query routing

function pt(req)
println("query ", req[:query])
return true
end

print(m::AbstractString, app...) = branch(req -> pt(req), app...)

function matchquery(q, req)
qdict = parsequerystring(req[:query])
length(q) != length(qdict) && return false
for pair in q
if haskey(qdict, pair[1]) && (pair[2] == "" || pair[2] == qdict[pair[1]])
continue
else
return false
end
end
return true
end

query(q::Dict{String, String}, app...) = branch(req -> matchquery(q, req), app...)

# Misc

probabilty(x, app...) = branch(_->rand()<x, app...)
13 changes: 12 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ using Lazy
import Requests

@test Mux.notfound()(d())[:status] == 404

d1 = Dict("one"=> "1", "two"=> "2")
d2 = Dict("one"=> "1", "two"=> "")
# Test basic server
@app test = (
Mux.defaults,
page(respond("<h1>Hello World!</h1>")),
page("/about", respond("<h1>Boo!</h1>")),
page("/user/:user", req -> "<h1>Hello, $(req[:params][:user])!</h1>"),
query(d1, respond("<h1>query1</h1>")),
query(d2, respond("<h1>query2</h1>")),
Mux.notfound())
serve(test)
@test Requests.text(Requests.get("http://localhost:8000")) ==
Expand All @@ -19,3 +22,11 @@ serve(test)
"<h1>Boo!</h1>"
@test Requests.text(Requests.get("http://localhost:8000/user/julia")) ==
"<h1>Hello, julia!</h1>"
@test Requests.text(Requests.get("http://localhost:8000/dum?one=1&two=2")) == "<h1>query1</h1>"
@test Requests.text(Requests.get("http://localhost:8000/dum?one=1")) != "<h1>query1</h1>"
@test Requests.text(Requests.get("http://localhost:8000/dum?one=1&two=2&sarv=boo")) != "<h1>query1</h1>"

@test Requests.text(Requests.get("http://localhost:8000/dum?one=1")) != "<h1>query2</h1>"
@test Requests.text(Requests.get("http://localhost:8000/dum?one=1&two=56")) == "<h1>query2</h1>"
@test Requests.text(Requests.get("http://localhost:8000/dum?one=1&two=hfjd")) == "<h1>query2</h1>"
@test Requests.text(Requests.get("http://localhost:8000/dum?one=1&two=2&sarv=boo")) != "<h1>query2</h1>"

0 comments on commit 48b91db

Please sign in to comment.