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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
require 'vcr/structs'
require 'vcr/request_ignorer'
module VCR
describe RequestIgnorer do
def request(uri)
VCR::Request.new.tap { |r| r.uri = uri }
end
shared_examples_for "#ignore?" do |url, expected_value|
it "returns #{expected_value} if given a request with a url like #{url}" do
expect(subject.ignore?(request(url))).to eq(expected_value)
end
end
context 'when example.com and example.net are ignored' do
before(:each) { subject.ignore_hosts 'example.com', 'example.net' }
it_behaves_like "#ignore?", "http://www.example.com/foo", false
it_behaves_like "#ignore?", "http://example.com/foo", true
it_behaves_like "#ignore?", "http://example.net:890/foo", true
it_behaves_like "#ignore?", "http://some-other-domain.com/", false
end
context 'when example.com is unignored' do
before(:each) do
subject.instance_variable_set(:@ignored_hosts, Set['example.com'])
subject.unignore_hosts 'example.com'
end
it_behaves_like "#ignore?", "http://example.com/foo", false
end
context 'when two of three example hosts are unignored' do
before(:each) do
subject.instance_variable_set(:@ignored_hosts, Set['example.com', 'example.net', 'example.org'])
subject.unignore_hosts 'example.com', 'example.net'
end
it_behaves_like "#ignore?", "http://example.com/foo", false
it_behaves_like "#ignore?", "http://example.net:890/foo", false
it_behaves_like "#ignore?", "https://example.org:890/foo", true
end
context 'when not ignored host is unignored' do
it 'no errors should be raised' do
expect { subject.unignore_hosts 'example.com' }.not_to raise_error
end
end
context 'when ignore_localhost is set to true' do
before(:each) { subject.ignore_localhost = true }
it_behaves_like "#ignore?", "http://some-host.com/foo", false
RequestIgnorer::LOCALHOST_ALIASES.each do |host|
it_behaves_like "#ignore?", "http://#{host}/foo", true
end
end
context 'when ignore_localhost is not set' do
it_behaves_like "#ignore?", "http://some-host.com/foo", false
RequestIgnorer::LOCALHOST_ALIASES.each do |host|
it_behaves_like "#ignore?", "http://#{host}/foo", false
end
end
context 'when ignore_localhost is set to false after being set to true' do
before(:each) do
subject.ignore_localhost = true
subject.ignore_localhost = false
end
it_behaves_like "#ignore?", "http://some-host.com/foo", false
RequestIgnorer::LOCALHOST_ALIASES.each do |host|
it_behaves_like "#ignore?", "http://#{host}/foo", false
end
end
context 'when a custom ignore_request hook has been set' do
before(:each) do
subject.ignore_request do |request|
URI(request.uri).port == 5
end
end
it 'ignores requests for which the block returns true' do
expect(subject.ignore?(request('http://foo.com:5/bar'))).to be true
end
it 'does not ignore requests for which the block returns false' do
expect(subject.ignore?(request('http://foo.com:6/bar'))).to be false
end
end
end
end
|