From 721f6f9c97ec0131e543296db832a9ca7a633a24 Mon Sep 17 00:00:00 2001 From: KMY Date: Wed, 7 May 2025 09:15:44 +0900 Subject: [PATCH] Fix test --- spec/config/initializers/rack/attack_spec.rb | 40 ++++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/spec/config/initializers/rack/attack_spec.rb b/spec/config/initializers/rack/attack_spec.rb index b13efd1e06..6b3030158a 100644 --- a/spec/config/initializers/rack/attack_spec.rb +++ b/spec/config/initializers/rack/attack_spec.rb @@ -7,6 +7,26 @@ RSpec.describe Rack::Attack, type: :request do Rails.application end + def below_limit + limit - 1 + end + + def above_limit + limit * 2 + end + + def throttle_count + described_class.cache.read("#{counter_prefix}:#{throttle}:#{discriminator}") || 0 + end + + def counter_prefix + (Time.now.to_i / period.seconds).to_i + end + + def increment_counter + described_class.cache.count("#{throttle}:#{discriminator}", period) + end + shared_context 'with throttled endpoint base' do before do # Rack::Attack periods are not rolling, so avoid flaky tests by setting the time in a way @@ -18,26 +38,6 @@ RSpec.describe Rack::Attack, type: :request do travel_to Time.zone.at(counter_prefix * period.seconds) end - - def below_limit - limit - 1 - end - - def above_limit - limit * 2 - end - - def throttle_count - described_class.cache.read("#{counter_prefix}:#{throttle}:#{discriminator}") || 0 - end - - def counter_prefix - (Time.now.to_i / period.seconds).to_i - end - - def increment_counter - described_class.cache.count("#{throttle}:#{discriminator}", period) - end end shared_examples 'throttled endpoint' do