Skip to content

Feature request: testing helpers #645

@fatkodima

Description

@fatkodima

Currently, to test rack-attack's behavior we need to use something like:

context "number of requests is lower than the limit" do
  it "does not change the request status" do
    1000.times do
      get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
      expect(last_response.status).to_not eq(429)
    end
  end
end

to perform a lot of unneeded requests (depending on the throttling configuration) or use some internals from the gem like:

def emulate_previous_requests(key, count:, period:)
  # Go to the rack-attack's internals to make tests faster.
  key, expires_in = Rack::Attack.cache.send(:key_and_expiry, key, period)
  Rack::Attack.cache.store.write(key, count, expires_in: expires_in)
end

# and use the `emulate_previous_requests` helper in tests

This can be also solved by using some constants in the rack-attack configuration block and then assign some smaller numbers to these constants in the testing environment, but this is ugly if we have a lot of different constants.

It would be nice to have some test helpers to simplify write the tests.
I can help with this, but needs to figure out the API.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions