mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Opt for built-in HTTP::Params class instead.
This commit is contained in:
		
							parent
							
								
									a5e4bc21f3
								
							
						
					
					
						commit
						d1f9c4394b
					
				
					 4 changed files with 20 additions and 59 deletions
				
			
		|  | @ -18,7 +18,7 @@ describe "ParamParser" do | ||||||
|     end |     end | ||||||
|     request = HTTP::Request.new("POST", "/?hasan=cemal&hasan=lamec") |     request = HTTP::Request.new("POST", "/?hasan=cemal&hasan=lamec") | ||||||
|     query_params = Kemal::ParamParser.new(request).query |     query_params = Kemal::ParamParser.new(request).query | ||||||
|     query_params["hasan"].should eq ["cemal", "lamec"] |     query_params.fetch_all("hasan").should eq ["cemal", "lamec"] | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   it "parses url params" do |   it "parses url params" do | ||||||
|  | @ -49,10 +49,14 @@ describe "ParamParser" do | ||||||
|     ) |     ) | ||||||
| 
 | 
 | ||||||
|     query_params = Kemal::ParamParser.new(request).query |     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 = 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 |   end | ||||||
| 
 | 
 | ||||||
|   it "parses multiple values in request body" do |   it "parses multiple values in request body" do | ||||||
|  | @ -69,7 +73,7 @@ describe "ParamParser" do | ||||||
|     ) |     ) | ||||||
| 
 | 
 | ||||||
|     body_params = Kemal::ParamParser.new(request).body |     body_params = Kemal::ParamParser.new(request).body | ||||||
|     body_params.should eq({"hasan" => ["cemal", "lamec"]}) |     body_params.fetch_all("hasan").should eq(["cemal", "lamec"]) | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   context "when content type is application/json" do |   context "when content type is application/json" do | ||||||
|  | @ -112,7 +116,9 @@ describe "ParamParser" do | ||||||
|       ) |       ) | ||||||
| 
 | 
 | ||||||
|       query_params = Kemal::ParamParser.new(request).query |       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 = Kemal::ParamParser.new(request).json | ||||||
|       json_params.should eq({"_json": [1]}) |       json_params.should eq({"_json": [1]}) | ||||||
|  | @ -131,10 +137,10 @@ describe "ParamParser" do | ||||||
|       url_params.should eq({} of String => String) |       url_params.should eq({} of String => String) | ||||||
| 
 | 
 | ||||||
|       query_params = Kemal::ParamParser.new(request).query |       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 = 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 = Kemal::ParamParser.new(request).json | ||||||
|       json_params.should eq({} of String => AllParamTypes) |       json_params.should eq({} of String => AllParamTypes) | ||||||
|  | @ -158,10 +164,10 @@ describe "ParamParser" do | ||||||
|       ) |       ) | ||||||
| 
 | 
 | ||||||
|       query_params = Kemal::ParamParser.new(request).query |       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 = Kemal::ParamParser.new(request).body | ||||||
|       body_params.should eq({} of String => String) |       body_params.to_s.should eq("") | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
| end | end | ||||||
|  |  | ||||||
|  | @ -1,14 +0,0 @@ | ||||||
| require "./spec_helper" |  | ||||||
| 
 |  | ||||||
| describe "Request" do |  | ||||||
|   it "uses the last method override provided" do |  | ||||||
|     request = HTTP::Request.new( |  | ||||||
|       "POST", |  | ||||||
|       "/", |  | ||||||
|       body: "_method=PUT&_method=PATCH", |  | ||||||
|       headers: HTTP::Headers{"Content-Type": "application/x-www-form-urlencoded"}, |  | ||||||
|     ) |  | ||||||
| 
 |  | ||||||
|     request.override_method.should eq("PATCH") |  | ||||||
|   end |  | ||||||
| end |  | ||||||
|  | @ -11,8 +11,8 @@ class Kemal::ParamParser | ||||||
| 
 | 
 | ||||||
|   def initialize(@request : HTTP::Request) |   def initialize(@request : HTTP::Request) | ||||||
|     @url = {} of String => String |     @url = {} of String => String | ||||||
|     @query = {} of String => String | Array(String) |     @query = HTTP::Params.new({} of String => Array(String)) | ||||||
|     @body = {} of String => String | Array(String) |     @body = HTTP::Params.new({} of String => Array(String)) | ||||||
|     @json = {} of String => AllParamTypes |     @json = {} of String => AllParamTypes | ||||||
|     @url_parsed = false |     @url_parsed = false | ||||||
|     @query_parsed = false |     @query_parsed = false | ||||||
|  | @ -68,26 +68,6 @@ class Kemal::ParamParser | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def parse_part(part) |   def parse_part(part) | ||||||
|     part_params = {} of String => String | Array(String) |     HTTP::Params.parse(part || "") | ||||||
| 
 |  | ||||||
|     if part |  | ||||||
|       HTTP::Params.parse(part) do |key, value| |  | ||||||
|         key_string = key as String |  | ||||||
|         value_string = value as String |  | ||||||
|         current_value = part_params[key_string]? |  | ||||||
| 
 |  | ||||||
|         part_params[key_string] = if current_value |  | ||||||
|                                     if current_value.is_a?(Array) |  | ||||||
|                                       current_value << value_string |  | ||||||
|                                     else |  | ||||||
|                                       [current_value, value_string] |  | ||||||
|                                     end |  | ||||||
|                                   else |  | ||||||
|                                     value_string |  | ||||||
|                                   end |  | ||||||
|       end |  | ||||||
|     end |  | ||||||
| 
 |  | ||||||
|     part_params |  | ||||||
|   end |   end | ||||||
| end | end | ||||||
|  |  | ||||||
|  | @ -10,29 +10,18 @@ class HTTP::Request | ||||||
|   # Checks if request params contain _method param to override request incoming method |   # Checks if request params contain _method param to override request incoming method | ||||||
|   private def check_for_method_override! |   private def check_for_method_override! | ||||||
|     @override_method = @method |     @override_method = @method | ||||||
| 
 |  | ||||||
|     if @method == "POST" |     if @method == "POST" | ||||||
|       params = Kemal::ParamParser.new(self).body |       params = Kemal::ParamParser.new(self).body | ||||||
| 
 |  | ||||||
|       if params.has_key?("_method") && HTTP::Request.override_method_valid?(params["_method"]) |       if params.has_key?("_method") && HTTP::Request.override_method_valid?(params["_method"]) | ||||||
| 	_method = params["_method"] |         @override_method = params["_method"] | ||||||
| 
 |  | ||||||
|         @override_method = if _method.is_a?(Array) |  | ||||||
|           _method.last |  | ||||||
|         else |  | ||||||
|           _method |  | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
|     end |  | ||||||
| 
 |  | ||||||
|     @override_method |     @override_method | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   # Checks if method contained in _method param is valid one |   # Checks if method contained in _method param is valid one | ||||||
|   def self.override_method_valid?(override_method) |   def self.override_method_valid?(override_method) | ||||||
|     return false unless override_method.is_a?(String) || override_method.is_a?(Array(String)) |     return false unless override_method.is_a?(String) | ||||||
| 
 |  | ||||||
|     override_method = override_method.last if override_method.is_a?(Array(String)) |  | ||||||
|     override_method = override_method.upcase |     override_method = override_method.upcase | ||||||
|     override_method == "PUT" || override_method == "PATCH" || override_method == "DELETE" |     override_method == "PUT" || override_method == "PATCH" || override_method == "DELETE" | ||||||
|   end |   end | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue