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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
require 'spec_helper'
describe VCR::Configuration do
describe '#cassette_library_dir=' do
let(:tmp_dir) { VCR::SPEC_ROOT + '/../tmp/cassette_library_dir/new_dir' }
after(:each) { FileUtils.rm_rf tmp_dir }
it 'creates the directory if it does not exist' do
expect { subject.cassette_library_dir = tmp_dir }.to change { File.exist?(tmp_dir) }.from(false).to(true)
end
it 'does not raise an error if given nil' do
expect { subject.cassette_library_dir = nil }.to_not raise_error
end
it 'resolves the given directory to an absolute path, so VCR continues to work even if the current directory changes' do
relative_dir = 'tmp/cassette_library_dir/new_dir'
subject.cassette_library_dir = relative_dir
absolute_dir = File.join(VCR::SPEC_ROOT.sub(/\/spec\z/, ''), relative_dir)
expect(subject.cassette_library_dir).to eq(absolute_dir)
end
end
describe '#default_cassette_options' do
it 'has a hash with some defaults' do
expect(subject.default_cassette_options).to eq({
:match_requests_on => VCR::RequestMatcherRegistry::DEFAULT_MATCHERS,
:allow_unused_http_interactions => true,
:record => :once,
:serialize_with => :yaml,
:persist_with => :file_system
})
end
it "returns #{VCR::RequestMatcherRegistry::DEFAULT_MATCHERS.inspect} for :match_requests_on when other defaults have been set" do
subject.default_cassette_options = { :record => :none }
expect(subject.default_cassette_options).to include(:match_requests_on => VCR::RequestMatcherRegistry::DEFAULT_MATCHERS)
end
it "returns :once for :record when other defaults have been set" do
subject.default_cassette_options = { :erb => :true }
expect(subject.default_cassette_options).to include(:record => :once)
end
it "allows defaults to be overriden" do
subject.default_cassette_options = { :record => :all }
expect(subject.default_cassette_options).to include(:record => :all)
end
it "allows other keys to be set" do
subject.default_cassette_options = { :re_record_interval => 10 }
expect(subject.default_cassette_options).to include(:re_record_interval => 10)
end
end
describe '#register_request_matcher' do
it 'registers the given request matcher' do
expect {
VCR.request_matchers[:custom]
}.to raise_error(VCR::UnregisteredMatcherError)
matcher_run = false
subject.register_request_matcher(:custom) { |r1, r2| matcher_run = true }
VCR.request_matchers[:custom].matches?(:r1, :r2)
expect(matcher_run).to be true
end
end
describe '#hook_into' do
it 'requires the named library hook' do
expect(subject).to receive(:require).with("vcr/library_hooks/webmock")
expect(subject).to receive(:require).with("vcr/library_hooks/excon")
subject.hook_into :webmock, :excon
end
it 'raises an error for unsupported stubbing libraries' do
expect {
subject.hook_into :unsupported_library
}.to raise_error(ArgumentError, /unsupported_library is not a supported VCR HTTP library hook/i)
end
it 'invokes the after_library_hooks_loaded hooks' do
called = false
subject.after_library_hooks_loaded { called = true }
subject.hook_into :webmock
expect(called).to be true
end
end
describe '#ignore_hosts' do
it 'delegates to the current request_ignorer instance' do
expect(VCR.request_ignorer).to receive(:ignore_hosts).with('example.com', 'example.net')
subject.ignore_hosts 'example.com', 'example.net'
end
end
describe '#unignore_hosts' do
it 'delegates to the current request_ignorer instance' do
expect(VCR.request_ignorer).to receive(:unignore_hosts).with('example.com', 'example.net')
subject.unignore_hosts 'example.com', 'example.net'
end
end
describe '#ignore_localhost=' do
it 'delegates to the current request_ignorer instance' do
expect(VCR.request_ignorer).to receive(:ignore_localhost=).with(true)
subject.ignore_localhost = true
end
end
describe '#ignore_request' do
let(:uri){ URI('http://foo.com') }
it 'registers the given block with the request ignorer' do
block_called = false
subject.ignore_request { |r| block_called = true }
VCR.request_ignorer.ignore?(double(:parsed_uri => uri))
expect(block_called).to be true
end
end
describe '#allow_http_connections_when_no_cassette=' do
[true, false].each do |val|
it "sets the allow_http_connections_when_no_cassette to #{val} when set to #{val}" do
subject.allow_http_connections_when_no_cassette = val
expect(subject.allow_http_connections_when_no_cassette?).to eq(val)
end
end
end
describe "request/configuration interactions", :with_monkey_patches => :webmock do
specify 'the request on the yielded interaction is not typed even though the request given to before_http_request is' do
before_record_req = before_request_req = nil
VCR.configure do |c|
c.before_http_request { |r| before_request_req = r }
c.before_record { |i| before_record_req = i.request }
end
VCR.use_cassette("example") do
::Net::HTTP.get_response(URI("http://localhost:#{VCR::SinatraApp.port}/foo"))
end
expect(before_record_req).not_to respond_to(:type)
expect(before_request_req).to respond_to(:type)
end unless (RUBY_VERSION =~ /^1\.8/ || RUBY_INTERPRETER == :jruby)
specify 'the filter_sensitive_data option works even when it modifies the URL in a way that makes it an invalid URI' do
VCR.configure do |c|
c.filter_sensitive_data('<HOST>') { 'localhost' }
end
2.times do
VCR.use_cassette("example") do
::Net::HTTP.get_response(URI("http://localhost:#{VCR::SinatraApp.port}/foo"))
end
end
end
end
[:before_record, :before_playback].each do |hook_type|
describe "##{hook_type}" do
it 'sets up a tag filter' do
called = false
VCR.configuration.send(hook_type, :my_tag) { called = true }
VCR.configuration.invoke_hook(hook_type, double, double(:tags => []))
expect(called).to be false
VCR.configuration.invoke_hook(hook_type, double, double(:tags => [:my_tag]))
expect(called).to be true
end
end
end
%w[ filter_sensitive_data define_cassette_placeholder ].each do |method|
describe "##{method}" do
let(:interaction) { double('interaction').as_null_object }
before(:each) { allow(interaction).to receive(:filter!) }
it 'adds a before_record hook that replaces the string returned by the block with the given string' do
subject.send(method, 'foo', &lambda { 'bar' })
expect(interaction).to receive(:filter!).with('bar', 'foo')
subject.invoke_hook(:before_record, interaction, double.as_null_object)
end
it 'adds a before_playback hook that replaces the given string with the string returned by the block' do
subject.send(method, 'foo', &lambda { 'bar' })
expect(interaction).to receive(:filter!).with('foo', 'bar')
subject.invoke_hook(:before_playback, interaction, double.as_null_object)
end
it 'tags the before_record hook when given a tag' do
expect(subject).to receive(:before_record).with(:my_tag)
subject.send(method, 'foo', :my_tag) { 'bar' }
end
it 'tags the before_playback hook when given a tag' do
expect(subject).to receive(:before_playback).with(:my_tag)
subject.send(method, 'foo', :my_tag) { 'bar' }
end
it 'yields the interaction to the block for the before_record hook' do
yielded_interaction = nil
subject.send(method, 'foo', &lambda { |i| yielded_interaction = i; 'bar' })
subject.invoke_hook(:before_record, interaction, double.as_null_object)
expect(yielded_interaction).to equal(interaction)
end
it 'yields the interaction to the block for the before_playback hook' do
yielded_interaction = nil
subject.send(method, 'foo', &lambda { |i| yielded_interaction = i; 'bar' })
subject.invoke_hook(:before_playback, interaction, double.as_null_object)
expect(yielded_interaction).to equal(interaction)
end
end
end
describe "#after_http_request" do
let(:raw_request) { VCR::Request.new }
let(:response) { VCR::Response.new }
def request(type)
VCR::Request::Typed.new(raw_request, type)
end
it 'handles symbol request predicate filters properly' do
yielded = false
subject.after_http_request(:stubbed_by_vcr?) { |req| yielded = true }
subject.invoke_hook(:after_http_request, request(:stubbed_by_vcr), response)
expect(yielded).to be true
yielded = false
subject.invoke_hook(:after_http_request, request(:ignored), response)
expect(yielded).to be false
end
end
describe "#cassette_serializers" do
let(:custom_serializer) { double }
it 'allows a custom serializer to be registered' do
expect { subject.cassette_serializers[:custom] }.to raise_error(ArgumentError)
subject.cassette_serializers[:custom] = custom_serializer
expect(subject.cassette_serializers[:custom]).to be(custom_serializer)
end
end
describe "#cassette_persisters" do
let(:custom_persister) { double }
it 'allows a custom persister to be registered' do
expect { subject.cassette_persisters[:custom] }.to raise_error(ArgumentError)
subject.cassette_persisters[:custom] = custom_persister
expect(subject.cassette_persisters[:custom]).to be(custom_persister)
end
end
describe "#uri_parser=" do
let(:custom_parser) { double }
it 'allows a custom uri parser to be set' do
subject.uri_parser = custom_parser
expect(subject.uri_parser).to eq(custom_parser)
end
it "uses Ruby's standard library `URI` as a default" do
expect(subject.uri_parser).to eq(URI)
end
end
describe "#preserve_exact_body_bytes_for?" do
def message_for(body)
double(:body => body)
end
context "default hook" do
it "returns false when there is no current cassette" do
expect(subject.preserve_exact_body_bytes_for?(message_for "string")).to be false
end
it "returns false when the current cassette has been created without the :preserve_exact_body_bytes option" do
VCR.insert_cassette('foo')
expect(subject.preserve_exact_body_bytes_for?(message_for "string")).to be false
end
it 'returns true when the current cassette has been created with the :preserve_exact_body_bytes option' do
VCR.insert_cassette('foo', :preserve_exact_body_bytes => true)
expect(subject.preserve_exact_body_bytes_for?(message_for "string")).to be true
end
end
it "returns true when the configured block returns true" do
subject.preserve_exact_body_bytes { |msg| msg.body == "a" }
expect(subject.preserve_exact_body_bytes_for?(message_for "a")).to be true
expect(subject.preserve_exact_body_bytes_for?(message_for "b")).to be false
end
it "returns true when any of the registered blocks returns true" do
called_hooks = []
subject.preserve_exact_body_bytes { called_hooks << :hook_1; false }
subject.preserve_exact_body_bytes { called_hooks << :hook_2; true }
expect(subject.preserve_exact_body_bytes_for?(message_for "a")).to be true
expect(called_hooks).to eq([:hook_1, :hook_2])
end
it "invokes the configured hook with the http message and the current cassette" do
VCR.use_cassette('example') do |cassette|
expect(cassette).to be_a(VCR::Cassette)
message = double(:message)
yielded_objects = nil
subject.preserve_exact_body_bytes { |a, b| yielded_objects = [a, b] }
subject.preserve_exact_body_bytes_for?(message)
expect(yielded_objects).to eq([message, cassette])
end
end
end
describe "#configure_rspec_metadata!" do
it "only configures the underlying metadata once, no matter how many times it is called" do
expect(VCR::RSpec::Metadata).to receive(:configure!).once
VCR.configure do |c|
c.configure_rspec_metadata!
end
VCR.configure do |c|
c.configure_rspec_metadata!
end
end
end
end
|