From da1f54b6fcbc0f2a4f16b85c5f6091568d5667ff Mon Sep 17 00:00:00 2001 From: Gonzalo Rodriguez Date: Thu, 22 Mar 2018 11:44:41 -0300 Subject: [PATCH 1/2] Acceptance test ability to access match data in #blocklisted_response --- .../customizing_blocked_response_spec.rb | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/spec/acceptance/customizing_blocked_response_spec.rb b/spec/acceptance/customizing_blocked_response_spec.rb index 7f3f307..190f5c0 100644 --- a/spec/acceptance/customizing_blocked_response_spec.rb +++ b/spec/acceptance/customizing_blocked_response_spec.rb @@ -1,11 +1,13 @@ require_relative "../spec_helper" describe "Customizing block responses" do - it "can be customized" do + before do Rack::Attack.blocklist("block 1.2.3.4") do |request| request.ip == "1.2.3.4" end + end + it "can be customized" do get "/", {}, "REMOTE_ADDR" => "1.2.3.4" assert_equal 403, last_response.status @@ -19,4 +21,21 @@ describe "Customizing block responses" do assert_equal 503, last_response.status assert_equal "Blocked", last_response.body end + + it "exposes match data" do + matched = nil + match_type = nil + + Rack::Attack.blocklisted_response = lambda do |env| + matched = env['rack.attack.matched'] + match_type = env['rack.attack.match_type'] + + [503, {}, ["Blocked"]] + end + + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" + + assert_equal "block 1.2.3.4", matched + assert_equal :blocklist, match_type + end end From 3f5574c4e41439ff2142ecd2b287d66c7c450404 Mon Sep 17 00:00:00 2001 From: Gonzalo Rodriguez Date: Thu, 22 Mar 2018 11:48:56 -0300 Subject: [PATCH 2/2] Acceptance test ability to access match data in #throttled_response --- .../customizing_throttled_response_spec.rb | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/spec/acceptance/customizing_throttled_response_spec.rb b/spec/acceptance/customizing_throttled_response_spec.rb index 7f66372..5f657aa 100644 --- a/spec/acceptance/customizing_throttled_response_spec.rb +++ b/spec/acceptance/customizing_throttled_response_spec.rb @@ -1,13 +1,15 @@ require_relative "../spec_helper" describe "Customizing throttled response" do - it "can be customized" do + before do Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new Rack::Attack.throttle("by ip", limit: 1, period: 60) do |request| request.ip end + end + it "can be customized" do get "/", {}, "REMOTE_ADDR" => "1.2.3.4" assert_equal 200, last_response.status @@ -25,4 +27,33 @@ describe "Customizing throttled response" do assert_equal 503, last_response.status assert_equal "Throttled", last_response.body end + + it "exposes match data" do + matched = nil + match_type = nil + match_data = nil + match_discriminator = nil + + Rack::Attack.throttled_response = lambda do |env| + matched = env['rack.attack.matched'] + match_type = env['rack.attack.match_type'] + match_data = env['rack.attack.match_data'] + match_discriminator = env['rack.attack.match_discriminator'] + + [429, {}, ["Throttled"]] + end + + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" + + assert_equal "by ip", matched + assert_equal :throttle, match_type + assert_equal 60, match_data[:period] + assert_equal 1, match_data[:limit] + assert_equal 2, match_data[:count] + assert_equal "1.2.3.4", match_discriminator + + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" + assert_equal 3, match_data[:count] + end end