Revert "Modiy HTTP::Params.[] to fetch all params"

This reverts commit c0745cc9e2.
This commit is contained in:
sdogruyol 2017-05-08 17:28:21 +03:00
parent 0b4856b741
commit e695a491b7
3 changed files with 98 additions and 88 deletions

View file

@ -2,20 +2,25 @@ require "./spec_helper"
describe "ParamParser" do describe "ParamParser" do
it "parses query params" do it "parses query params" do
route = Route.new "POST", "/" do |env|
hasan = env.params.query["hasan"]
"Hello #{hasan}"
end
request = HTTP::Request.new("POST", "/?hasan=cemal") request = HTTP::Request.new("POST", "/?hasan=cemal")
query_params = Kemal::ParamParser.new(request).query query_params = Kemal::ParamParser.new(request).query
query_params["hasan"].should eq "cemal" query_params["hasan"].should eq "cemal"
end end
context "#query" do
it "parses multiple values for query params" do it "parses multiple values for query params" do
route = Route.new "POST", "/" do |env|
hasan = env.params.query["hasan"]
"Hello #{hasan}"
end
request = HTTP::Request.new("POST", "/?hasan=cemal&hasan=lamec") request = HTTP::Request.new("POST", "/?hasan=cemal&hasan=lamec")
query_params = Kemal::ParamParser.new(request).query query_params = Kemal::ParamParser.new(request).query
query_params["hasan"].should eq ["cemal", "lamec"] query_params.fetch_all("hasan").should eq ["cemal", "lamec"]
end
end end
context "#url" do
it "parses url params" do it "parses url params" do
kemal = Kemal::RouteHandler::INSTANCE kemal = Kemal::RouteHandler::INSTANCE
kemal.add_route "POST", "/hello/:hasan" do |env| kemal.add_route "POST", "/hello/:hasan" do |env|
@ -44,10 +49,15 @@ describe "ParamParser" do
url_params["money"].should eq "$19.99" url_params["money"].should eq "$19.99"
url_params["spanish"].should eq "año" url_params["spanish"].should eq "año"
end end
it "parses request body" do
route = Route.new "POST", "/" do |env|
name = env.params.query["name"]
age = env.params.query["age"]
hasan = env.params.body["hasan"]
"Hello #{name} #{hasan} #{age}"
end end
context "#body" do
it "parses request body" do
request = HTTP::Request.new( request = HTTP::Request.new(
"POST", "POST",
"/?hasan=cemal", "/?hasan=cemal",
@ -67,6 +77,11 @@ describe "ParamParser" do
end end
it "parses multiple values in request body" do it "parses multiple values in request body" do
route = Route.new "POST", "/" do |env|
hasan = env.params.body["hasan"]
"Hello #{hasan}"
end
request = HTTP::Request.new( request = HTTP::Request.new(
"POST", "POST",
"/", "/",
@ -75,25 +90,13 @@ describe "ParamParser" do
) )
body_params = Kemal::ParamParser.new(request).body body_params = Kemal::ParamParser.new(request).body
body_params["hasan"].should eq(["cemal", "lamec"]) body_params.fetch_all("hasan").should eq(["cemal", "lamec"])
end end
it "parses array values in request body" do context "when content type is application/json" do
request = HTTP::Request.new(
"POST",
"/",
body: "framework=kemal&feats[]=fast&feats[]=effective&feats[]=simple",
headers: HTTP::Headers{"Content-Type" => "application/x-www-form-urlencoded"},
)
body_params = Kemal::ParamParser.new(request).body
body_params["framework"].should eq("kemal")
body_params["feats[]"].should eq(["fast", "effective", "simple"])
end
end
context "#json" do
it "parses request body" do it "parses request body" do
route = Route.new "POST", "/" { }
request = HTTP::Request.new( request = HTTP::Request.new(
"POST", "POST",
"/", "/",
@ -106,6 +109,8 @@ describe "ParamParser" do
end end
it "parses request body when passed charset" do it "parses request body when passed charset" do
route = Route.new "POST", "/" { }
request = HTTP::Request.new( request = HTTP::Request.new(
"POST", "POST",
"/", "/",
@ -118,6 +123,8 @@ describe "ParamParser" do
end end
it "parses request body for array" do it "parses request body for array" do
route = Route.new "POST", "/" { }
request = HTTP::Request.new( request = HTTP::Request.new(
"POST", "POST",
"/", "/",
@ -130,6 +137,8 @@ describe "ParamParser" do
end end
it "parses request body and query params" do it "parses request body and query params" do
route = Route.new "POST", "/" { }
request = HTTP::Request.new( request = HTTP::Request.new(
"POST", "POST",
"/?foo=bar", "/?foo=bar",
@ -147,6 +156,8 @@ describe "ParamParser" do
end end
it "handles no request body" do it "handles no request body" do
route = Route.new "GET", "/" { }
request = HTTP::Request.new( request = HTTP::Request.new(
"GET", "GET",
"/", "/",
@ -169,6 +180,13 @@ describe "ParamParser" do
context "when content type is incorrect" do context "when content type is incorrect" do
it "does not parse request body" do it "does not parse request body" do
route = Route.new "POST", "/" do |env|
name = env.params.body["name"]
age = env.params.body["age"]
hasan = env.params.query["hasan"]
"Hello #{name} #{hasan} #{age}"
end
request = HTTP::Request.new( request = HTTP::Request.new(
"POST", "POST",
"/?hasan=cemal", "/?hasan=cemal",

View file

@ -1,8 +0,0 @@
module HTTP
struct Params
def [](name)
params = raw_params[name]
params.size == 1 ? params.first : params
end
end
end

View file

@ -1,5 +1,5 @@
class HTTP::Request class HTTP::Request
property override_method : String? property override_method
property url_params : Hash(String, String)? property url_params : Hash(String, String)?
getter param_parser : Kemal::ParamParser? getter param_parser : Kemal::ParamParser?
@ -21,7 +21,7 @@ class HTTP::Request
@param_parser = Kemal::ParamParser.new(self) @param_parser = Kemal::ParamParser.new(self)
params = @param_parser.not_nil!.body params = @param_parser.not_nil!.body
if params.has_key?("_method") && HTTP::Request.override_method_valid?(params["_method"]) if params.has_key?("_method") && HTTP::Request.override_method_valid?(params["_method"])
@override_method = params["_method"].as(String) @override_method = params["_method"]
end end
end end
@override_method @override_method