Merge pull request #10 from botandrose/get_json_with_no_request_body

application/json GETs can have no request body
This commit is contained in:
Serdar Dogruyol 2015-11-10 10:58:27 +02:00
commit da0d532447
2 changed files with 26 additions and 11 deletions

View file

@ -30,18 +30,33 @@ describe "ParamParser" do
params.should eq({"hasan" => "cemal", "name" => "serdar", "age" => "99"}) params.should eq({"hasan" => "cemal", "name" => "serdar", "age" => "99"})
end end
it "parses request body" do context "when content type is application/json" do
route = Route.new "POST", "/" { } it "parses request body" do
route = Route.new "POST", "/" { }
request = HTTP::Request.new( request = HTTP::Request.new(
"POST", "POST",
"/", "/",
body: "{\"name\": \"Serdar\"}", body: "{\"name\": \"Serdar\"}",
headers: HTTP::Headers{"Content-Type": "application/json"}, headers: HTTP::Headers{"Content-Type": "application/json"},
) )
params = Kemal::ParamParser.new(route, request).parse params = Kemal::ParamParser.new(route, request).parse
params.should eq({"name": "Serdar"}) params.should eq({"name": "Serdar"})
end
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
end end
context "when content type is incorrect" do context "when content type is incorrect" do

View file

@ -38,7 +38,7 @@ class Kemal::ParamParser
end end
def parse_json def parse_json
return unless @request.headers["Content-Type"]? == APPLICATION_JSON return unless @request.body && @request.headers["Content-Type"]? == APPLICATION_JSON
body = @request.body as String body = @request.body as String
json = JSON.parse(body) as Hash json = JSON.parse(body) as Hash
json.each do |k, v| json.each do |k, v|