Skip to content

Commit

Permalink
Support Rack 3. (#586)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jul 25, 2023
1 parent e039ecd commit b708de3
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
- 2.6.10
- 2.5.8
gemfile:
- rack_3
- rack_2
- rack_1
- rails_7_0
Expand Down
4 changes: 4 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# frozen_string_literal: true

appraise "rack_3" do
gem "rack", "~> 3.0"
end

appraise "rack_2" do
gem "rack", "~> 2.0"
end
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rack_2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
source "https://rubygems.org"

gem "rack", "~> 2.0"
gem "railties"

gemspec path: "../"
7 changes: 7 additions & 0 deletions gemfiles/rack_3.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "rack", "~> 3.0"

gemspec path: "../"
10 changes: 8 additions & 2 deletions lib/rack/attack/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ class Cache
attr_accessor :prefix
attr_reader :last_epoch_time

def initialize
self.store = ::Rails.cache if defined?(::Rails.cache)
def self.default_store
if Object.const_defined?(:Rails) && Rails.respond_to?(:cache)
::Rails.cache
end
end

def initialize(store: self.class.default_store)
self.store = store
@prefix = 'rack::attack'
end

Expand Down
6 changes: 6 additions & 0 deletions lib/rack/attack/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# frozen_string_literal: true

begin
require 'rails/railtie'
rescue LoadError
return
end

module Rack
class Attack
class Railtie < ::Rails::Railtie
Expand Down
4 changes: 2 additions & 2 deletions rack-attack.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Gem::Specification.new do |s|

s.required_ruby_version = '>= 2.4'

s.add_runtime_dependency 'rack', ">= 1.0", "< 3"
s.add_runtime_dependency 'rack', ">= 1.0", "< 4"

s.add_development_dependency 'appraisal', '~> 2.2'
s.add_development_dependency "bundler", ">= 1.17", "< 3.0"
Expand All @@ -46,5 +46,5 @@ Gem::Specification.new do |s|
s.add_development_dependency 'byebug', '~> 11.0'
end

s.add_development_dependency 'railties', '>= 4.2', '< 7.1'
s.add_development_dependency "activesupport"
end
2 changes: 1 addition & 1 deletion spec/acceptance/rails_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require_relative "../spec_helper"

if defined?(Rails)
if defined?(Rails::Application)
describe "Middleware for Rails" do
before do
@app = Class.new(Rails::Application) do
Expand Down
1 change: 1 addition & 0 deletions spec/rack_attack_instrumentation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative "spec_helper"
require 'active_support'

# ActiveSupport::Subscribers added in ~> 4.0.2.0
if ActiveSupport::VERSION::MAJOR > 3
Expand Down
4 changes: 4 additions & 0 deletions spec/rack_attack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
end

it 'blocks requests with trailing slash' do
if Rack::Attack::PathNormalizer == Rack::Attack::FallbackPathNormalizer
skip "Normalization is only present on Rails"
end

get '/foo/'
_(last_response.status).must_equal 403
end
Expand Down
7 changes: 4 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
require "minitest/autorun"
require "minitest/pride"
require "rack/test"
require "rails"

require "active_support"
require "rack/attack"

if RUBY_ENGINE == "ruby"
Expand All @@ -29,7 +28,9 @@ class MiniTest::Spec
include Rack::Test::Methods

before do
Rails.cache = nil
if Object.const_defined?(:Rails) && Rails.respond_to?(:cache)
Rails.cache.clear
end
end

after do
Expand Down

0 comments on commit b708de3

Please sign in to comment.