use lazy & instead of try

unregister ip skipper in test
raise if called when a skipper is in play
This commit is contained in:
Sam Saffron 2018-02-06 10:38:15 +11:00
parent d7e90edeb5
commit df8e43abdd
2 changed files with 11 additions and 1 deletions

View File

@ -38,6 +38,11 @@ class Middleware::RequestTracker
end
end
# used for testing
def self.unregister_ip_skipper
@@ip_skipper = nil
end
# Register a custom `ip_skipper`, a function that will skip rate limiting
# for any IP that returns true.
#
@ -49,6 +54,7 @@ class Middleware::RequestTracker
# end
# ```
def self.register_ip_skipper(&blk)
raise "IP skipper is already registered!" if @@ip_skipper
@@ip_skipper = blk
end
@ -181,7 +187,7 @@ class Middleware::RequestTracker
return false if is_private_ip?(ip)
end
return false if @@ip_skipper.try(:call, ip)
return false if @@ip_skipper&.call(ip)
limiter10 = RateLimiter.new(
nil,

View File

@ -136,6 +136,10 @@ describe Middleware::RequestTracker do
global_setting :max_reqs_per_ip_mode, 'block'
end
after do
Middleware::RequestTracker.unregister_ip_skipper
end
it "won't block if the ip is skipped" do
env1 = env("REMOTE_ADDR" => "1.1.1.2")
status, _ = middleware.call(env1)