Skip to content

Instantly share code, notes, and snippets.

@lukeledet
Created December 14, 2015 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukeledet/af6d3c96dd06d52a2ba2 to your computer and use it in GitHub Desktop.
Save lukeledet/af6d3c96dd06d52a2ba2 to your computer and use it in GitHub Desktop.
rack-attack spec
require 'spec_helper'
class MockRequest
attr_reader :ip, :env
def initialize hash
@ip = hash[:ip] || '127.0.0.1'
@env = hash[:env] || {}
end
end
RSpec.describe 'Block China and Russia' do
let(:blacklist) { Rack::Attack.blacklists['block China and Russia'] }
it 'has a list of IP addresses to block' do
expect(BlockedIP.count).to be > 0
end
context 'with a visit from China' do
let(:request) { MockRequest.new(ip: '89.22.1.1') }
it 'should block the request' do
expect(blacklist[request]).to be_truthy
end
it 'should be fast' do
expect(Benchmark.realtime { blacklist[request] }).to be < 0.001
end
end
context 'with a visit from the US of A' do
let(:request) { MockRequest.new(ip: '70.183.119.185') }
it 'should not block the request' do
expect(blacklist[request]).to be_falsey
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment