From 14aabb8907a624972847a50eec6632ac191d0423 Mon Sep 17 00:00:00 2001 From: Anton Maminov Date: Wed, 1 Sep 2021 16:25:21 +0300 Subject: [PATCH] fixed executing filters when before and after is defined at the same time (#612) --- spec/middleware/filters_spec.cr | 24 ++++++++++++++++++++++++ src/kemal/filter_handler.cr | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/spec/middleware/filters_spec.cr b/spec/middleware/filters_spec.cr index 9bc2564..5c3b477 100644 --- a/spec/middleware/filters_spec.cr +++ b/spec/middleware/filters_spec.cr @@ -183,6 +183,30 @@ describe "Kemal::FilterHandler" do client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) client_response.body.should eq("true") end + + it "executes code before and after request" do + before_filter = FilterTest.new + before_filter.modified = "false" + + after_filter = FilterTest.new + after_filter.modified = "false" + + filter_middleware = Kemal::FilterHandler.new + filter_middleware._add_route_filter("ALL", "*", :before) { before_filter.modified = "true" } + filter_middleware._add_route_filter("ALL", "*", :after) { after_filter.modified = "true" } + + kemal = Kemal::RouteHandler::INSTANCE + kemal.add_route "GET", "/" { "#{before_filter.modified}-#{after_filter.modified}" } + + before_filter.modified.should eq("false") + after_filter.modified.should eq("false") + + request = HTTP::Request.new("GET", "/") + create_request_and_return_io_and_context(filter_middleware, request) + io_with_context = create_request_and_return_io_and_context(kemal, request)[0] + client_response = HTTP::Client::Response.from_io(io_with_context, decompress: false) + client_response.body.should eq("true-true") + end end class FilterTest diff --git a/src/kemal/filter_handler.cr b/src/kemal/filter_handler.cr index 6d28680..298ce26 100644 --- a/src/kemal/filter_handler.cr +++ b/src/kemal/filter_handler.cr @@ -71,7 +71,7 @@ module Kemal end private def radix_path(verb : String?, path : String, type : Symbol) - "#{type}/#{verb}/#{path}" + "/#{type}/#{verb}/#{path}" end # :nodoc: