|
3 | 3 | require_relative "../spec_helper" |
4 | 4 |
|
5 | 5 | describe "#blocklist" do |
| 6 | + let(:notifications) { [] } |
| 7 | + |
6 | 8 | before do |
7 | 9 | Rack::Attack.blocklist do |request| |
8 | 10 | request.ip == "1.2.3.4" |
|
22 | 24 | end |
23 | 25 |
|
24 | 26 | it "notifies when the request is blocked" do |
25 | | - notification_matched = nil |
26 | | - notification_type = nil |
27 | | - |
28 | 27 | ActiveSupport::Notifications.subscribe("rack.attack") do |_name, _start, _finish, _id, payload| |
29 | | - notification_matched = payload[:request].env["rack.attack.matched"] |
30 | | - notification_type = payload[:request].env["rack.attack.match_type"] |
| 28 | + notifications.push(payload) |
31 | 29 | end |
32 | 30 |
|
33 | 31 | get "/", {}, "REMOTE_ADDR" => "5.6.7.8" |
34 | 32 |
|
35 | | - assert_nil notification_matched |
36 | | - assert_nil notification_type |
| 33 | + assert notifications.empty? |
37 | 34 |
|
38 | 35 | get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
39 | 36 |
|
40 | | - assert_nil notification_matched |
41 | | - assert_equal :blocklist, notification_type |
| 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"] |
42 | 41 | end |
43 | 42 | end |
44 | 43 |
|
45 | 44 | describe "#blocklist with name" do |
| 45 | + let(:notifications) { [] } |
| 46 | + |
46 | 47 | before do |
47 | 48 | Rack::Attack.blocklist("block 1.2.3.4") do |request| |
48 | 49 | request.ip == "1.2.3.4" |
|
62 | 63 | end |
63 | 64 |
|
64 | 65 | it "notifies when the request is blocked" do |
65 | | - notification_matched = nil |
66 | | - notification_type = nil |
67 | | - |
68 | 66 | ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload| |
69 | | - notification_matched = payload[:request].env["rack.attack.matched"] |
70 | | - notification_type = payload[:request].env["rack.attack.match_type"] |
| 67 | + notifications.push(payload) |
71 | 68 | end |
72 | 69 |
|
73 | 70 | get "/", {}, "REMOTE_ADDR" => "5.6.7.8" |
74 | 71 |
|
75 | | - assert_nil notification_matched |
76 | | - assert_nil notification_type |
| 72 | + assert notifications.empty? |
77 | 73 |
|
78 | 74 | get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
79 | 75 |
|
80 | | - assert_equal "block 1.2.3.4", notification_matched |
81 | | - assert_equal :blocklist, notification_type |
| 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"] |
82 | 80 | end |
83 | 81 | end |
0 commit comments