From ad6baba12b98789ca6c7fec8ca614d4ad69f2ff9 Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Wed, 28 Oct 2015 20:38:24 +0200 Subject: [PATCH] Added param_parser specs --- spec/param_parser_spec.cr | 23 +++++++++++++++++------ src/kemal/handler.cr | 2 +- src/kemal/param_parser.cr | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/spec/param_parser_spec.cr b/spec/param_parser_spec.cr index 9f544f3..06bac0e 100644 --- a/spec/param_parser_spec.cr +++ b/spec/param_parser_spec.cr @@ -1,16 +1,27 @@ require "./spec_helper" describe "ParamParser" do - it "parses params" do - kemal = Kemal::Handler.new - kemal.add_route "POST", "/" do |env| + + it "parses query params" do + route = Route.new "POST", "/" do |env| + hasan = env.params["hasan"] + "Hello #{hasan}" + end + request = HTTP::Request.new("POST", "/?hasan=cemal") + params = Kemal::ParamParser.new(route, request).parse + 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"] "Hello #{name} #{hasan} #{age}" end - request = HTTP::Request.new("POST", "/?hasan=cemal", body: "name=kemal&age=99") - response = kemal.call(request) - response.body.should eq("Hello kemal cemal 99") + request = HTTP::Request.new("POST", "/?hasan=cemal", body: "name=serdar&age=99") + params = Kemal::ParamParser.new(route, request).parse + params.should eq({"hasan" => "cemal", "name" => "serdar", "age" => "99"}) end + end diff --git a/src/kemal/handler.cr b/src/kemal/handler.cr index 527514f..719f207 100644 --- a/src/kemal/handler.cr +++ b/src/kemal/handler.cr @@ -22,7 +22,7 @@ class Kemal::Handler < HTTP::Handler @routes.each do |route| match = route.match?(request) if match - params = Kemal::Params.new(route, request).parse + params = Kemal::ParamParser.new(route, request).parse context = Context.new(request, params.not_nil!) begin body = route.handler.call(context).to_s diff --git a/src/kemal/param_parser.cr b/src/kemal/param_parser.cr index 01526ed..df0cdcb 100644 --- a/src/kemal/param_parser.cr +++ b/src/kemal/param_parser.cr @@ -1,4 +1,4 @@ -class Kemal::Params +class Kemal::ParamParser def initialize(@route, @request) @route_components = route.components @request_components = request.path.not_nil!.split "/"