mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Merge pull request #161 from MGerrior/allow_multiple_values_for_params
Allow multiple values for a single parameter key.
This commit is contained in:
commit
74044d62e9
2 changed files with 43 additions and 16 deletions
|
@ -11,6 +11,16 @@ describe "ParamParser" do
|
|||
query_params["hasan"].should eq "cemal"
|
||||
end
|
||||
|
||||
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")
|
||||
query_params = Kemal::ParamParser.new(request).query
|
||||
query_params.fetch_all("hasan").should eq ["cemal", "lamec"]
|
||||
end
|
||||
|
||||
it "parses url params" do
|
||||
kemal = Kemal::RouteHandler::INSTANCE
|
||||
kemal.add_route "POST", "/hello/:hasan" do |env|
|
||||
|
@ -39,10 +49,31 @@ describe "ParamParser" do
|
|||
)
|
||||
|
||||
query_params = Kemal::ParamParser.new(request).query
|
||||
query_params.should eq({"hasan" => "cemal"})
|
||||
{"hasan": "cemal"}.each do |k, v|
|
||||
query_params[k].should eq(v)
|
||||
end
|
||||
|
||||
body_params = Kemal::ParamParser.new(request).body
|
||||
body_params.should eq({"name" => "serdar", "age" => "99"})
|
||||
{"name": "serdar", "age": "99"}.each do |k, v|
|
||||
body_params[k].should eq(v)
|
||||
end
|
||||
end
|
||||
|
||||
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(
|
||||
"POST",
|
||||
"/",
|
||||
body: "hasan=cemal&hasan=lamec",
|
||||
headers: HTTP::Headers{"Content-Type": "application/x-www-form-urlencoded"},
|
||||
)
|
||||
|
||||
body_params = Kemal::ParamParser.new(request).body
|
||||
body_params.fetch_all("hasan").should eq(["cemal", "lamec"])
|
||||
end
|
||||
|
||||
context "when content type is application/json" do
|
||||
|
@ -85,7 +116,9 @@ describe "ParamParser" do
|
|||
)
|
||||
|
||||
query_params = Kemal::ParamParser.new(request).query
|
||||
query_params.should eq({"foo": "bar"})
|
||||
{"foo": "bar"}.each do |k, v|
|
||||
query_params[k].should eq(v)
|
||||
end
|
||||
|
||||
json_params = Kemal::ParamParser.new(request).json
|
||||
json_params.should eq({"_json": [1]})
|
||||
|
@ -104,10 +137,10 @@ describe "ParamParser" do
|
|||
url_params.should eq({} of String => String)
|
||||
|
||||
query_params = Kemal::ParamParser.new(request).query
|
||||
query_params.should eq({} of String => String)
|
||||
query_params.to_s.should eq("")
|
||||
|
||||
body_params = Kemal::ParamParser.new(request).body
|
||||
body_params.should eq({} of String => String)
|
||||
body_params.to_s.should eq("")
|
||||
|
||||
json_params = Kemal::ParamParser.new(request).json
|
||||
json_params.should eq({} of String => AllParamTypes)
|
||||
|
@ -131,10 +164,10 @@ describe "ParamParser" do
|
|||
)
|
||||
|
||||
query_params = Kemal::ParamParser.new(request).query
|
||||
query_params.should eq({"hasan" => "cemal"})
|
||||
query_params["hasan"].should eq("cemal")
|
||||
|
||||
body_params = Kemal::ParamParser.new(request).body
|
||||
body_params.should eq({} of String => String)
|
||||
body_params.to_s.should eq("")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,8 +11,8 @@ class Kemal::ParamParser
|
|||
|
||||
def initialize(@request : HTTP::Request)
|
||||
@url = {} of String => String
|
||||
@query = {} of String => String
|
||||
@body = {} of String => String
|
||||
@query = HTTP::Params.new({} of String => Array(String))
|
||||
@body = HTTP::Params.new({} of String => Array(String))
|
||||
@json = {} of String => AllParamTypes
|
||||
@url_parsed = false
|
||||
@query_parsed = false
|
||||
|
@ -68,12 +68,6 @@ class Kemal::ParamParser
|
|||
end
|
||||
|
||||
def parse_part(part)
|
||||
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
|
||||
HTTP::Params.parse(part || "")
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue