2015-10-28 18:30:27 +00:00
|
|
|
require "./spec_helper"
|
|
|
|
|
|
|
|
describe "ParamParser" do
|
2015-10-28 18:38:24 +00:00
|
|
|
it "parses query params" do
|
|
|
|
route = Route.new "POST", "/" do |env|
|
2015-10-31 06:53:49 +00:00
|
|
|
hasan = env.params["hasan"]
|
|
|
|
"Hello #{hasan}"
|
|
|
|
end
|
2015-10-28 18:38:24 +00:00
|
|
|
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|
|
2015-10-31 06:53:49 +00:00
|
|
|
name = env.params["name"]
|
|
|
|
age = env.params["age"]
|
|
|
|
hasan = env.params["hasan"]
|
|
|
|
"Hello #{name} #{hasan} #{age}"
|
|
|
|
end
|
2015-11-05 10:38:06 +00:00
|
|
|
|
|
|
|
request = HTTP::Request.new(
|
|
|
|
"POST",
|
|
|
|
"/?hasan=cemal",
|
|
|
|
body: "name=serdar&age=99",
|
|
|
|
headers: HTTP::Headers{"Content-Type": "application/x-www-form-urlencoded"},
|
|
|
|
)
|
|
|
|
|
2015-10-28 18:38:24 +00:00
|
|
|
params = Kemal::ParamParser.new(route, request).parse
|
|
|
|
params.should eq({"hasan" => "cemal", "name" => "serdar", "age" => "99"})
|
2015-10-28 18:30:27 +00:00
|
|
|
end
|
2015-11-05 10:38:06 +00:00
|
|
|
|
2015-11-10 00:35:28 +00:00
|
|
|
context "when content type is application/json" do
|
|
|
|
it "parses request body" do
|
|
|
|
route = Route.new "POST", "/" { }
|
2015-11-06 18:24:38 +00:00
|
|
|
|
2015-11-10 00:35:28 +00:00
|
|
|
request = HTTP::Request.new(
|
|
|
|
"POST",
|
|
|
|
"/",
|
|
|
|
body: "{\"name\": \"Serdar\"}",
|
|
|
|
headers: HTTP::Headers{"Content-Type": "application/json"},
|
|
|
|
)
|
2015-11-06 18:24:38 +00:00
|
|
|
|
2015-11-10 00:35:28 +00:00
|
|
|
params = Kemal::ParamParser.new(route, request).parse
|
|
|
|
params.should eq({"name": "Serdar"})
|
|
|
|
end
|
|
|
|
|
2015-11-10 11:30:16 +00:00
|
|
|
it "parses request body for array" do
|
|
|
|
route = Route.new "POST", "/" { }
|
|
|
|
|
|
|
|
request = HTTP::Request.new(
|
|
|
|
"POST",
|
|
|
|
"/",
|
|
|
|
body: "[1]",
|
|
|
|
headers: HTTP::Headers{"Content-Type": "application/json"},
|
|
|
|
)
|
|
|
|
|
|
|
|
params = Kemal::ParamParser.new(route, request).parse
|
|
|
|
params.should eq({"_json": [1]})
|
|
|
|
end
|
|
|
|
|
|
|
|
it "parses request body and query params" do
|
|
|
|
route = Route.new "POST", "/" { }
|
|
|
|
|
|
|
|
request = HTTP::Request.new(
|
|
|
|
"POST",
|
|
|
|
"/?foo=bar",
|
|
|
|
body: "[1]",
|
|
|
|
headers: HTTP::Headers{"Content-Type": "application/json"},
|
|
|
|
)
|
|
|
|
|
|
|
|
params = Kemal::ParamParser.new(route, request).parse
|
|
|
|
params.should eq({"foo": "bar", "_json": [1]})
|
|
|
|
end
|
|
|
|
|
2015-11-10 00:35:28 +00:00
|
|
|
it "handles no request body" do
|
|
|
|
route = Route.new "GET", "/" { }
|
|
|
|
|
|
|
|
request = HTTP::Request.new(
|
|
|
|
"GET",
|
|
|
|
"/",
|
|
|
|
headers: HTTP::Headers{"Content-Type": "application/json"},
|
|
|
|
)
|
|
|
|
|
|
|
|
params = Kemal::ParamParser.new(route, request).parse
|
|
|
|
params.should eq({} of String => AllParamTypes)
|
|
|
|
end
|
2015-11-06 18:24:38 +00:00
|
|
|
end
|
|
|
|
|
2015-11-05 10:38:06 +00:00
|
|
|
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"]
|
|
|
|
"Hello #{name} #{hasan} #{age}"
|
|
|
|
end
|
|
|
|
|
|
|
|
request = HTTP::Request.new(
|
|
|
|
"POST",
|
|
|
|
"/?hasan=cemal",
|
|
|
|
body: "name=serdar&age=99",
|
|
|
|
headers: HTTP::Headers{"Content-Type": "text/plain"},
|
|
|
|
)
|
|
|
|
|
|
|
|
params = Kemal::ParamParser.new(route, request).parse
|
|
|
|
params.should eq({"hasan" => "cemal"})
|
|
|
|
end
|
|
|
|
end
|
2015-10-28 18:30:27 +00:00
|
|
|
end
|