Skip to content

Commit

Permalink
224400 Enable throttling on GET route (#7703)
Browse files Browse the repository at this point in the history
  • Loading branch information
dillo authored Aug 20, 2021
1 parent 29b022c commit ed71663
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/initializers/rack_attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def remote_ip
req.ip if req.path.starts_with?('/check_in/v0') && (req.post? || req.get?)
end

throttle('medical_copays/ip', limit: 20, period: 1.minute) do |req|
req.ip if req.path.starts_with?('/v0/medical_copays') && req.get?
end

# Source: https://github.com/kickstarter/rack-attack#x-ratelimit-headers-for-well-behaved-clients
Rack::Attack.throttled_response = lambda do |env|
rate_limit = env['rack.attack.match_data']
Expand Down
22 changes: 22 additions & 0 deletions spec/middleware/rack/attack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ def app
end
end

describe 'medical_copays/ip' do
before do
allow_any_instance_of(MedicalCopays::VBS::Service).to receive(:get_copays).and_return([])
end

context 'when more than 20 requests' do
before do
20.times do
get '/v0/medical_copays', headers: headers

expect(last_response.status).to eq(401)
end
end

it 'throttles with status 429' do
get '/v0/medical_copays', headers: headers

expect(last_response.status).to eq(429)
end
end
end

describe 'facility_locator/ip' do
let(:endpoint) { '/facilities_api/v1/ccp/provider' }
let(:headers) { { 'X-Real-Ip' => '1.2.3.4' } }
Expand Down

0 comments on commit ed71663

Please sign in to comment.