Best Vcr_ruby code snippet using VCR.each
cassette_spec.rb
Source:cassette_spec.rb
...23 it 'keeps any path separators' do24 cassette = VCR::Cassette.new("dir/file_name")25 cassette.file.should =~ /#{Regexp.escape('dir/file_name.yml')}$/26 end27 VCR::Cassette::VALID_RECORD_MODES.each do |mode|28 it "returns nil if the cassette_library_dir is not set (when the record mode is :#{mode})" do29 VCR.configuration.cassette_library_dir = nil30 cassette = VCR::Cassette.new('the_file', :record => mode)31 cassette.file.should be_nil32 end33 end34 end35 describe '#tags' do36 it 'returns a blank array if no tag has been set' do37 VCR::Cassette.new("name").tags.should eq([])38 end39 it 'converts a single :tag to an array' do40 VCR::Cassette.new("name", :tag => :foo).tags.should eq([:foo])41 end42 it 'accepts an array as the :tags option' do43 VCR::Cassette.new("name", :tags => [:foo]).tags.should eq([:foo])44 end45 end46 describe '#record_http_interaction' do47 let(:the_interaction) { stub(:request => stub(:method => :get).as_null_object).as_null_object }48 it 'adds the interaction to #new_recorded_interactions' do49 cassette = VCR::Cassette.new(:test_cassette)50 cassette.new_recorded_interactions.should eq([])51 cassette.record_http_interaction(the_interaction)52 cassette.new_recorded_interactions.should eq([the_interaction])53 end54 end55 describe "#serializable_hash" do56 subject { VCR::Cassette.new("foo") }57 let(:interaction_1) { http_interaction { |i| i.request.body = 'req body 1'; i.response.body = 'res body 1' } }58 let(:interaction_2) { http_interaction { |i| i.request.body = 'req body 2'; i.response.body = 'res body 2' } }59 let(:interactions) { [interaction_1, interaction_2] }60 before(:each) do61 interactions.each do |i|62 subject.record_http_interaction(i)63 end64 end65 let(:metadata) { subject.serializable_hash.reject { |k,v| k == "http_interactions" } }66 it 'includes the hash form of all recorded interactions' do67 interaction_1.stub(:to_hash => { "i" => 1, 'body' => '' })68 interaction_2.stub(:to_hash => { "i" => 2, 'body' => '' })69 subject.serializable_hash.should include('http_interactions' => [{ "i" => 1, 'body' => '' }, { "i" => 2, 'body' => '' }])70 end71 it 'includes additional metadata about the cassette' do72 metadata.should eq("recorded_with" => "VCR #{VCR.version}")73 end74 end75 describe "#recording?" do76 [:all, :new_episodes].each do |mode|77 it "returns true when the record mode is :#{mode}" do78 cassette = VCR::Cassette.new("foo", :record => mode)79 cassette.should be_recording80 end81 end82 it "returns false when the record mode is :none" do83 cassette = VCR::Cassette.new("foo", :record => :none)84 cassette.should_not be_recording85 end86 context 'when the record mode is :once' do87 before(:each) do88 VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"89 end90 it 'returns false when there is an existing cassette file with content' do91 cassette = VCR::Cassette.new("example", :record => :once)92 File.should exist(cassette.file)93 File.size?(cassette.file).should be_true94 cassette.should_not be_recording95 end96 it 'returns true when there is an empty existing cassette file' do97 cassette = VCR::Cassette.new("empty", :record => :once)98 File.should exist(cassette.file)99 File.size?(cassette.file).should be_false100 cassette.should be_recording101 end102 it 'returns true when there is no existing cassette file' do103 cassette = VCR::Cassette.new("non_existant_file", :record => :once)104 File.should_not exist(cassette.file)105 cassette.should be_recording106 end107 end108 end109 describe '#match_requests_on' do110 before(:each) { VCR.configuration.default_cassette_options.merge!(:match_requests_on => [:uri, :method]) }111 it "returns the provided options" do112 c = VCR::Cassette.new('example', :match_requests_on => [:uri])113 c.match_requests_on.should eq([:uri])114 end115 it "returns a the default #match_requests_on when it has not been specified for the cassette" do116 c = VCR::Cassette.new('example')117 c.match_requests_on.should eq([:uri, :method])118 end119 end120 describe "reading the file from disk" do121 before(:each) do122 File.stub(:size? => true)123 end124 let(:empty_cassette_yaml) { YAML.dump("http_interactions" => []) }125 it 'reads the appropriate file from disk using a VCR::Cassette::Reader' do126 VCR::Cassette::Reader.should_receive(:new).with(127 "#{VCR.configuration.cassette_library_dir}/foo.yml", anything128 ).and_return(mock('reader', :read => empty_cassette_yaml))129 VCR::Cassette.new('foo', :record => :new_episodes).http_interactions130 end131 [true, false, nil, { }].each do |erb|132 it "passes #{erb.inspect} to the VCR::Cassette::Reader when given as the :erb option" do133 # test that it overrides the default134 VCR.configuration.default_cassette_options = { :erb => true }135 VCR::Cassette::Reader.should_receive(:new).with(136 anything, erb137 ).and_return(mock('reader', :read => empty_cassette_yaml))138 VCR::Cassette.new('foo', :record => :new_episodes, :erb => erb).http_interactions139 end140 it "passes #{erb.inspect} to the VCR::Cassette::Reader when it is the default :erb option and none is given" do141 VCR.configuration.default_cassette_options = { :erb => erb }142 VCR::Cassette::Reader.should_receive(:new).with(143 anything, erb144 ).and_return(mock('reader', :read => empty_cassette_yaml))145 VCR::Cassette.new('foo', :record => :new_episodes).http_interactions146 end147 end148 it 'raises a friendly error when the cassette file is in the old VCR 1.x format' do149 VCR.configuration.cassette_library_dir = 'spec/fixtures/cassette_spec'150 expect {151 VCR::Cassette.new('1_x_cassette').http_interactions152 }.to raise_error(VCR::Errors::InvalidCassetteFormatError)153 end154 end155 describe '.new' do156 it "raises an error if given an invalid record mode" do157 expect { VCR::Cassette.new(:test, :record => :not_a_record_mode) }.to raise_error(ArgumentError)158 end159 it 'raises an error if given invalid options' do160 expect {161 VCR::Cassette.new(:test, :invalid => :option)162 }.to raise_error(ArgumentError)163 end164 it 'does not raise an error in the case of an empty file' do165 VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"166 VCR::Cassette.new('empty', :record => :none).send(:previously_recorded_interactions).should eq([])167 end168 VCR::Cassette::VALID_RECORD_MODES.each do |record_mode|169 stub_requests = (record_mode != :all)170 context "when VCR.configuration.default_cassette_options[:record] is :#{record_mode}" do171 before(:each) { VCR.configuration.default_cassette_options = { :record => record_mode } }172 it "defaults the record mode to #{record_mode} when VCR.configuration.default_cassette_options[:record] is #{record_mode}" do173 cassette = VCR::Cassette.new(:test)174 cassette.record_mode.should eq(record_mode)175 end176 end177 context "when :#{record_mode} is passed as the record option" do178 def stub_old_interactions(interactions)179 hashes = interactions.map(&:to_hash)180 VCR.cassette_serializers[:yaml].stub(:deserialize => { 'http_interactions' => hashes })181 VCR::HTTPInteraction.stub(:from_hash) do |hash|182 interactions[hashes.index(hash)]183 end184 end185 unless record_mode == :all186 let(:interaction_1) { http_interaction { |i| i.request.uri = 'http://example.com/foo' } }187 let(:interaction_2) { http_interaction { |i| i.request.uri = 'http://example.com/bar' } }188 let(:interactions) { [interaction_1, interaction_2] }189 before(:each) { VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec" }190 it 'updates the content_length headers when given :update_content_length_header => true' do191 stub_old_interactions(interactions)192 interaction_1.response.should_receive(:update_content_length_header)193 interaction_2.response.should_receive(:update_content_length_header)194 VCR::Cassette.new('example', :record => record_mode, :update_content_length_header => true).http_interactions195 end196 [nil, false].each do |val|197 it "does not update the content_lenth headers when given :update_content_length_header => #{val.inspect}" do198 stub_old_interactions(interactions)199 interaction_1.response.should_not_receive(:update_content_length_header)200 interaction_2.response.should_not_receive(:update_content_length_header)201 VCR::Cassette.new('example', :record => record_mode, :update_content_length_header => val).http_interactions202 end203 end204 context "and re_record_interval is 7.days" do205 let(:file_name) { File.join(VCR.configuration.cassette_library_dir, "cassette_name.yml") }206 subject { VCR::Cassette.new(File.basename(file_name).gsub('.yml', ''), :record => record_mode, :re_record_interval => 7.days) }207 context 'when the cassette file does not exist' do208 before(:each) { File.stub(:exist?).with(file_name).and_return(false) }209 it "has :#{record_mode} for the record mode" do210 subject.record_mode.should eq(record_mode)211 end212 end213 context 'when the cassette file does exist' do214 before(:each) do215 interactions = timestamps.map do |ts|216 http_interaction { |i| i.recorded_at = ts }.to_hash217 end218 yaml = YAML.dump("http_interactions" => interactions)219 File.stub(:exist?).with(file_name).and_return(true)220 File.stub(:size?).with(file_name).and_return(true)221 File.stub(:read).with(file_name).and_return(yaml)222 end223 context 'and the earliest recorded interaction was recorded less than 7 days ago' do224 let(:timestamps) do [225 Time.now - 6.days + 60,226 Time.now - 7.days + 60,227 Time.now - 5.days + 60228 ] end229 it "has :#{record_mode} for the record mode" do230 subject.record_mode.should eq(record_mode)231 end232 end233 context 'and the earliest recorded interaction was recorded more than 7 days ago' do234 let(:timestamps) do [235 Time.now - 6.days - 60,236 Time.now - 7.days - 60,237 Time.now - 5.days - 60238 ] end239 it "has :all for the record mode when there is an internet connection available" do240 VCR::InternetConnection.stub(:available? => true)241 subject.record_mode.should eq(:all)242 end243 it "has :#{record_mode} for the record mode when there is no internet connection available" do244 VCR::InternetConnection.stub(:available? => false)245 subject.record_mode.should eq(record_mode)246 end247 end248 end249 end250 end251 it 'does not load ignored interactions' do252 VCR.request_ignorer.stub(:ignore?) do |request|253 request.uri !~ /example\.com/254 end255 VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"256 cassette = VCR::Cassette.new('with_localhost_requests', :record => record_mode)257 cassette.send(:previously_recorded_interactions).map { |i| URI.parse(i.request.uri).host }.should eq(%w[example.com])258 end259 it "loads the recorded interactions from the library yml file" do260 VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"261 cassette = VCR::Cassette.new('example', :record => record_mode)262 cassette.should have(3).previously_recorded_interactions263 i1, i2, i3 = *cassette.send(:previously_recorded_interactions)264 i1.request.method.should eq(:get)265 i1.request.uri.should eq('http://example.com/')266 i1.response.body.should =~ /You have reached this web page by typing.+example\.com/267 i2.request.method.should eq(:get)268 i2.request.uri.should eq('http://example.com/foo')269 i2.response.body.should =~ /foo was not found on this server/270 i3.request.method.should eq(:get)271 i3.request.uri.should eq('http://example.com/')272 i3.response.body.should =~ /Another example\.com response/273 end274 [true, false].each do |value|275 it "instantiates the http_interactions with allow_playback_repeats = #{value} if given :allow_playback_repeats => #{value}" do276 VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"277 cassette = VCR::Cassette.new('example', :record => record_mode, :allow_playback_repeats => value)278 cassette.http_interactions.allow_playback_repeats.should eq(value)279 end280 end281 it "instantiates the http_interactions with parent_list set to a null list if given :exclusive => true" do282 VCR.stub(:http_interactions => stub)283 VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"284 cassette = VCR::Cassette.new('example', :record => record_mode, :exclusive => true)285 cassette.http_interactions.parent_list.should be(VCR::Cassette::HTTPInteractionList::NullList)286 end287 it "instantiates the http_interactions with parent_list set to VCR.http_interactions if given :exclusive => false" do288 VCR.stub(:http_interactions => stub)289 VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"290 cassette = VCR::Cassette.new('example', :record => record_mode, :exclusive => false)291 cassette.http_interactions.parent_list.should be(VCR.http_interactions)292 end293 if stub_requests294 it 'invokes the before_playback hooks' do295 VCR.configuration.should_receive(:invoke_hook).with(296 :before_playback,297 an_instance_of(VCR::HTTPInteraction::HookAware),298 an_instance_of(VCR::Cassette)299 ).exactly(3).times300 cassette = VCR::Cassette.new('example', :record => record_mode)301 cassette.should have(3).previously_recorded_interactions302 end303 it 'does not playback any interactions that are ignored in a before_playback hook' do304 VCR.configure do |c|305 c.before_playback { |i| i.ignore! if i.request.uri =~ /foo/ }306 end307 VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"308 cassette = VCR::Cassette.new('example', :record => record_mode)309 cassette.should have(2).previously_recorded_interactions310 end311 it 'instantiates the http_interactions with the loaded interactions and the request matchers' do312 VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"313 cassette = VCR::Cassette.new('example', :record => record_mode, :match_requests_on => [:body, :headers])314 cassette.http_interactions.interactions.should have(3).interactions315 cassette.http_interactions.request_matchers.should eq([:body, :headers])316 end317 else318 it 'instantiates the http_interactions with the no interactions and the request matchers' do319 VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"320 cassette = VCR::Cassette.new('example', :record => record_mode, :match_requests_on => [:body, :headers])321 cassette.http_interactions.interactions.should have(0).interactions322 cassette.http_interactions.request_matchers.should eq([:body, :headers])323 end324 end325 end326 end327 end328 describe '#eject' do329 it "writes the serializable_hash to disk as yaml" do330 cassette = VCR::Cassette.new(:eject_test)331 cassette.record_http_interaction http_interaction # so it has one332 cassette.should respond_to(:serializable_hash)333 cassette.stub(:serializable_hash => { "http_interactions" => [1, 3, 5] })334 expect { cassette.eject }.to change { File.exist?(cassette.file) }.from(false).to(true)335 saved_stuff = YAML.load_file(cassette.file)336 saved_stuff.should eq("http_interactions" => [1, 3, 5])337 end338 it 'invokes the appropriately tagged before_record hooks' do339 interactions = [340 http_interaction { |i| i.request.uri = 'http://foo.com/'; i.response.body = 'res 1' },341 http_interaction { |i| i.request.uri = 'http://bar.com/'; i.response.body = 'res 2' }342 ]343 cassette = VCR::Cassette.new('example', :tag => :foo)344 cassette.stub!(:new_recorded_interactions).and_return(interactions)345 VCR.configuration.stub(:invoke_hook).and_return([false])346 interactions.each do |i|347 VCR.configuration.should_receive(:invoke_hook).with(348 :before_record,349 an_instance_of(VCR::HTTPInteraction::HookAware),350 cassette351 ).ordered352 end353 cassette.eject354 end355 it 'does not record interactions that have been ignored' do356 interaction_1 = http_interaction { |i| i.request.uri = 'http://foo.com/'; i.response.body = 'res 1' }357 interaction_2 = http_interaction { |i| i.request.uri = 'http://bar.com/'; i.response.body = 'res 2' }358 hook_aware_interaction_1 = interaction_1.hook_aware359 interaction_1.stub(:hook_aware => hook_aware_interaction_1)360 hook_aware_interaction_1.ignore!361 cassette = VCR::Cassette.new('test_cassette')362 cassette.stub!(:new_recorded_interactions).and_return([interaction_1, interaction_2])363 cassette.eject364 saved_recorded_interactions = ::YAML.load_file(cassette.file)365 saved_recorded_interactions["http_interactions"].should eq([interaction_2.to_hash])366 end367 it 'does not write the cassette to disk if all interactions have been ignored' do368 interaction_1 = http_interaction { |i| i.request.uri = 'http://foo.com/'; i.response.body = 'res 1' }369 hook_aware_interaction_1 = interaction_1.hook_aware370 interaction_1.stub(:hook_aware => hook_aware_interaction_1)371 hook_aware_interaction_1.ignore!372 cassette = VCR::Cassette.new('test_cassette')373 cassette.stub!(:new_recorded_interactions).and_return([interaction_1])374 cassette.eject375 File.should_not exist(cassette.file)376 end377 it "writes the recorded interactions to a subdirectory if the cassette name includes a directory" do378 recorded_interactions = [http_interaction { |i| i.response.body = "subdirectory response" }]379 cassette = VCR::Cassette.new('subdirectory/test_cassette')380 cassette.stub(:new_recorded_interactions => recorded_interactions)381 expect { cassette.eject }.to change { File.exist?(cassette.file) }.from(false).to(true)382 saved_recorded_interactions = YAML.load_file(cassette.file)383 saved_recorded_interactions["http_interactions"].should eq(recorded_interactions.map(&:to_hash))384 end385 [:all, :none, :new_episodes].each do |record_mode|386 context "for a :record => :#{record_mode} cassette with previously recorded interactions" do387 subject { VCR::Cassette.new('example', :record => record_mode, :match_requests_on => [:uri]) }388 before(:each) do389 base_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"390 FileUtils.cp(base_dir + "/example.yml", VCR.configuration.cassette_library_dir + "/example.yml")391 end392 it "does not re-write to disk the previously recorded interactions if there are no new ones" do393 yaml_file = subject.file394 File.should_not_receive(:open).with(subject.file, 'w')395 expect { subject.eject }.to_not change { File.mtime(yaml_file) }396 end397 context 'when some new interactions have been recorded' do398 def interaction(response_body, request_attributes)399 http_interaction do |interaction|400 interaction.response.body = response_body401 request_attributes.each do |key, value|402 interaction.request.send("#{key}=", value)403 end404 end405 end406 let(:interaction_foo_1) { interaction("foo 1", :uri => 'http://foo.com/') }407 let(:interaction_foo_2) { interaction("foo 2", :uri => 'http://foo.com/') }408 let(:interaction_bar) { interaction("bar", :uri => 'http://bar.com/') }409 let(:saved_recorded_interactions) { YAML.load_file(subject.file)['http_interactions'].map { |h| VCR::HTTPInteraction.from_hash(h) } }410 let(:now) { Time.utc(2011, 6, 11, 12, 30) }411 before(:each) do412 Time.stub(:now => now)413 subject.stub(:previously_recorded_interactions => [interaction_foo_1])414 subject.record_http_interaction(interaction_foo_2)415 subject.record_http_interaction(interaction_bar)416 subject.eject417 end418 if record_mode == :all419 it 'replaces previously recorded interactions with new ones when the requests match' do420 saved_recorded_interactions.first.should eq(interaction_foo_2)421 saved_recorded_interactions.should_not include(interaction_foo_1)422 end423 it 'appends new recorded interactions that do not match existing ones' do424 saved_recorded_interactions.last.should eq(interaction_bar)425 end...
hook_into_http_library.rb
Source:hook_into_http_library.rb
...46 describe 'making an HTTP request' do47 let(:status) { VCR::ResponseStatus.new(200, 'OK') }48 let(:interaction) { VCR::HTTPInteraction.new(request, response) }49 let(:response_body) { "The response body" }50 before(:each) do51 stub_requests([interaction], [:method, :uri])52 end53 context "when the the stubbed request and response has no headers" do54 let(:request) { VCR::Request.new(:get, 'http://example.com:80/') }55 let(:response) { VCR::Response.new(status, nil, response_body, '1.1') }56 it 'returns the response for a matching request' do57 get_body_string(make_http_request(:get, 'http://example.com/')).should eq(response_body)58 end59 end60 def self.test_playback(description, url)61 context "when a URL #{description} has been stubbed" do62 let(:request) { VCR::Request.new(:get, url) }63 let(:response) { VCR::Response.new(status, nil, response_body, '1.1') }64 it 'returns the expected response for the same request' do65 get_body_string(make_http_request(:get, url)).should eq(response_body)66 end67 end68 end69 test_playback "using https and no explicit port", "https://example.com/foo"70 test_playback "using https and port 443", "https://example.com:443/foo"71 test_playback "using https and some other port", "https://example.com:5190/foo"72 test_playback "that has query params", "http://example.com/search?q=param"73 test_playback "with an encoded ampersand", "http://example.com:80/search?q=#{CGI.escape("Q&A")}"74 end75 it 'does not query the http interaction list excessively' do76 call_count = 077 [:has_interaction_matching?, :response_for].each do |method_name|78 orig_meth = VCR.http_interactions.method(method_name)79 VCR.http_interactions.stub(method_name) do |*args|80 call_count += 181 orig_meth.call(*args)82 end83 end84 VCR.insert_cassette('foo')85 make_http_request(:get, "http://localhost:#{VCR::SinatraApp.port}/foo")86 call_count.should eq(1)87 end88 describe "using the library's stubbing/disconnection APIs" do89 let!(:request_url) { "http://localhost:#{VCR::SinatraApp.port}/foo" }90 if method_defined?(:disable_real_connections)91 it 'can make a real request when VCR is turned off' do92 enable_real_connections93 VCR.turn_off!94 get_body_string(make_http_request(:get, request_url)).should eq("FOO!")95 end96 it 'does not mess with VCR when real connections are disabled' do97 VCR.insert_cassette('example')98 disable_real_connections99 VCR.should_receive(:record_http_interaction) do |interaction|100 interaction.request.uri.should eq(request_url)101 end102 make_http_request(:get, request_url)103 end104 it 'can disable real connections when VCR is turned off' do105 VCR.turn_off!106 expected_error = disable_real_connections107 expect {108 make_http_request(:get, request_url)109 }.to raise_error(expected_error)110 end111 end112 if method_defined?(:directly_stub_request)113 it 'can directly stub the request when VCR is turned off' do114 VCR.turn_off!115 directly_stub_request(:get, request_url, "stubbed response")116 get_body_string(make_http_request(:get, request_url)).should eq("stubbed response")117 end118 end119 end120 describe "request hooks" do121 context 'when there is an around_http_request hook' do122 let(:request_url) { "http://localhost:#{VCR::SinatraApp.port}/foo" }123 it 'yields the request to the block' do124 yielded_request = nil125 VCR.configuration.around_http_request do |request|126 yielded_request = request127 request.proceed128 end129 VCR.use_cassette('new_cassette') do130 make_http_request(:get, request_url)131 end132 yielded_request.method.should eq(:get)133 yielded_request.uri.should eq(request_url)134 end135 it 'returns the response from request.proceed' do136 response = nil137 VCR.configuration.around_http_request do |request|138 response = request.proceed139 end140 VCR.use_cassette('new_cassette') do141 make_http_request(:get, request_url)142 end143 response.body.should eq("FOO!")144 end145 it 'can be used to use a cassette for a request' do146 VCR.configuration.around_http_request do |request|147 VCR.use_cassette('new_cassette', &request)148 end149 VCR.should_receive(:record_http_interaction) do150 VCR.current_cassette.name.should eq('new_cassette')151 end152 VCR.current_cassette.should be_nil153 make_http_request(:get, request_url)154 VCR.current_cassette.should be_nil155 end156 it 'nests them inside each other, making the first declared hook the outermost' do157 order = []158 VCR.configure do |c|159 c.ignore_request { |r| true }160 c.around_http_request do |request|161 order << :before_1162 request.proceed163 order << :after_1164 end165 c.around_http_request do |request|166 order << :before_2167 request.proceed168 order << :after_2169 end170 end171 make_http_request(:get, request_url)172 order.should eq([:before_1, :before_2, :after_2, :after_1])173 end174 it 'raises an appropriate error if the hook does not call request.proceed' do175 VCR.configuration.ignore_request { |r| true }176 hook_declaration = "#{__FILE__}:#{__LINE__ + 1}"177 VCR.configuration.around_http_request { |r| }178 expect {179 make_http_request(:get, request_url)180 }.to raise_error { |error|181 error.message.should include('must call #proceed on the yielded request')182 error.message.should include(hook_declaration)183 }184 end185 it 'does not get a dead fiber error when multiple requests are made' do186 VCR.configuration.around_http_request do |request|187 VCR.use_cassette('new_cassette', &request)188 end189 3.times { make_http_request(:get, request_url) }190 end191 it 'allows the hook to be filtered' do192 order = []193 VCR.configure do |c|194 c.ignore_request { |r| true }195 c.around_http_request(lambda { |r| r.uri =~ /foo/}) do |request|196 order << :before_foo197 request.proceed198 order << :after_foo199 end200 c.around_http_request(lambda { |r| r.uri !~ /foo/}) do |request|201 order << :before_not_foo202 request.proceed203 order << :after_not_foo204 end205 end206 make_http_request(:get, request_url)207 order.should eq([:before_foo, :after_foo])208 end209 it 'ensures that both around/before are invoked or neither' do210 order = []211 allow_1, allow_2 = false, true212 VCR.configure do |c|213 c.ignore_request { |r| true }214 c.around_http_request(lambda { |r| allow_1 = !allow_1 }) do |request|215 order << :before_1216 request.proceed217 order << :after_1218 end219 c.around_http_request(lambda { |r| allow_2 = !allow_2 }) do |request|220 order << :before_2221 request.proceed222 order << :after_2223 end224 end225 make_http_request(:get, request_url)226 order.should eq([:before_1, :after_1])227 end228 end if RUBY_VERSION >= '1.9'229 it 'correctly assigns the correct type to both before and after request hooks, even if they are different' do230 before_type = after_type = nil231 VCR.configuration.before_http_request do |request|232 before_type = request.type233 VCR.insert_cassette('example')234 end235 VCR.configuration.after_http_request do |request|236 after_type = request.type237 VCR.eject_cassette238 end239 make_http_request(:get, "http://localhost:#{VCR::SinatraApp.port}/foo")240 before_type.should be(:unhandled)241 after_type.should be(:recordable)242 end243 context "when the request is ignored" do244 before(:each) do245 VCR.configuration.ignore_request { |r| true }246 end247 it_behaves_like "request hooks", library_hook_name, :ignored248 end249 context 'when the request is recorded' do250 let!(:inserted_cassette) { VCR.insert_cassette('new_cassette') }251 it_behaves_like "request hooks", library_hook_name, :recordable do252 let(:string_in_cassette) { 'example.com get response 1 with path=foo' }253 it 'plays back the cassette when a request is made' do254 VCR.eject_cassette255 VCR.configure do |c|256 c.cassette_library_dir = File.join(VCR::SPEC_ROOT, 'fixtures')257 c.before_http_request do |request|258 VCR.insert_cassette('fake_example_responses', :record => :none)259 end260 end261 get_body_string(make_http_request(:get, 'http://example.com/foo')).should eq(string_in_cassette)262 end263 specify 'the after_http_request hook can be used to eject a cassette after the request is recorded' do264 VCR.configuration.after_http_request { |request| VCR.eject_cassette }265 VCR.should_receive(:record_http_interaction) do |interaction|266 VCR.current_cassette.should be(inserted_cassette)267 end268 make_request269 VCR.current_cassette.should be_nil270 end271 end272 end273 context 'when a stubbed response is played back for the request' do274 before(:each) do275 stub_requests([http_interaction(request_url)], [:method, :uri])276 end277 it_behaves_like "request hooks", library_hook_name, :stubbed278 end279 context 'when the request is not allowed' do280 it_behaves_like "request hooks", library_hook_name, :unhandled do281 undef assert_expected_response282 def assert_expected_response(response)283 response.should be_nil284 end285 undef make_request286 def make_request(disabled = false)287 if disabled288 make_http_request(:get, request_url)289 else290 expect { make_http_request(:get, request_url) }.to raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)291 end292 end293 end294 end295 end296 describe '.stub_requests using specific match_attributes' do297 before(:each) { VCR.stub(:real_http_connections_allowed? => false) }298 let(:interactions) { interactions_from('match_requests_on.yml') }299 let(:normalized_interactions) do300 interactions.each do |i|301 i.request.headers = normalize_request_headers(i.request.headers)302 end303 interactions304 end305 def self.matching_on(attribute, valid, invalid, &block)306 describe ":#{attribute}" do307 let(:perform_stubbing) { stub_requests(normalized_interactions, [attribute]) }308 before(:each) { perform_stubbing }309 module_eval(&block)310 valid.each do |val, response|311 it "returns the expected response for a #{val.inspect} request" do312 get_body_string(make_http_request(val)).should eq(response)313 end314 end315 it "raises an error for a request with a different #{attribute}" do316 expect { make_http_request(invalid) }.to raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)317 end318 end319 end320 matching_on :method, { :get => "get method response", :post => "post method response" }, :put do321 def make_http_request(http_method)322 make_request(http_method, 'http://some-wrong-domain.com/', nil, {})323 end324 end325 matching_on :host, { 'example1.com' => 'example1.com host response', 'example2.com' => 'example2.com host response' }, 'example3.com' do326 def make_http_request(host)327 make_request(:get, "http://#{host}/some/wrong/path", nil, {})328 end329 end330 matching_on :path, { '/path1' => 'path1 response', '/path2' => 'path2 response' }, '/path3' do331 def make_http_request(path)332 make_request(:get, "http://some.wrong.domain.com#{path}?p=q", nil, {})333 end334 end335 matching_on :uri, { 'http://example.com/uri1' => 'uri1 response', 'http://example.com/uri2' => 'uri2 response' }, 'http://example.com/uri3' do336 def make_http_request(uri)337 make_request(:get, uri, nil, {})338 end339 end340 matching_on :body, { 'param=val1' => 'val1 body response', 'param=val2' => 'val2 body response' }, 'param=val3' do341 def make_http_request(body)342 make_request(:put, "http://wrong-domain.com/wrong/path", body, {})343 end344 end345 matching_on :headers, {{ 'X-Http-Header1' => 'val1' } => 'val1 header response', { 'X-Http-Header1' => 'val2' } => 'val2 header response' }, { 'X-Http-Header1' => 'val3' } do346 def make_http_request(headers)347 make_request(:get, "http://wrong-domain.com/wrong/path", nil, headers)348 end349 end350 end351 def self.test_real_http_request(http_allowed, *other)352 let(:url) { "http://localhost:#{VCR::SinatraApp.port}/foo" }353 if http_allowed354 it 'allows real http requests' do355 get_body_string(make_http_request(:get, url)).should eq('FOO!')356 end357 describe 'recording new http requests' do358 let(:recorded_interaction) do359 interaction = nil360 VCR.should_receive(:record_http_interaction) { |i| interaction = i }361 make_http_request(:post, url, "the body", { 'X-Http-Foo' => 'bar' })362 interaction363 end364 it 'does not record the request if the hook is disabled' do365 VCR.library_hooks.exclusively_enabled :something_else do366 VCR.should_not_receive(:record_http_interaction)367 make_http_request(:get, url)368 end369 end unless other.include?(:not_disableable)370 it 'records the request uri' do371 recorded_interaction.request.uri.should eq(url)372 end373 it 'records the request method' do374 recorded_interaction.request.method.should eq(:post)375 end376 it 'records the request body' do377 recorded_interaction.request.body.should eq("the body")378 end379 it 'records the request headers' do380 headers = downcase_headers(recorded_interaction.request.headers)381 headers.should include('x-http-foo' => ['bar'])382 end383 it 'records the response status code' do384 recorded_interaction.response.status.code.should eq(200)385 end386 it 'records the response status message' do387 recorded_interaction.response.status.message.strip.should eq('OK')388 end unless other.include?(:status_message_not_exposed)389 it 'records the response body' do390 recorded_interaction.response.body.should eq('FOO!')391 end392 it 'records the response headers' do393 headers = downcase_headers(recorded_interaction.response.headers)394 headers.should include('content-type' => ["text/html;charset=utf-8"])395 end396 end397 else398 it 'does not allow real HTTP requests or record them' do399 VCR.should_receive(:record_http_interaction).never400 expect { make_http_request(:get, url) }.to raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)401 end402 end403 end404 [true, false].each do |http_allowed|405 context "when VCR.real_http_connections_allowed? is returning #{http_allowed}" do406 before(:each) { VCR.stub(:real_http_connections_allowed? => http_allowed) }407 test_real_http_request(http_allowed, *other)408 unless http_allowed409 localhost_response = "Localhost response"410 context 'when ignore_hosts is configured to "127.0.0.1", "localhost"' do411 before(:each) do412 VCR.configure { |c| c.ignore_hosts "127.0.0.1", "localhost" }413 end414 %w[ 127.0.0.1 localhost ].each do |localhost_alias|415 it "allows requests to #{localhost_alias}" do416 get_body_string(make_http_request(:get, "http://#{localhost_alias}:#{VCR::SinatraApp.port}/localhost_test")).should eq(localhost_response)417 end418 end419 it 'does not allow requests to 0.0.0.0' do420 expect { make_http_request(:get, "http://0.0.0.0:#{VCR::SinatraApp.port}/localhost_test") }.to raise_error(NET_CONNECT_NOT_ALLOWED_ERROR)421 end422 end423 end424 context 'when some requests are stubbed' do425 let(:interactions) { interactions_from('fake_example_responses.yml') }426 before(:each) do427 stub_requests(interactions, VCR::RequestMatcherRegistry::DEFAULT_MATCHERS)428 end429 it 'gets the stubbed responses when requests are made to http://example.com/foo, and does not record them' do430 VCR.should_receive(:record_http_interaction).never431 get_body_string(make_http_request(:get, 'http://example.com/foo')).should =~ /example\.com get response \d with path=foo/432 end433 it 'rotates through multiple responses for the same request' do434 get_body_string(make_http_request(:get, 'http://example.com/foo')).should eq('example.com get response 1 with path=foo')435 get_body_string(make_http_request(:get, 'http://example.com/foo')).should eq('example.com get response 2 with path=foo')436 end unless other.include?(:does_not_support_rotating_responses)437 it "correctly handles stubbing multiple values for the same header" do438 header = get_header('Set-Cookie', make_http_request(:get, 'http://example.com/two_set_cookie_headers'))439 header = header.split(', ') if header.respond_to?(:split)440 header.should =~ ['bar=bazz', 'foo=bar']...
each
Using AI Code Generation
1VCR.use_cassette('google') do2 response = Net::HTTP.get_response(URI('http://www.google.com'))3VCR.use_cassette('yahoo') do4 response = Net::HTTP.get_response(URI('http://www.yahoo.com'))5VCR.use_cassette('google') do6 response = Net::HTTP.get_response(URI('http://www.google.com'))7VCR.use_cassette('yahoo') do8 response = Net::HTTP.get_response(URI('http://www.yahoo.com'))9VCR.use_cassette('google') do10 response = Net::HTTP.get_response(URI('http://www.google.com'))11VCR.use_cassette('yahoo') do12 response = Net::HTTP.get_response(URI('http://www.yahoo.com'))13VCR.use_cassette('google') do14 response = Net::HTTP.get_response(URI('http://www.google.com'))15VCR.use_cassette('yahoo') do16 response = Net::HTTP.get_response(URI('http://www.yahoo.com'))17VCR.use_cassette('google') do18 response = Net::HTTP.get_response(URI('http://www.google.com'))19VCR.use_cassette('yahoo') do20 response = Net::HTTP.get_response(URI('http://www.yahoo.com'))21VCR.use_cassette('google') do22 response = Net::HTTP.get_response(URI('http://www.google.com'))23VCR.use_cassette('yahoo') do24 response = Net::HTTP.get_response(URI('http://www.yahoo.com'))25VCR.use_cassette('google') do26 response = Net::HTTP.get_response(URI('http://www.google.com'))
each
Using AI Code Generation
1 VCR.use_cassette('test') do2 ui = URI.pars('http://www.google.m/')3 esponse = Net::HTTP.get_response(uri)4 VCR.use_cassette('test') do5 uri = URI.parse('http://www.google.com/')6 response = Net::HTTP.get_response(uri)
each
Using AI Code Generation
1 VCR.use_cassette('test') do2 uri = URI.parse('http://www.google.com/')3 response = Net::HTTP.get_response(uri)4 VCR.use_cassette('test') do5 uri = URI.parse('http://www.google.com/')6 response = Net::HTTP.get_response(uri)
each
Using AI Code Generation
1VCR.use_cassette('google') do2 response = Net::HTTP.get_response(URI('http://www.google.com'))3VCR.use_cassette('yahoo') do4 response = Net::HTTP.get_response(URI('http://www.yahoo.com'))5VCR.use_cassette('google') do6 response = Net::HTTP.get_response(URI('http://www.google.com'))7VCR.use_cassette('yahoo') do8 response = Net::HTTP.get_response(URI('http://www.yahoo.com'))9VCR.use_cassette('google') do10 response = Net::HTTP.get_response(URI('http://www.google.com'))11VCR.use_cassette('yahoo') do12 response = Net::HTTP.get_response(URI('http://www.yahoo.com'))13VCR.use_cassette('google') do14 response = Net::HTTP.get_response(URI('http://www.google.com'))15VCR.use_cassette('yahoo') do16 response = Net::HTTP.get_response(URI('http://www.yahoo.com'))17VCR.use_cassette('google') do18 response = Net::HTTP.get_response(URI('http://www.google.com'))19VCR.use_cassette('yahoo') do20 response = Net::HTTP.get_response(URI('http://www.yahoo.com'))21VCR.use_cassette('google') do22 response = Net::HTTP.get_response(URI('http://www.google.com'))23VCR.use_cassette('yahoo') do24 response = Net::HTTP.get_response(URI('http://www.yahoo.com'))25VCR.use_cassette('google') do26 response = Net::HTTP.get_response(URI('http://www.google.com'))
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!