From 4a6fb6cf66c748a96506781f6c73dd0ae5970f9b Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Mon, 26 Oct 2015 20:49:28 +0200 Subject: [PATCH] Now supports multiple query strings --- spec/kemal_handler_spec.cr | 10 ++++++++++ src/kemal/handler.cr | 7 +++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/spec/kemal_handler_spec.cr b/spec/kemal_handler_spec.cr index 6ed8b33..9edcdfe 100644 --- a/spec/kemal_handler_spec.cr +++ b/spec/kemal_handler_spec.cr @@ -21,6 +21,16 @@ describe "Kemal::Handler" do response.body.should eq("hello world") end + it "routes request with multiple query strings" do + kemal = Kemal::Handler.new + kemal.add_route "GET", "/" do |ctx| + "hello #{ctx.params["message"]} time #{ctx.params["time"]}" + end + request = HTTP::Request.new("GET", "/?message=world&time=now") + response = kemal.call(request) + response.body.should eq("hello world time now") + end + it "route parameter has more precedence than query string arguments" do kemal = Kemal::Handler.new kemal.add_route "GET", "/:message" do |ctx| diff --git a/src/kemal/handler.cr b/src/kemal/handler.cr index 9adce1d..aeee1a2 100644 --- a/src/kemal/handler.cr +++ b/src/kemal/handler.cr @@ -23,10 +23,9 @@ class Kemal::Handler < HTTP::Handler params = route.match(request.method, components) if params if query = request.query - split = query.split("=") - key = split[0] - value = split[1] - params[key] ||= value + HTTP::Params.parse(query) do |key, value| + params[key] ||= value + end end if body = request.body