Accepts requests with query string

This commit is contained in:
Juan Wajnerman 2015-05-29 18:21:15 -03:00
parent 7e841a88b2
commit 8b1447b68d
3 changed files with 23 additions and 1 deletions

View file

@ -11,6 +11,16 @@ describe "Frank::Handler" do
response.body.should eq("hello") response.body.should eq("hello")
end end
it "routes request with query string" do
frank = Frank::Handler.new
frank.add_route "GET", "/" do |ctx|
"hello #{ctx.params["message"]}"
end
request = HTTP::Request.new("GET", "/?message=world")
response = frank.call(request)
response.body.should eq("hello world")
end
it "sets content type" do it "sets content type" do
frank = Frank::Handler.new frank = Frank::Handler.new
frank.add_route "GET", "/" do |env| frank.add_route "GET", "/" do |env|

View file

@ -11,4 +11,8 @@ class Frank::Context
def response? def response?
@response @response
end end
def params
request.params
end
end end

View file

@ -1,4 +1,5 @@
require "http/server" require "http/server"
require "cgi"
class Frank::Handler < HTTP::Handler class Frank::Handler < HTTP::Handler
INSTANCE = new INSTANCE = new
@ -17,10 +18,17 @@ class Frank::Handler < HTTP::Handler
end end
def exec_request(request) def exec_request(request)
components = request.path.split "/" uri = request.uri
components = uri.path.not_nil!.split "/"
@routes.each do |route| @routes.each do |route|
params = route.match(request.method, components) params = route.match(request.method, components)
if params if params
if query = uri.query
CGI.parse(query) do |key, value|
params[key] = value
end
end
frank_request = Request.new(request, params) frank_request = Request.new(request, params)
context = Context.new(frank_request) context = Context.new(frank_request)
begin begin