Skip to content

A platform for community discussion. Free, open, simple.

DEV: Ensure that `BlockRequestsMiddleware` cookie is always set (#25826)

Alan Guo Xiang Tanb64a58071d44
diff --git a/config/initializers/200-first_middlewares.rb b/config/initializers/200-first_middlewares.rb
index e06b7b9f3d1..ed3e2dccf2a 100644
--- a/config/initializers/200-first_middlewares.rb
+++ b/config/initializers/200-first_middlewares.rb
@@ -58,12 +58,18 @@ if Rails.env.test?
(
@@block_requests ||
(
- request.cookies[RSPEC_CURRENT_EXAMPLE_COOKIE_STRING].present? &&
+ self.class.current_example_location.present? &&
self.class.current_example_location !=
request.cookies[RSPEC_CURRENT_EXAMPLE_COOKIE_STRING]
)
)
- [503, { "Content-Type" => "text/plain" }, ["Blocked by BlockRequestsMiddleware"]]
+ [
+ 503,
+ { "Content-Type" => "text/plain" },
+ [
+ "Blocked by BlockRequestsMiddleware for requests initiated by #{request.cookies[RSPEC_CURRENT_EXAMPLE_COOKIE_STRING]} when running #{self.class.current_example_location}",
+ ],
+ ]
else
@app.call(env)
end
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb
index af8f3d93dac..6bd575fe359 100644
--- a/spec/rails_helper.rb
+++ b/spec/rails_helper.rb
@@ -593,8 +593,9 @@ RSpec.configure do |config|
ActiveRecord::Base.connection.schema_cache.add(table)
end
- ApplicationController.before_action do
- if BlockRequestsMiddleware.current_example_location
+ ApplicationController.before_action(prepend: true) do
+ if BlockRequestsMiddleware.current_example_location && !request.xhr? &&
+ request.format == "html"
cookies[
BlockRequestsMiddleware::RSPEC_CURRENT_EXAMPLE_COOKIE_STRING
] = BlockRequestsMiddleware.current_example_location