Skip to content

Commit f23dd26

Browse files
committed
Add ability to use multiple instances of middleware
1 parent ffc2102 commit f23dd26

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/rack/attack.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,16 @@ def reset!
9696

9797
attr_reader :configuration
9898

99-
def initialize(app)
99+
def initialize(app, &block)
100100
@app = app
101-
@configuration = self.class.configuration
101+
@configuration =
102+
if block_given?
103+
configuration = Configuration.new
104+
configuration.instance_exec(&block)
105+
configuration
106+
else
107+
self.class.configuration
108+
end
102109
end
103110

104111
def call(env)

spec/acceptance/rails_middleware_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,17 @@
1616
@app.initialize!
1717
assert @app.middleware.include?(Rack::Attack)
1818
end
19+
20+
it "can be configured via a block" do
21+
@app.middleware.delete(Rack::Attack)
22+
@app.middleware.use(Rack::Attack) do
23+
blocklist_ip("1.2.3.4")
24+
end
25+
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
26+
assert_equal 403, last_response.status
27+
28+
get "/", {}, "REMOTE_ADDR" => "4.3.2.1"
29+
assert_equal 200, last_response.status
30+
end
1931
end
2032
end

0 commit comments

Comments
 (0)