diff --git a/spec/param_parser_spec.cr b/spec/param_parser_spec.cr index f2b4f0e..c825b4e 100644 --- a/spec/param_parser_spec.cr +++ b/spec/param_parser_spec.cr @@ -3,19 +3,31 @@ require "./spec_helper" describe "ParamParser" do it "parses query params" do route = Route.new "POST", "/" do |env| - hasan = env.params["hasan"] + hasan = env.params.query["hasan"] "Hello #{hasan}" end request = HTTP::Request.new("POST", "/?hasan=cemal") - params = Kemal::ParamParser.new(request).parse - params["hasan"].should eq "cemal" + query_params = Kemal::ParamParser.new(request).params.query + query_params["hasan"].should eq "cemal" + end + + it "parses url params" do + kemal = Kemal::RouteHandler::INSTANCE + kemal.add_route "POST", "/hello/:hasan" do |env| + "hello #{env.params.url["hasan"]}" + end + request = HTTP::Request.new("POST", "/hello/cemal") + # Radix tree MUST be run to parse url params. + io_with_context = create_request_and_return_io(kemal, request) + url_params = Kemal::ParamParser.new(request).params.url + url_params["hasan"].should eq "cemal" end it "parses request body" do route = Route.new "POST", "/" do |env| - name = env.params["name"] - age = env.params["age"] - hasan = env.params["hasan"] + name = env.params.query["name"] + age = env.params.query["age"] + hasan = env.params.body["hasan"] "Hello #{name} #{hasan} #{age}" end @@ -26,8 +38,11 @@ describe "ParamParser" do headers: HTTP::Headers{"Content-Type": "application/x-www-form-urlencoded"}, ) - params = Kemal::ParamParser.new(request).parse - params.should eq({"hasan" => "cemal", "name" => "serdar", "age" => "99"}) + query_params = Kemal::ParamParser.new(request).params.query + query_params.should eq({"hasan" => "cemal"}) + + body_params = Kemal::ParamParser.new(request).params.body + body_params.should eq({"name" => "serdar", "age" => "99"}) end context "when content type is application/json" do @@ -41,8 +56,8 @@ describe "ParamParser" do headers: HTTP::Headers{"Content-Type": "application/json"}, ) - params = Kemal::ParamParser.new(request).parse - params.should eq({"name": "Serdar"}) + json_params = Kemal::ParamParser.new(request).params.json + json_params.should eq({"name": "Serdar"}) end it "parses request body for array" do @@ -55,8 +70,8 @@ describe "ParamParser" do headers: HTTP::Headers{"Content-Type": "application/json"}, ) - params = Kemal::ParamParser.new(request).parse - params.should eq({"_json": [1]}) + json_params = Kemal::ParamParser.new(request).params.json + json_params.should eq({"_json": [1]}) end it "parses request body and query params" do @@ -69,8 +84,11 @@ describe "ParamParser" do headers: HTTP::Headers{"Content-Type": "application/json"}, ) - params = Kemal::ParamParser.new(request).parse - params.should eq({"foo": "bar", "_json": [1]}) + query_params = Kemal::ParamParser.new(request).params.query + query_params.should eq({"foo": "bar"}) + + json_params = Kemal::ParamParser.new(request).params.json + json_params.should eq({"_json": [1]}) end it "handles no request body" do @@ -82,17 +100,26 @@ describe "ParamParser" do headers: HTTP::Headers{"Content-Type": "application/json"}, ) - params = Kemal::ParamParser.new(request).parse - params.should eq({} of String => AllParamTypes) + url_params = Kemal::ParamParser.new(request).params.url + url_params.should eq({} of String => String) + + query_params = Kemal::ParamParser.new(request).params.query + query_params.should eq({} of String => String) + + body_params = Kemal::ParamParser.new(request).params.body + body_params.should eq({} of String => String) + + json_params = Kemal::ParamParser.new(request).params.json + json_params.should eq({} of String => AllParamTypes) end end context "when content type is incorrect" do it "does not parse request body" do route = Route.new "POST", "/" do |env| - name = env.params["name"] - age = env.params["age"] - hasan = env.params["hasan"] + name = env.params.body["name"] + age = env.params.body["age"] + hasan = env.params.query["hasan"] "Hello #{name} #{hasan} #{age}" end @@ -103,8 +130,11 @@ describe "ParamParser" do headers: HTTP::Headers{"Content-Type": "text/plain"}, ) - params = Kemal::ParamParser.new(request).parse - params.should eq({"hasan" => "cemal"}) + query_params = Kemal::ParamParser.new(request).params.query + query_params.should eq({"hasan" => "cemal"}) + + body_params = Kemal::ParamParser.new(request).params.body + body_params.should eq({} of String => String) end end end diff --git a/spec/route_handler_spec.cr b/spec/route_handler_spec.cr index 2df06c5..69e742a 100644 --- a/spec/route_handler_spec.cr +++ b/spec/route_handler_spec.cr @@ -15,7 +15,7 @@ describe "Kemal::RouteHandler" do it "routes request with query string" do kemal = Kemal::RouteHandler::INSTANCE kemal.add_route "GET", "/" do |env| - "hello #{env.params["message"]}" + "hello #{env.params.query["message"]}" end request = HTTP::Request.new("GET", "/?message=world") io_with_context = create_request_and_return_io(kemal, request) @@ -26,7 +26,7 @@ describe "Kemal::RouteHandler" do it "routes request with multiple query strings" do kemal = Kemal::RouteHandler::INSTANCE kemal.add_route "GET", "/" do |env| - "hello #{env.params["message"]} time #{env.params["time"]}" + "hello #{env.params.query["message"]} time #{env.params.query["time"]}" end request = HTTP::Request.new("GET", "/?message=world&time=now") io_with_context = create_request_and_return_io(kemal, request) @@ -37,7 +37,7 @@ describe "Kemal::RouteHandler" do it "route parameter has more precedence than query string arguments" do kemal = Kemal::RouteHandler::INSTANCE kemal.add_route "GET", "/:message" do |env| - "hello #{env.params["message"]}" + "hello #{env.params.url["message"]}" end request = HTTP::Request.new("GET", "/world?message=coco") io_with_context = create_request_and_return_io(kemal, request) @@ -48,8 +48,8 @@ describe "Kemal::RouteHandler" do it "parses simple JSON body" do kemal = Kemal::RouteHandler::INSTANCE kemal.add_route "POST", "/" do |env| - name = env.params["name"] - age = env.params["age"] + name = env.params.json["name"] + age = env.params.json["age"] "Hello #{name} Age #{age}" end @@ -68,7 +68,7 @@ describe "Kemal::RouteHandler" do it "parses JSON with string array" do kemal = Kemal::RouteHandler::INSTANCE kemal.add_route "POST", "/" do |env| - skills = env.params["skills"] as Array + skills = env.params.json["skills"] as Array "Skills #{skills.each.join(',')}" end @@ -87,7 +87,7 @@ describe "Kemal::RouteHandler" do it "parses JSON with json object array" do kemal = Kemal::RouteHandler::INSTANCE kemal.add_route "POST", "/" do |env| - skills = env.params["skills"] as Array + skills = env.params.json["skills"] as Array skills_from_languages = skills.map do |skill| skill = skill as Hash skill["language"] diff --git a/spec/view_spec.cr b/spec/view_spec.cr index 42c49af..5906e3f 100644 --- a/spec/view_spec.cr +++ b/spec/view_spec.cr @@ -8,7 +8,7 @@ describe "Views" do it "renders file" do kemal = Kemal::RouteHandler::INSTANCE kemal.add_route "GET", "/view/:name" do |env| - name = env.params["name"] + name = env.params.url["name"] render "spec/asset/hello.ecr" end request = HTTP::Request.new("GET", "/view/world") @@ -20,7 +20,7 @@ describe "Views" do it "renders file with dynamic variables" do kemal = Kemal::RouteHandler::INSTANCE kemal.add_route "GET", "/view/:name" do |env| - name = env.params["name"] + name = env.params.url["name"] render_with_base_and_layout "hello.ecr" end request = HTTP::Request.new("GET", "/view/world") @@ -32,7 +32,7 @@ describe "Views" do it "renders layout" do kemal = Kemal::RouteHandler::INSTANCE kemal.add_route "GET", "/view/:name" do |env| - name = env.params["name"] + name = env.params.url["name"] render "spec/asset/hello.ecr", "spec/asset/layout.ecr" end request = HTTP::Request.new("GET", "/view/world") diff --git a/src/kemal/context.cr b/src/kemal/context.cr index 84867db..41a55dc 100644 --- a/src/kemal/context.cr +++ b/src/kemal/context.cr @@ -2,9 +2,10 @@ # information such as params, content_type e.g class HTTP::Server class Context + def params @request.url_params = route_lookup.params - @params ||= Kemal::ParamParser.new(@request).parse + @params ||= Kemal::ParamParser.new(@request).params end def redirect(url, status_code = 302) diff --git a/src/kemal/param_parser.cr b/src/kemal/param_parser.cr index ad0bf45..37f24dc 100644 --- a/src/kemal/param_parser.cr +++ b/src/kemal/param_parser.cr @@ -5,16 +5,34 @@ require "json" # context. alias AllParamTypes = Nil | String | Int64 | Float64 | Bool | Hash(String, JSON::Type) | Array(JSON::Type) +class Kemal::ParamContainer + getter url + getter query + getter body + getter json + + def initialize(@url, @query, @body, @json) + end + + def all + @url.merge(@query).merge(@body).merge(@json) + end +end + class Kemal::ParamParser URL_ENCODED_FORM = "application/x-www-form-urlencoded" APPLICATION_JSON = "application/json" def initialize(@request) - @params = {} of String => AllParamTypes + @url = {} of String => String + @query = {} of String => String + @body = {} of String => String + @json = {} of String => AllParamTypes end - def parse + def params parse_request + Kemal::ParamContainer.new(@url, @query, @body, @json) end def parse_request @@ -22,22 +40,21 @@ class Kemal::ParamParser parse_body parse_json parse_url_params - @params end def parse_body return if (@request.headers["Content-Type"]? =~ /#{URL_ENCODED_FORM}/).nil? - parse_part(@request.body) + @body = parse_part(@request.body) end def parse_query - parse_part(@request.query) + @query = parse_part(@request.query) end def parse_url_params if params = @request.url_params params.each do |key, value| - @params[key] = value + @url[key as String] = value as String end end end @@ -52,18 +69,21 @@ class Kemal::ParamParser body = @request.body as String case json = JSON.parse(body).raw when Hash - json.each do |k, v| - @params[k as String] = v as AllParamTypes + json.each do |key, value| + @json[key as String] = value as AllParamTypes end when Array - @params["_json"] = json + @json["_json"] = json end end def parse_part(part) - return unless part - HTTP::Params.parse(part) do |key, value| - @params[key] ||= value + part_params = {} of String => String + if part + HTTP::Params.parse(part) do |key, value| + part_params[key as String] ||= value as String + end end + part_params end end diff --git a/src/kemal/request.cr b/src/kemal/request.cr index 44cd118..dfc4935 100644 --- a/src/kemal/request.cr +++ b/src/kemal/request.cr @@ -11,7 +11,7 @@ class HTTP::Request private def check_for_method_override! @override_method = @method if @method == "POST" - params = Kemal::ParamParser.new(self).parse_request + params = Kemal::ParamParser.new(self).params.all if params.has_key?("_method") && HTTP::Request.override_method_valid?(params["_method"]) @override_method = params["_method"] end