mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	application/json GETs can have no request body.
This commit is contained in:
		
							parent
							
								
									d8ab11f73f
								
							
						
					
					
						commit
						dea9b2f663
					
				
					 2 changed files with 26 additions and 11 deletions
				
			
		|  | @ -30,18 +30,33 @@ describe "ParamParser" do | |||
|     params.should eq({"hasan" => "cemal", "name" => "serdar", "age" => "99"}) | ||||
|   end | ||||
| 
 | ||||
|   it "parses request body" do | ||||
|     route = Route.new "POST", "/" { } | ||||
|   context "when content type is application/json" do | ||||
|     it "parses request body" do | ||||
|       route = Route.new "POST", "/" { } | ||||
| 
 | ||||
|     request = HTTP::Request.new( | ||||
|       "POST", | ||||
|       "/", | ||||
|       body: "{\"name\": \"Serdar\"}", | ||||
|       headers: HTTP::Headers{"Content-Type": "application/json"}, | ||||
|     ) | ||||
|       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"}) | ||||
|       params = Kemal::ParamParser.new(route, request).parse | ||||
|       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 | ||||
| 
 | ||||
|   context "when content type is incorrect" do | ||||
|  |  | |||
|  | @ -38,7 +38,7 @@ class Kemal::ParamParser | |||
|   end | ||||
| 
 | ||||
|   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 | ||||
|     json = JSON.parse(body) as Hash | ||||
|     json.each do |k, v| | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue