mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Remove _method override
This commit is contained in:
parent
b8ec6ee328
commit
ad5dc053c4
5 changed files with 6 additions and 76 deletions
|
@ -102,48 +102,6 @@ describe "Kemal::RouteHandler" do
|
||||||
client_response.body.should eq("Skills ruby,crystal")
|
client_response.body.should eq("Skills ruby,crystal")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "checks for _method param in POST request to simulate PUT" do
|
|
||||||
put "/" do
|
|
||||||
"Hello World from PUT"
|
|
||||||
end
|
|
||||||
request = HTTP::Request.new(
|
|
||||||
"POST",
|
|
||||||
"/",
|
|
||||||
body: "_method=PUT",
|
|
||||||
headers: HTTP::Headers{"Content-Type" => "application/x-www-form-urlencoded"}
|
|
||||||
)
|
|
||||||
client_response = call_request_on_app(request)
|
|
||||||
client_response.body.should eq("Hello World from PUT")
|
|
||||||
end
|
|
||||||
|
|
||||||
it "checks for _method param in POST request to simulate PATCH" do
|
|
||||||
patch "/" do
|
|
||||||
"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"}
|
|
||||||
)
|
|
||||||
client_response = call_request_on_app(request)
|
|
||||||
client_response.body.should eq("Hello World from PATCH")
|
|
||||||
end
|
|
||||||
|
|
||||||
it "checks for _method param in POST request to simulate DELETE" do
|
|
||||||
delete "/" do
|
|
||||||
"Hello World from DELETE"
|
|
||||||
end
|
|
||||||
request = HTTP::Request.new(
|
|
||||||
"POST",
|
|
||||||
"/",
|
|
||||||
body: "_method=DELETE",
|
|
||||||
headers: HTTP::Headers{"Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8"}
|
|
||||||
)
|
|
||||||
client_response = call_request_on_app(request)
|
|
||||||
client_response.body.should eq("Hello World from DELETE")
|
|
||||||
end
|
|
||||||
|
|
||||||
it "can process HTTP HEAD requests for defined GET routes" do
|
it "can process HTTP HEAD requests for defined GET routes" do
|
||||||
get "/" do
|
get "/" do
|
||||||
"Hello World from GET"
|
"Hello World from GET"
|
||||||
|
|
|
@ -15,11 +15,8 @@ class HTTP::Server
|
||||||
|
|
||||||
def params
|
def params
|
||||||
@request.url_params ||= route_lookup.params
|
@request.url_params ||= route_lookup.params
|
||||||
@params ||= if @request.param_parser
|
@params ||= Kemal::ParamParser.new(@request)
|
||||||
@request.param_parser.not_nil!
|
|
||||||
else
|
|
||||||
Kemal::ParamParser.new(@request)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def redirect(url : String, status_code : Int32 = 302)
|
def redirect(url : String, status_code : Int32 = 302)
|
||||||
|
@ -36,7 +33,7 @@ class HTTP::Server
|
||||||
end
|
end
|
||||||
|
|
||||||
def route_lookup
|
def route_lookup
|
||||||
Kemal::RouteHandler::INSTANCE.lookup_route(@request.override_method.as(String), @request.path)
|
Kemal::RouteHandler::INSTANCE.lookup_route(@request.method.as(String), @request.path)
|
||||||
end
|
end
|
||||||
|
|
||||||
def route_defined?
|
def route_defined?
|
||||||
|
|
|
@ -1,32 +1,7 @@
|
||||||
class HTTP::Request
|
class HTTP::Request
|
||||||
property override_method
|
|
||||||
property url_params : Hash(String, String)?
|
property url_params : Hash(String, String)?
|
||||||
getter param_parser : Kemal::ParamParser?
|
|
||||||
|
|
||||||
def override_method
|
|
||||||
@override_method ||= check_for_method_override!
|
|
||||||
end
|
|
||||||
|
|
||||||
def content_type
|
def content_type
|
||||||
@headers["Content-Type"]?
|
@headers["Content-Type"]?
|
||||||
end
|
end
|
||||||
|
|
||||||
# Checks if method contained in _method param is valid one
|
|
||||||
def self.override_method_valid?(override_method : String)
|
|
||||||
override_method = override_method.upcase
|
|
||||||
override_method == "PUT" || override_method == "PATCH" || override_method == "DELETE"
|
|
||||||
end
|
|
||||||
|
|
||||||
# Checks if request params contain _method param to override request incoming method
|
|
||||||
private def check_for_method_override!
|
|
||||||
@override_method = @method
|
|
||||||
if @method == "POST"
|
|
||||||
@param_parser = Kemal::ParamParser.new(self)
|
|
||||||
params = @param_parser.not_nil!.body
|
|
||||||
if params.has_key?("_method") && HTTP::Request.override_method_valid?(params["_method"])
|
|
||||||
@override_method = params["_method"]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@override_method
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,12 +14,12 @@ module Kemal
|
||||||
def call(context : HTTP::Server::Context)
|
def call(context : HTTP::Server::Context)
|
||||||
return call_next(context) unless context.route_defined?
|
return call_next(context) unless context.route_defined?
|
||||||
call_block_for_path_type("ALL", context.request.path, :before, context)
|
call_block_for_path_type("ALL", context.request.path, :before, context)
|
||||||
call_block_for_path_type(context.request.override_method, context.request.path, :before, context)
|
call_block_for_path_type(context.request.method, context.request.path, :before, context)
|
||||||
if Kemal.config.error_handlers.has_key?(context.response.status_code)
|
if Kemal.config.error_handlers.has_key?(context.response.status_code)
|
||||||
raise Kemal::Exceptions::CustomException.new(context)
|
raise Kemal::Exceptions::CustomException.new(context)
|
||||||
end
|
end
|
||||||
call_next(context)
|
call_next(context)
|
||||||
call_block_for_path_type(context.request.override_method, context.request.path, :after, context)
|
call_block_for_path_type(context.request.method, context.request.path, :after, context)
|
||||||
call_block_for_path_type("ALL", context.request.path, :after, context)
|
call_block_for_path_type("ALL", context.request.path, :after, context)
|
||||||
context
|
context
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,7 +8,7 @@ module Kemal::Exceptions
|
||||||
|
|
||||||
class RouteNotFound < Exception
|
class RouteNotFound < Exception
|
||||||
def initialize(context : HTTP::Server::Context)
|
def initialize(context : HTTP::Server::Context)
|
||||||
super "Requested path: '#{context.request.override_method}:#{context.request.path}' was not found."
|
super "Requested path: '#{context.request.method}:#{context.request.path}' was not found."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue