Skip to content

Commit

Permalink
test fixes for Facility Locator throttle
Browse files Browse the repository at this point in the history
Co-authored-by: Lindsey Hattamer <lindsey.hattamer@oddball.io>
Co-authored-by: Riley Anderson <riley.anderson@oddball.io>
  • Loading branch information
3 people committed Aug 18, 2021
1 parent 759ac3b commit 576b34d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion config/initializers/rack_attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Rack::Attack
# we're behind a load balancer and/or proxy, which is what request.ip returns
class Request < ::Rack::Request
def remote_ip
@remote_ip ||= (env['action_dispatch.remote_ip'] || env['X-Real-Ip'] || ip).to_s
@remote_ip ||= (env['X-Real-Ip'] || ip).to_s
end
end

Expand Down
19 changes: 11 additions & 8 deletions spec/middleware/rack/attack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,33 @@ def app
end

describe 'facility_locator/ip' do
subject { last_response.status }

let(:endpoint) { '/facilities_api/v1/ccp/provider' }
let(:headers) { { 'X-Real-Ip' => '1.2.3.4' } }
let(:limit) { 3 }

before do
limit.times do
get endpoint, headers: headers
expect(subject).not_to eq(429)
get endpoint, nil, headers
expect(last_response.status).not_to eq(429)
end

get endpoint, headers: other_headers
get endpoint, nil, other_headers
end

context 'response status for repeated requests from the same IP' do
let(:other_headers) { headers }

it { is_expected.to eq 429 }
it 'limits requests' do
expect(last_response.status).to eq(429)
end
end

context 'response status for request from different IP' do
let(:other_headers) { { 'REMOTE_ADDR' => '4.3.2.1' } }
let(:other_headers) { { 'X-Real-Ip' => '4.3.2.1' } }

it { is_expected.to eq 200 }
it 'limits requests' do
expect(last_response.status).not_to eq(429)
end
end
end

Expand Down

0 comments on commit 576b34d

Please sign in to comment.