File tree Expand file tree Collapse file tree 3 files changed +45
-22
lines changed Expand file tree Collapse file tree 3 files changed +45
-22
lines changed Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ require_relative "../spec_helper"
4+
5+ describe "Middleware" do
6+ it "is used in Rails by default" do
7+ skip unless defined? ( Rails )
8+
9+ app = Class . new ( Rails ::Application ) do
10+ config . eager_load = false
11+ config . logger = Logger . new ( nil ) # avoid creating the log/ directory automatically
12+ config . cache_store = :null_store # avoid creating tmp/ directory for cache
13+ end
14+
15+ app . initialize!
16+ assert app . middleware . include? ( Rack ::Attack )
17+ end
18+
19+ def app
20+ Rack ::Builder . new do
21+ use Rack ::Attack do
22+ blocklist_ip ( "1.2.3.4" )
23+ end
24+
25+ run lambda { |_env | [ 200 , { } , [ 'Hello World' ] ] }
26+ end . to_app
27+ end
28+
29+ it "can be configured via a block" do
30+ get "/" , { } , "REMOTE_ADDR" => "1.2.3.4"
31+ assert_equal 403 , last_response . status
32+
33+ get "/" , { } , "REMOTE_ADDR" => "4.3.2.1"
34+ assert_equal 200 , last_response . status
35+ end
36+ end
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments