Best Vcr_ruby code snippet using VCR.new
cassette_spec.rb
Source:cassette_spec.rb
1require 'spec_helper'2describe VCR::Cassette do3 def http_interaction4 request = VCR::Request.new(:get)5 response = VCR::Response.new6 response.status = VCR::ResponseStatus.new7 VCR::HTTPInteraction.new(request, response).tap { |i| yield i if block_given? }8 end9 describe '#file' do10 it 'combines the cassette_library_dir with the cassette name' do11 cassette = VCR::Cassette.new('the_file')12 cassette.file.should eq(File.join(VCR.configuration.cassette_library_dir, 'the_file.yml'))13 end14 it 'uses the file extension from the serializer' do15 VCR.cassette_serializers[:custom] = stub(:file_extension => "custom")16 cassette = VCR::Cassette.new('the_file', :serialize_with => :custom)17 cassette.file.should =~ /\.custom$/18 end19 it 'strips out disallowed characters so that it is a valid file name with no spaces' do20 cassette = VCR::Cassette.new("\nthis \t! is-the_13212_file name")21 cassette.file.should =~ /#{Regexp.escape('_this_is-the_13212_file_name.yml')}$/22 end23 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 end426 else427 it 'appends new recorded interactions after existing ones' do428 saved_recorded_interactions.should eq([interaction_foo_1, interaction_foo_2, interaction_bar])429 end430 end431 end432 end433 end434 end435end...
vcr_spec.rb
Source:vcr_spec.rb
...3 def insert_cassette(name = :cassette_test)4 VCR.insert_cassette(name)5 end6 describe '.insert_cassette' do7 it 'creates a new cassette' do8 insert_cassette.should be_instance_of(VCR::Cassette)9 end10 it 'takes over as the #current_cassette' do11 orig_cassette = VCR.current_cassette12 new_cassette = insert_cassette13 new_cassette.should_not eq(orig_cassette)14 VCR.current_cassette.should eq(new_cassette)15 end16 it 'raises an error if the stack of inserted cassettes already contains a cassette with the same name' do17 insert_cassette(:foo)18 expect {19 insert_cassette(:foo)20 }.to raise_error(/There is already a cassette with the same name/)21 end22 end23 describe '.eject_cassette' do24 it 'ejects the current cassette' do25 cassette = insert_cassette26 cassette.should_receive(:eject)27 VCR.eject_cassette28 end29 it 'returns the ejected cassette' do30 cassette = insert_cassette31 VCR.eject_cassette.should eq(cassette)32 end33 it 'returns the #current_cassette to the previous one' do34 cassette1, cassette2 = insert_cassette(:foo1), insert_cassette(:foo2)35 expect { VCR.eject_cassette }.to change(VCR, :current_cassette).from(cassette2).to(cassette1)36 end37 it 'keeps the cassette as the current one until after #eject has finished' do38 cassette = insert_cassette39 current = nil40 cassette.stub(:eject) { current = VCR.current_cassette }41 VCR.eject_cassette42 current.should be(cassette)43 VCR.current_cassette.should_not be(cassette)44 end45 end46 describe '.use_cassette' do47 it 'inserts a new cassette' do48 new_cassette = VCR::Cassette.new(:use_cassette_test)49 VCR.should_receive(:insert_cassette).and_return(new_cassette)50 VCR.use_cassette(:cassette_test) { }51 end52 it 'yields' do53 yielded = false54 VCR.use_cassette(:cassette_test, &lambda { yielded = true })55 yielded.should be_true56 end57 it 'yields the cassette instance if the block expects an argument' do58 VCR.use_cassette('name', :record => :new_episodes, &lambda do |cassette|59 cassette.should equal(VCR.current_cassette)60 end)61 end62 it 'yields the cassette instance if the block expects a variable number of args' do63 VCR.use_cassette('name', :record => :new_episodes) do |*args|64 args.size.should eq(1)65 args.first.should equal(VCR.current_cassette)66 end67 end68 it 'ejects the cassette' do69 VCR.should_receive(:eject_cassette)70 VCR.use_cassette(:cassette_test) { }71 end72 it 'ejects the cassette even if there is an error' do73 VCR.should_receive(:eject_cassette)74 expect { VCR.use_cassette(:cassette_test) { raise StandardError } }.to raise_error75 end76 it 'does not eject a cassette if there was an error inserting it' do77 VCR.should_receive(:insert_cassette).and_raise(StandardError.new('Boom!'))78 VCR.should_not_receive(:eject_cassette)79 expect { VCR.use_cassette(:test) { } }.to raise_error(StandardError, 'Boom!')80 end81 end82 describe '.http_interactions' do83 it 'returns the current_cassette.http_interactions when there is a current cassette' do84 cassette = VCR.insert_cassette("a cassette")85 VCR.http_interactions.should be(cassette.http_interactions)86 end87 it 'returns a null list when there is no current cassette' do88 VCR.current_cassette.should be_nil89 VCR.http_interactions.should be(VCR::Cassette::HTTPInteractionList::NullList)90 end91 end...
typhoeus.rb
Source:typhoeus.rb
1require 'vcr/util/version_checker'2require 'vcr/request_handler'3require 'typhoeus'4VCR::VersionChecker.new('Typhoeus', Typhoeus::VERSION, '0.3.2', '0.3').check_version!5module VCR6 class LibraryHooks7 # @private8 module Typhoeus9 # @private10 class RequestHandler < ::VCR::RequestHandler11 attr_reader :request12 def initialize(request)13 @request = request14 end15 def vcr_request16 @vcr_request ||= VCR::Request.new \17 request.method,18 request.url,19 request.body,20 request.headers21 end22 private23 def set_typed_request_for_after_hook(*args)24 super25 request.instance_variable_set(:@__typed_vcr_request, @after_hook_typed_request)26 end27 def on_unhandled_request28 invoke_after_request_hook(nil)29 super30 end31 def on_stubbed_request32 ::Typhoeus::Response.new \33 :http_version => stubbed_response.http_version,34 :code => stubbed_response.status.code,35 :status_message => stubbed_response.status.message,36 :headers_hash => stubbed_response_headers,37 :body => stubbed_response.body38 end39 def stubbed_response_headers40 @stubbed_response_headers ||= {}.tap do |hash|41 stubbed_response.headers.each do |key, values|42 hash[key] = values.size == 1 ? values.first : values43 end if stubbed_response.headers44 end45 end46 end47 # @private48 def self.vcr_response_from(response)49 VCR::Response.new \50 VCR::ResponseStatus.new(response.code, response.status_message),51 response.headers_hash,52 response.body,53 response.http_version54 end55 ::Typhoeus::Hydra.after_request_before_on_complete do |request|56 unless VCR.library_hooks.disabled?(:typhoeus)57 vcr_response = vcr_response_from(request.response)58 typed_vcr_request = request.send(:remove_instance_variable, :@__typed_vcr_request)59 unless request.response.mock?60 http_interaction = VCR::HTTPInteraction.new(typed_vcr_request, vcr_response)61 VCR.record_http_interaction(http_interaction)62 end63 VCR.configuration.invoke_hook(:after_http_request, typed_vcr_request, vcr_response)64 end65 end66 ::Typhoeus::Hydra.register_stub_finder do |request|67 VCR::LibraryHooks::Typhoeus::RequestHandler.new(request).handle68 end69 end70 end71end72# @private73module Typhoeus74 class << Hydra75 # ensure HTTP requests are always allowed; VCR takes care of disallowing76 # them at the appropriate times in its hook77 def allow_net_connect_with_vcr?(*args)78 VCR.turned_on? ? true : allow_net_connect_without_vcr?79 end80 alias allow_net_connect_without_vcr? allow_net_connect?81 alias allow_net_connect? allow_net_connect_with_vcr?...
new
Using AI Code Generation
1 c.default_cassette_options = { :record => :new_episodes }2 c.default_cassette_options = { :record => :all }3 c.default_cassette_options = { :record => :new_episodes }4 c.default_cassette_options = { :record => :new_episodes }5 c.default_cassette_options = { :record => :all }6 c.default_cassette_options = { :record => :all }7 c.default_cassette_options = { :record => :all }8 c.default_cassette_options = { :record => :all }
new
Using AI Code Generation
1 c.default_cassette_options = { :record => :new_episodes }2 c.default_cassette_options = { :record => :none }3 c.default_cassette_options = { :record => :none }4 c.default_cassette_options = { :record => :new_episodes }5 c.default_cassette_options = { :record => :all }6 c.default_cassette_options = { :record => :none }
new
Using AI Code Generation
1 c.default_cassette_options = { :record => :new_episodes }2 c.default_cassette_options = { :record => :none }3 c.default_cassette_options = { :record => :new_episodes }4 c.default_cassette_options = { :record => :none }5 c.default_cassette_options = { :record => :new_episodes }6 c.default_cassette_options = { :record => :none }7 c.default_cassette_options = { :record =>
new
Using AI Code Generation
1VCR.use_cassette('my_cassette') do2VCR.use_cassette('my_cassette') do3VCR.use_cassette('my_cassette', :cassette_library_dir => 'spec/cassettes/1') do4VCR.use_cassette('my_cassette', :cassette_library_dir => 'spec/cassettes/2') do
new
Using AI Code Generation
1VCR.use_cassette('google') do2 puts Net::HTTP.get('www.google.com', '/')3VCR.use_cassette('google') do4 puts Net::HTTP.get('www.google.com', '/')5VCR.use_cassette('google') do6 puts Net::HTTP.get('www.google.com', '/')7VCR.use_cassette('google') do8 puts Net::HTTP.get('www.google.com', '/')9VCR.use_cassette('google') do10 puts Net::HTTP.get('www.google.com', '/')11VCR.use_cassette('google') do12 puts Net::HTTP.get('www.google.com', '/')
new
Using AI Code Generation
1 def initialize(user, repo)2 response = Net::HTTP.get_response(uri)3 JSON.parse(response.body)4 c.default_cassette_options = { :record =>
new
Using AI Code Generation
1VCR.use_cassette('my_cassette') do2VCR.use_cassette('my_cassette') do3VCR.use_cassette('my_cassette', :cassette_library_dir => 'spec/cassettes/1') do4VCR.use_cassette('my_cassette', :cassette_library_dir => 'spec/cassettes/2') do
new
Using AI Code Generation
1 c.default_cassette_options = { :record => :new_episodes }2 c.default_cassette_options = { :record => :all }3 c.default_cassette_options = { :record => :new_episodes }4 c.default_cassette_options = { :record => :new_episodes }5 c.default_cassette_options = { :record => :all }6 c.default_cassette_options = { :record => :all }7 c.default_cassette_options = { :record => :all }8 c.default_cassette_options = { :record => :all }
new
Using AI Code Generation
1VCR.use_cassette('my_cassette') do2VCR.use_cassette('my_cassette') do3VCR.use_cassette('my_cassette', :cassette_library_dir => 'spec/cassettes/1') do4VCR.use_cassette('my_cassette', :cassette_library_dir => 'spec/cassettes/2') do
new
Using AI Code Generation
1VCR.use_cassette('google') do2 puts Net::HTTP.get('www.google.com', '/')3VCR.use_cassette('google') do4 puts Net::HTTP.get('www.google.com', '/')5VCR.use_cassette('google') do6 puts Net::HTTP.get('www.google.com', '/')7VCR.use_cassette('google') do8 puts Net::HTTP.get('www.google.com', '/')9VCR.use_cassette('google') do10 puts Net::HTTP.get('www.google.com', '/')11VCR.use_cassette('google') do12 puts Net::HTTP.get('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!!