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

Query based routing. #39

Closed
wants to merge 2 commits into from
Closed
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
19 changes: 18 additions & 1 deletion src/routing.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using HTTP

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

# Request type

Expand Down Expand Up @@ -44,6 +44,23 @@ page(p::AbstractString, app...) = page(splitpath(p), app...)
page(app...) = page([], app...)
page(app::Function, p) = page(p, app)

# Query routing

function matchquery(q, req)
qdict = parsequerystring(req[:query])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to see if I can rebase this PR on top of master, but this one call parsequerystring doesn't exist, and I'm having trouble locating it in the git history. Does anyone know what this function is supposed to do? @sarvjeetsinghghotra @MikeInnes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

length(q) != length(qdict) && return false
for (key, value) in q
if haskey(qdict, key) && (value == "" || value == qdict[key])
continue
else
return false
end
end
return true
end

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

# Misc

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

@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 String(HTTP.get("http://localhost:8000").body) ==
Expand All @@ -20,8 +23,15 @@ serve(test)
@test String(HTTP.get("http://localhost:8000/user/julia").body) ==
"<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>"

# Issue #68
@test Mux.fileheaders("foo.css")["Content-Type"] == "text/css"
@test Mux.fileheaders("foo.html")["Content-Type"] == "text/html"
@test Mux.fileheaders("foo.js")["Content-Type"] == "application/javascript"