File: rack_attack_track_spec.rb

package info (click to toggle)
ruby-rack-attack 6.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 380 kB
  • sloc: ruby: 2,626; makefile: 4
file content (66 lines) | stat: -rw-r--r-- 1,484 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true

require_relative 'spec_helper'

describe 'Rack::Attack.track' do
  class Counter
    def self.incr
      @counter += 1
    end

    def self.reset
      @counter = 0
    end

    def self.check
      @counter
    end
  end

  before do
    Rack::Attack.track("everything") { |_req| true }
  end

  it_allows_ok_requests

  it "should tag the env" do
    get '/'

    _(last_request.env['rack.attack.matched']).must_equal 'everything'
    _(last_request.env['rack.attack.match_type']).must_equal :track
  end

  describe "with a notification subscriber and two tracks" do
    before do
      Counter.reset
      # A second track
      Rack::Attack.track("homepage") { |req| req.path == "/" }

      ActiveSupport::Notifications.subscribe("track.rack_attack") do |*_args|
        Counter.incr
      end

      get "/"
    end

    it "should notify twice" do
      _(Counter.check).must_equal 2
    end
  end

  describe "without limit and period options" do
    it "should assign the track filter to a Check instance" do
      track = Rack::Attack.track("homepage") { |req| req.path == "/" }

      _(track.filter.class).must_equal Rack::Attack::Check
    end
  end

  describe "with limit and period options" do
    it "should assign the track filter to a Throttle instance" do
      track = Rack::Attack.track("homepage", limit: 10, period: 10) { |req| req.path == "/" }

      _(track.filter.class).must_equal Rack::Attack::Throttle
    end
  end
end