mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Merge pull request #18 from ilatif/master
Added support for magic param `_method` just like Rails, Sinatra.
This commit is contained in:
		
						commit
						7e04f51e3c
					
				
					 4 changed files with 73 additions and 2 deletions
				
			
		|  | @ -120,4 +120,51 @@ describe "Kemal::Handler" do | |||
|     response.status_code.should eq 500 | ||||
|     response.body.includes?("Exception").should eq true | ||||
|   end | ||||
| 
 | ||||
|   it "checks for _method param in POST request to simulate PUT" do | ||||
|     kemal = Kemal::Handler.new | ||||
|     kemal.add_route "PUT", "/", do |env| | ||||
|       "Hello World from PUT" | ||||
|     end | ||||
|     request = HTTP::Request.new( | ||||
|       "POST", | ||||
|       "/", | ||||
|       body: "_method=PUT", | ||||
|       headers: HTTP::Headers{"Content-Type": "application/x-www-form-urlencoded"} | ||||
|     ) | ||||
|     response = kemal.call(request) | ||||
|     response.body.should eq("Hello World from PUT") | ||||
|   end | ||||
| 
 | ||||
|   it "checks for _method param in POST request to simulate PATCH" do | ||||
|     kemal = Kemal::Handler.new | ||||
|     kemal.add_route "PATCH", "/", do |env| | ||||
|       "Hello World from PATCH" | ||||
|     end | ||||
|     request = HTTP::Request.new( | ||||
|       "POST", | ||||
|       "/", | ||||
|       body: "_method=PATCH", | ||||
|       headers: HTTP::Headers{"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"} | ||||
|     ) | ||||
|     response = kemal.call(request) | ||||
|     response.body.should eq("Hello World from PATCH") | ||||
|   end | ||||
| 
 | ||||
|   it "checks for _method param in POST request to simulate DELETE" do | ||||
|     kemal = Kemal::Handler.new | ||||
|     kemal.add_route "DELETE", "/", do |env| | ||||
|       "Hello World from DELETE" | ||||
|     end | ||||
|     json_payload = {"_method": "DELETE"} | ||||
|     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 World from DELETE") | ||||
|   end | ||||
| 
 | ||||
| end | ||||
|  |  | |||
|  | @ -29,7 +29,7 @@ class Kemal::ParamParser | |||
|   end | ||||
| 
 | ||||
|   def parse_body | ||||
|     return unless @request.headers["Content-Type"]? == URL_ENCODED_FORM | ||||
|     return if (@request.headers["Content-Type"]? =~ /#{URL_ENCODED_FORM}/).nil? | ||||
|     parse_part(@request.body) | ||||
|   end | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										4
									
								
								src/kemal/request.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								src/kemal/request.cr
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,4 @@ | |||
| # Opening HTTP::Request to add override_method property | ||||
| class HTTP::Request | ||||
| 	property override_method | ||||
| end | ||||
|  | @ -10,7 +10,9 @@ class Kemal::Route | |||
|   end | ||||
| 
 | ||||
|   def match?(request) | ||||
|     return nil unless request.method == @method | ||||
|     check_for_method_override!(request) | ||||
| 
 | ||||
|     return nil unless request.override_method == @method | ||||
|     components = request.path.not_nil!.split "/" | ||||
|     return nil unless components.size == @components.size | ||||
|     @components.zip(components) do |route_component, req_component| | ||||
|  | @ -20,4 +22,22 @@ class Kemal::Route | |||
|     end | ||||
|     true | ||||
|   end | ||||
| 
 | ||||
|   # checks if request params contain _method param to override request incoming method | ||||
|   def check_for_method_override!(request) | ||||
|     request.override_method = request.method | ||||
|     if request.method == "POST" | ||||
|       params = Kemal::ParamParser.new(self, request).parse_request | ||||
|       if params.has_key?("_method") && self.override_method_valid?(params["_method"]) | ||||
|         request.override_method = params["_method"] | ||||
|       end | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   # checks if method contained in _method param is valid one | ||||
|   def override_method_valid?(override_method) | ||||
|     return false unless override_method.is_a?(String) | ||||
|     override_method = override_method.upcase | ||||
|     return (override_method == "PUT" || override_method == "PATCH" || override_method == "DELETE") | ||||
|   end | ||||
| end | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue