diff --git a/config/initializers/200-first_middlewares.rb b/config/initializers/200-first_middlewares.rb index ea011a6f8af..79bbb199040 100644 --- a/config/initializers/200-first_middlewares.rb +++ b/config/initializers/200-first_middlewares.rb @@ -28,8 +28,35 @@ if Rails.env.test? super(env) end end + Rails.configuration.middleware.unshift TestMultisiteMiddleware, RailsMultisite::DiscoursePatches.config + + class BlockRequestsMiddleware + @@block_requests = false + + def self.block_requests! + @@block_requests = true + end + + def self.allow_requests! + @@block_requests = false + end + + def initialize(app) + @app = app + end + + def call(env) + if @@block_requests + [503, { "Content-Type" => "text/plain" }, ["Blocked by BlockRequestsMiddleware"]] + else + @app.call(env) + end + end + end + + Rails.configuration.middleware.unshift BlockRequestsMiddleware elsif Rails.configuration.multisite assets_hostnames = GlobalSetting.cdn_hostnames diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 67ec956addf..dbf92bd38a4 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -608,6 +608,7 @@ RSpec.configure do |config| driven_by driver.join("_").to_sym setup_system_test + BlockRequestsMiddleware.allow_requests! end config.after(:each, type: :system) do |example| @@ -663,6 +664,10 @@ RSpec.configure do |config| end page.execute_script("if (typeof MessageBus !== 'undefined') { MessageBus.stop(); }") + + # Block all incoming requests before resetting Capybara session which will wait for all requests to finish + BlockRequestsMiddleware.block_requests! + Capybara.reset_session! MessageBus.backend_instance.reset! # Clears all existing backlog from memory backend Discourse.redis.flushdb