|
2 | 2 |
|
3 | 3 | require_relative "../spec_helper" |
4 | 4 |
|
5 | | -describe "#blocklist" do |
6 | | - let(:notifications) { [] } |
7 | | - |
8 | | - before do |
9 | | - Rack::Attack.blocklist do |request| |
10 | | - request.ip == "1.2.3.4" |
| 5 | +if defined?(::ActiveSupport::Notifications) |
| 6 | + describe "#blocklist" do |
| 7 | + let(:notifications) { [] } |
| 8 | + |
| 9 | + before do |
| 10 | + Rack::Attack.blocklist do |request| |
| 11 | + request.ip == "1.2.3.4" |
| 12 | + end |
11 | 13 | end |
12 | | - end |
13 | 14 |
|
14 | | - it "forbids request if blocklist condition is true" do |
15 | | - get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
16 | | - |
17 | | - assert_equal 403, last_response.status |
18 | | - end |
| 15 | + it "forbids request if blocklist condition is true" do |
| 16 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
19 | 17 |
|
20 | | - it "succeeds if blocklist condition is false" do |
21 | | - get "/", {}, "REMOTE_ADDR" => "5.6.7.8" |
| 18 | + assert_equal 403, last_response.status |
| 19 | + end |
22 | 20 |
|
23 | | - assert_equal 200, last_response.status |
24 | | - end |
| 21 | + it "succeeds if blocklist condition is false" do |
| 22 | + get "/", {}, "REMOTE_ADDR" => "5.6.7.8" |
25 | 23 |
|
26 | | - it "notifies when the request is blocked" do |
27 | | - ActiveSupport::Notifications.subscribe("rack.attack") do |_name, _start, _finish, _id, payload| |
28 | | - notifications.push(payload) |
| 24 | + assert_equal 200, last_response.status |
29 | 25 | end |
30 | 26 |
|
31 | | - get "/", {}, "REMOTE_ADDR" => "5.6.7.8" |
| 27 | + it "notifies when the request is blocked" do |
| 28 | + ActiveSupport::Notifications.subscribe("rack.attack") do |_name, _start, _finish, _id, payload| |
| 29 | + notifications.push(payload) |
| 30 | + end |
32 | 31 |
|
33 | | - assert notifications.empty? |
| 32 | + get "/", {}, "REMOTE_ADDR" => "5.6.7.8" |
34 | 33 |
|
35 | | - get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
36 | | - |
37 | | - assert_equal 1, notifications.size |
38 | | - notification = notifications.pop |
39 | | - assert_nil notification[:request].env["rack.attack.matched"] |
40 | | - assert_equal :blocklist, notification[:request].env["rack.attack.match_type"] |
41 | | - end |
42 | | -end |
| 34 | + assert notifications.empty? |
43 | 35 |
|
44 | | -describe "#blocklist with name" do |
45 | | - let(:notifications) { [] } |
| 36 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
46 | 37 |
|
47 | | - before do |
48 | | - Rack::Attack.blocklist("block 1.2.3.4") do |request| |
49 | | - request.ip == "1.2.3.4" |
| 38 | + assert_equal 1, notifications.size |
| 39 | + notification = notifications.pop |
| 40 | + assert_nil notification[:request].env["rack.attack.matched"] |
| 41 | + assert_equal :blocklist, notification[:request].env["rack.attack.match_type"] |
50 | 42 | end |
51 | 43 | end |
52 | 44 |
|
53 | | - it "forbids request if blocklist condition is true" do |
54 | | - get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 45 | + describe "#blocklist with name" do |
| 46 | + let(:notifications) { [] } |
55 | 47 |
|
56 | | - assert_equal 403, last_response.status |
57 | | - end |
| 48 | + before do |
| 49 | + Rack::Attack.blocklist("block 1.2.3.4") do |request| |
| 50 | + request.ip == "1.2.3.4" |
| 51 | + end |
| 52 | + end |
58 | 53 |
|
59 | | - it "succeeds if blocklist condition is false" do |
60 | | - get "/", {}, "REMOTE_ADDR" => "5.6.7.8" |
| 54 | + it "forbids request if blocklist condition is true" do |
| 55 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
61 | 56 |
|
62 | | - assert_equal 200, last_response.status |
63 | | - end |
| 57 | + assert_equal 403, last_response.status |
| 58 | + end |
| 59 | + |
| 60 | + it "succeeds if blocklist condition is false" do |
| 61 | + get "/", {}, "REMOTE_ADDR" => "5.6.7.8" |
64 | 62 |
|
65 | | - it "notifies when the request is blocked" do |
66 | | - ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload| |
67 | | - notifications.push(payload) |
| 63 | + assert_equal 200, last_response.status |
68 | 64 | end |
69 | 65 |
|
70 | | - get "/", {}, "REMOTE_ADDR" => "5.6.7.8" |
| 66 | + it "notifies when the request is blocked" do |
| 67 | + ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload| |
| 68 | + notifications.push(payload) |
| 69 | + end |
71 | 70 |
|
72 | | - assert notifications.empty? |
| 71 | + get "/", {}, "REMOTE_ADDR" => "5.6.7.8" |
73 | 72 |
|
74 | | - get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 73 | + assert notifications.empty? |
75 | 74 |
|
76 | | - assert_equal 1, notifications.size |
77 | | - notification = notifications.pop |
78 | | - assert_equal "block 1.2.3.4", notification[:request].env["rack.attack.matched"] |
79 | | - assert_equal :blocklist, notification[:request].env["rack.attack.match_type"] |
| 75 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 76 | + |
| 77 | + assert_equal 1, notifications.size |
| 78 | + notification = notifications.pop |
| 79 | + assert_equal "block 1.2.3.4", notification[:request].env["rack.attack.matched"] |
| 80 | + assert_equal :blocklist, notification[:request].env["rack.attack.match_type"] |
| 81 | + end |
80 | 82 | end |
81 | 83 | end |
0 commit comments