From 08e8c7934717355aadd52782442b98bf2959a0ca Mon Sep 17 00:00:00 2001 From: James Fischer <39064176+jamesfischer8@users.noreply.github.com> Date: Tue, 24 Sep 2019 16:08:58 -0700 Subject: [PATCH] Call run_afters even if throwing from run_befores --- lib/sidekiq/web/application.rb | 16 ++++++---------- test/test_web.rb | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/sidekiq/web/application.rb b/lib/sidekiq/web/application.rb index 8326e775..e9f033ba 100644 --- a/lib/sidekiq/web/application.rb +++ b/lib/sidekiq/web/application.rb @@ -283,17 +283,13 @@ module Sidekiq action = self.class.match(env) return [404, {"Content-Type" => "text/plain", "X-Cascade" => "pass"}, ["Not Found"]] unless action - resp = catch(:halt) { - app = @klass + app = @klass + resp = catch(:halt) do self.class.run_befores(app, action) - begin - resp = action.instance_exec env, &action.block - ensure - self.class.run_afters(app, action) - end - - resp - } + action.instance_exec env, &action.block + ensure + self.class.run_afters(app, action) + end resp = case resp when Array diff --git a/test/test_web.rb b/test/test_web.rb index d9316910..c3cca02a 100644 --- a/test/test_web.rb +++ b/test/test_web.rb @@ -769,4 +769,28 @@ describe Sidekiq::Web do end end end + + describe "redirecting in before" do + include Rack::Test::Methods + + before do + Sidekiq::WebApplication.before { Thread.current[:some_setting] = :before } + Sidekiq::WebApplication.before { redirect '/' } + Sidekiq::WebApplication.after { Thread.current[:some_setting] = :after } + end + + after do + Sidekiq::WebApplication.remove_instance_variable(:@befores) + Sidekiq::WebApplication.remove_instance_variable(:@afters) + end + + def app + Sidekiq::Web.new + end + + it "allows afters to run" do + get '/' + assert_equal :after, Thread.current[:some_setting] + end + end end