mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Added JSON body parsing
This commit is contained in:
parent
5e1460bd31
commit
3687897005
3 changed files with 93 additions and 1 deletions
|
@ -40,4 +40,66 @@ describe "Kemal::Handler" do
|
|||
response = kemal.call(request)
|
||||
response.body.should eq("hello world")
|
||||
end
|
||||
|
||||
it "parses simple JSON body" do
|
||||
kemal = Kemal::Handler.new
|
||||
kemal.add_route "POST", "/" do |env|
|
||||
name = env.params["name"]
|
||||
age = env.params["age"]
|
||||
"Hello #{name} Age #{age}"
|
||||
end
|
||||
|
||||
json_payload = {"name": "Serdar", "age": 26}
|
||||
request = HTTP::Request.new(
|
||||
"POST",
|
||||
"/",
|
||||
body: json_payload.to_json,
|
||||
headers: HTTP::Headers{"Content-Type": "application/json"},
|
||||
)
|
||||
|
||||
response = kemal.call(request)
|
||||
response.body.should eq("Hello Serdar Age 26")
|
||||
end
|
||||
|
||||
it "parses JSON with string array" do
|
||||
kemal = Kemal::Handler.new
|
||||
kemal.add_route "POST", "/" do |env|
|
||||
skills = env.params["skills"] as Array
|
||||
"Skills #{skills.each.join(',')}"
|
||||
end
|
||||
|
||||
json_payload = {"skills": ["ruby", "crystal"]}
|
||||
request = HTTP::Request.new(
|
||||
"POST",
|
||||
"/",
|
||||
body: json_payload.to_json,
|
||||
headers: HTTP::Headers{"Content-Type": "application/json"},
|
||||
)
|
||||
|
||||
response = kemal.call(request)
|
||||
response.body.should eq("Skills ruby,crystal")
|
||||
end
|
||||
|
||||
it "parses JSON with json object array" do
|
||||
kemal = Kemal::Handler.new
|
||||
kemal.add_route "POST", "/" do |env|
|
||||
skills = env.params["skills"] as Array
|
||||
skills_from_languages = skills.map do |skill|
|
||||
skill = skill as Hash
|
||||
skill["language"]
|
||||
end
|
||||
"Skills #{skills_from_languages.each.join(',')}"
|
||||
end
|
||||
|
||||
json_payload = {"skills": [{"language": "ruby"}, {"language": "crystal"}]}
|
||||
request = HTTP::Request.new(
|
||||
"POST",
|
||||
"/",
|
||||
body: json_payload.to_json,
|
||||
headers: HTTP::Headers{"Content-Type": "application/json"},
|
||||
)
|
||||
|
||||
response = kemal.call(request)
|
||||
response.body.should eq("Skills ruby,crystal")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -30,6 +30,20 @@ describe "ParamParser" do
|
|||
params.should eq({"hasan" => "cemal", "name" => "serdar", "age" => "99"})
|
||||
end
|
||||
|
||||
it "parses request body" do
|
||||
route = Route.new "POST", "/" { }
|
||||
|
||||
request = HTTP::Request.new(
|
||||
"POST",
|
||||
"/",
|
||||
body: "{\"name\": \"Serdar\"}",
|
||||
headers: HTTP::Headers{"Content-Type": "application/json"},
|
||||
)
|
||||
|
||||
params = Kemal::ParamParser.new(route, request).parse
|
||||
params.should eq({"name": "Serdar"})
|
||||
end
|
||||
|
||||
context "when content type is incorrect" do
|
||||
it "does not parse request body" do
|
||||
route = Route.new "POST", "/" do |env|
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue