Best Vcr_ruby code snippet using VCR.ignore
configuration.rb
Source:configuration.rb
...57 def hook_into(*hooks)58 hooks.each { |a| load_library_hook(a) }59 invoke_hook(:after_library_hooks_loaded)60 end61 # Specifies host(s) that VCR should ignore.62 #63 # @param hosts [Array<String>] List of hosts to ignore64 # @see #ignore_localhost=65 # @see #ignore_request66 def ignore_hosts(*hosts)67 VCR.request_ignorer.ignore_hosts(*hosts)68 end69 alias ignore_host ignore_hosts70 # Sets whether or not VCR should ignore localhost requests.71 #72 # @param value [Boolean] the value to set73 # @see #ignore_hosts74 # @see #ignore_request75 def ignore_localhost=(value)76 VCR.request_ignorer.ignore_localhost = value77 end78 # Defines what requests to ignore using a block.79 #80 # @example81 # VCR.configure do |c|82 # c.ignore_request do |request|83 # uri = URI(request.uri)84 # # ignore only localhost requests to port 750085 # uri.host == 'localhost' && uri.port == 750086 # end87 # end88 #89 # @yield the callback90 # @yieldparam request [VCR::Request] the HTTP request91 # @yieldreturn [Boolean] whether or not to ignore the request92 def ignore_request(&block)93 VCR.request_ignorer.ignore_request(&block)94 end95 # Determines how VCR treats HTTP requests that are made when96 # no VCR cassette is in use. When set to `true`, requests made97 # when there is no VCR cassette in use will be allowed. When set98 # to `false` (the default), an {VCR::Errors::UnhandledHTTPRequestError}99 # will be raised for any HTTP request made when there is no100 # cassette in use.101 #102 # @overload allow_http_connections_when_no_cassette?103 # @return [Boolean] whether or not HTTP connections are allowed104 # when there is no cassette.105 # @overload allow_http_connections_when_no_cassette=106 # @param value [Boolean] sets whether or not to allow HTTP107 # connections when there is no cassette.108 attr_writer :allow_http_connections_when_no_cassette109 # @private (documented above)110 def allow_http_connections_when_no_cassette?111 !!@allow_http_connections_when_no_cassette112 end113 # Registers a request matcher for later use.114 #115 # @example116 # VCR.configure do |c|117 # c.register_request_matcher :port do |request_1, request_2|118 # URI(request_1.uri).port == URI(request_2.uri).port119 # end120 # end121 #122 # VCR.use_cassette("my_cassette", :match_requests_on => [:method, :host, :port]) do123 # # ...124 # end125 #126 # @param name [Symbol] the name of the request matcher127 # @yield the request matcher128 # @yieldparam request_1 [VCR::Request] One request129 # @yieldparam request_2 [VCR::Request] The other request130 # @yieldreturn [Boolean] whether or not these two requests should be considered131 # equivalent132 def register_request_matcher(name, &block)133 VCR.request_matchers.register(name, &block)134 end135 # Sets up a {#before_record} and a {#before_playback} hook that will136 # insert a placeholder string in the cassette in place of another string.137 # You can use this as a generic way to interpolate a variable into the138 # cassette for a unique string. It's particularly useful for unique139 # sensitive strings like API keys and passwords.140 #141 # @example142 # VCR.configure do |c|143 # # Put "<GITHUB_API_KEY>" in place of the actual API key in144 # # our cassettes so we don't have to commit to source control.145 # c.filter_sensitive_data('<GITHUB_API_KEY>') { GithubClient.api_key }146 #147 # # Put a "<USER_ID>" placeholder variable in our cassettes tagged with148 # # :user_cassette since it can be different for different test runs.149 # c.define_cassette_placeholder('<USER_ID>', :user_cassette) { User.last.id }150 # end151 #152 # @param placeholder [String] The placeholder string.153 # @param tag [Symbol] Set this apply this to only to cassettes154 # with a matching tag; otherwise it will apply to every cassette.155 # @yield block that determines what string to replace156 # @yieldparam interaction [(optional) VCR::HTTPInteraction::HookAware] the HTTP interaction157 # @yieldreturn the string to replace158 def define_cassette_placeholder(placeholder, tag = nil, &block)159 before_record(tag) do |interaction|160 orig_text = call_block(block, interaction)161 log "before_record: replacing #{orig_text.inspect} with #{placeholder.inspect}"162 interaction.filter!(orig_text, placeholder)163 end164 before_playback(tag) do |interaction|165 orig_text = call_block(block, interaction)166 log "before_playback: replacing #{placeholder.inspect} with #{orig_text.inspect}"167 interaction.filter!(placeholder, orig_text)168 end169 end170 alias filter_sensitive_data define_cassette_placeholder171 # Gets the registry of cassette serializers. Use it to register a custom serializer.172 #173 # @example174 # VCR.configure do |c|175 # c.cassette_serializers[:my_custom_serializer] = my_custom_serializer176 # end177 #178 # @return [VCR::Cassette::Serializers] the cassette serializer registry object.179 # @note Custom serializers must implement the following interface:180 #181 # * `file_extension # => String`182 # * `serialize(Hash) # => String`183 # * `deserialize(String) # => Hash`184 def cassette_serializers185 VCR.cassette_serializers186 end187 # Adds a callback that will be called before the recorded HTTP interactions188 # are serialized and written to disk.189 #190 # @example191 # VCR.configure do |c|192 # # Don't record transient 5xx errors193 # c.before_record do |interaction|194 # interaction.ignore! if interaction.response.status.code >= 500195 # end196 #197 # # Modify the response body for cassettes tagged with :twilio198 # c.before_record(:twilio) do |interaction|199 # interaction.response.body.downcase!200 # end201 # end202 #203 # @param tag [(optional) Symbol] Used to apply this hook to only cassettes that match204 # the given tag.205 # @yield the callback206 # @yieldparam interaction [VCR::HTTPInteraction::HookAware] The interaction that will be207 # serialized and written to disk.208 # @yieldparam cassette [(optional) VCR::Cassette] The current cassette.209 # @see #before_playback210 define_hook :before_record211 def before_record(tag = nil, &block)212 super(filter_from(tag), &block)213 end214 # Adds a callback that will be called before a previously recorded215 # HTTP interaction is loaded for playback.216 #217 # @example218 # VCR.configure do |c|219 # # Don't playback transient 5xx errors220 # c.before_playback do |interaction|221 # interaction.ignore! if interaction.response.status.code >= 500222 # end223 #224 # # Change a response header for playback225 # c.before_playback(:twilio) do |interaction|226 # interaction.response.headers['X-Foo-Bar'] = 'Bazz'227 # end228 # end229 #230 # @param tag [(optional) Symbol] Used to apply this hook to only cassettes that match231 # the given tag.232 # @yield the callback233 # @yieldparam interaction [VCR::HTTPInteraction::HookAware] The interaction that is being234 # loaded.235 # @yieldparam cassette [(optional) VCR::Cassette] The current cassette.236 # @see #before_record237 define_hook :before_playback238 def before_playback(tag = nil, &block)239 super(filter_from(tag), &block)240 end241 # Adds a callback that will be called with each HTTP request before it is made.242 #243 # @example244 # VCR.configure do |c|245 # c.before_http_request(:real?) do |request|246 # puts "Request: #{request.method} #{request.uri}"247 # end248 # end249 #250 # @param filters [optional splat of #to_proc] one or more filters to apply.251 # The objects provided will be converted to procs using `#to_proc`. If provided,252 # the callback will only be invoked if these procs all return `true`.253 # @yield the callback254 # @yieldparam request [VCR::Request::Typed] the request that is being made255 # @see #after_http_request256 # @see #around_http_request257 define_hook :before_http_request258 # Adds a callback that will be called with each HTTP request after it is complete.259 #260 # @example261 # VCR.configure do |c|262 # c.after_http_request(:ignored?) do |request, response|263 # puts "Request: #{request.method} #{request.uri}"264 # puts "Response: #{response.status.code}"265 # end266 # end267 #268 # @param filters [optional splat of #to_proc] one or more filters to apply.269 # The objects provided will be converted to procs using `#to_proc`. If provided,270 # the callback will only be invoked if these procs all return `true`.271 # @yield the callback272 # @yieldparam request [VCR::Request::Typed] the request that is being made273 # @yieldparam response [VCR::Response] the response from the request274 # @see #before_http_request275 # @see #around_http_request276 define_hook :after_http_request, :prepend...
vcr.rb
Source:vcr.rb
...5require 'vcr/configuration'6require 'vcr/deprecations'7require 'vcr/errors'8require 'vcr/library_hooks'9require 'vcr/request_ignorer'10require 'vcr/request_matcher_registry'11require 'vcr/structs'12require 'vcr/version'13# The main entry point for VCR.14# @note This module is extended onto itself; thus, the methods listed15# here as instance methods are available directly off of VCR.16module VCR17 include VariableArgsBlockCaller18 include Errors19 extend self20 autoload :CucumberTags, 'vcr/test_frameworks/cucumber'21 autoload :InternetConnection, 'vcr/util/internet_connection'22 autoload :RSpec, 'vcr/test_frameworks/rspec'23 module Middleware24 autoload :Faraday, 'vcr/middleware/faraday'25 autoload :Rack, 'vcr/middleware/rack'26 end27 # The currently active cassette.28 #29 # @return [nil, VCR::Cassette] The current cassette or nil if there is30 # no current cassette.31 def current_cassette32 cassettes.last33 end34 # Inserts the named cassette using the given cassette options.35 # New HTTP interactions, if allowed by the cassette's `:record` option, will36 # be recorded to the cassette. The cassette's existing HTTP interactions37 # will be used to stub requests, unless prevented by the cassete's38 # `:record` option.39 #40 # @example41 # VCR.insert_cassette('twitter', :record => :new_episodes)42 #43 # # ...later, after making an HTTP request:44 #45 # VCR.eject_cassette46 #47 # @param name [#to_s] The name of the cassette. VCR will sanitize48 # this to ensure it is a valid file name.49 # @param options [Hash] The cassette options. The given options will50 # be merged with the configured default_cassette_options.51 # @option options :record [:all, :none, :new_episodes, :once] The record mode.52 # @option options :erb [Boolean, Hash] Whether or not to evaluate the53 # cassette as an ERB template. Defaults to false. A hash can be used54 # to provide the ERB template with local variables.55 # @option options :match_requests_on [Array<Symbol, #call>] List of request matchers56 # to use to determine what recorded HTTP interaction to replay. Defaults to57 # [:method, :uri]. The built-in matchers are :method, :uri, :host, :path, :headers58 # and :body. You can also pass the name of a registered custom request matcher or59 # any object that responds to #call.60 # @option options :re_record_interval [Integer] When given, the61 # cassette will be re-recorded at the given interval, in seconds.62 # @option options :tag [Symbol] Used to apply tagged `before_record`63 # and `before_playback` hooks to the cassette.64 # @option options :tags [Array<Symbol>] Used to apply multiple tags to65 # a cassette so that tagged `before_record` and `before_playback` hooks66 # will apply to the cassette.67 # @option options :update_content_length_header [Boolean] Whether or68 # not to overwrite the Content-Length header of the responses to69 # match the length of the response body. Defaults to false.70 # @option options :allow_playback_repeats [Boolean] Whether or not to71 # allow a single HTTP interaction to be played back multiple times.72 # Defaults to false.73 # @option options :exclusive [Boolean] Whether or not to use only this74 # cassette and to completely ignore any cassettes in the cassettes stack.75 # Defaults to false.76 # @option options :serialize_with [Symbol] Which serializer to use.77 # Valid values are :yaml, :syck, :psych, :json or any registered78 # custom serializer. Defaults to :yaml.79 # @option options :preserve_exact_body_bytes [Boolean] Whether or not80 # to base64 encode the bytes of the requests and responses for this cassette81 # when serializing it. See also `VCR::Configuration#preserve_exact_body_bytes`.82 #83 # @return [VCR::Cassette] the inserted cassette84 #85 # @raise [ArgumentError] when the given cassette is already being used.86 # @raise [VCR::Errors::TurnedOffError] when VCR has been turned off87 # without using the :ignore_cassettes option.88 # @raise [VCR::Errors::MissingERBVariableError] when the `:erb` option89 # is used and the ERB template requires variables that you did not provide.90 #91 # @note If you use this method you _must_ call `eject_cassette` when you92 # are done. It is generally recommended that you use {#use_cassette}93 # unless your code-under-test cannot be run as a block.94 #95 def insert_cassette(name, options = {})96 if turned_on?97 if cassettes.any? { |c| c.name == name }98 raise ArgumentError.new("There is already a cassette with the same name (#{name}). You cannot nest multiple cassettes with the same name.")99 end100 cassette = Cassette.new(name, options)101 cassettes.push(cassette)102 cassette103 elsif !ignore_cassettes?104 message = "VCR is turned off. You must turn it on before you can insert a cassette. " +105 "Or you can use the `:ignore_cassettes => true` option to completely ignore cassette insertions."106 raise TurnedOffError.new(message)107 end108 end109 # Ejects the current cassette. The cassette will no longer be used.110 # In addition, any newly recorded HTTP interactions will be written to111 # disk.112 #113 # @return [VCR::Cassette, nil] the ejected cassette if there was one114 def eject_cassette115 cassette = cassettes.last116 cassette.eject if cassette117 cassettes.pop118 end119 # Inserts a cassette using the given name and options, runs the given120 # block, and ejects the cassette.121 #122 # @example123 # VCR.use_cassette('twitter', :record => :new_episodes) do124 # # make an HTTP request125 # end126 #127 # @param (see #insert_cassette)128 # @option (see #insert_cassette)129 # @yield Block to run while this cassette is in use.130 # @yieldparam cassette [(optional) VCR::Cassette] the cassette that has131 # been inserted.132 # @raise (see #insert_cassette)133 # @return [void]134 # @see #insert_cassette135 # @see #eject_cassette136 def use_cassette(name, options = {}, &block)137 cassette = insert_cassette(name, options)138 begin139 call_block(block, cassette)140 ensure141 eject_cassette142 end143 end144 # Used to configure VCR.145 #146 # @example147 # VCR.configure do |c|148 # c.some_config_option = true149 # end150 #151 # @yield the configuration block152 # @yieldparam config [VCR::Configuration] the configuration object153 # @return [void]154 def configure155 yield configuration156 end157 # @return [VCR::Configuration] the VCR configuration.158 def configuration159 @configuration ||= Configuration.new160 end161 # Sets up `Before` and `After` cucumber hooks in order to162 # use VCR with particular cucumber tags.163 #164 # @example165 # VCR.cucumber_tags do |t|166 # t.tags "tag1", "tag2"167 # t.tag "@some_other_tag", :record => :new_episodes168 # end169 #170 # @yield the cucumber tags configuration block171 # @yieldparam t [VCR::CucumberTags] Cucumber tags config object172 # @return [void]173 # @see VCR::CucumberTags#tags174 def cucumber_tags(&block)175 main_object = eval('self', block.binding)176 yield VCR::CucumberTags.new(main_object)177 end178 # Turns VCR off for the duration of a block.179 #180 # @param (see #turn_off!)181 # @return [void]182 # @raise (see #turn_off!)183 # @see #turn_off!184 # @see #turn_on!185 # @see #turned_on?186 def turned_off(options = {})187 turn_off!(options)188 begin189 yield190 ensure191 turn_on!192 end193 end194 # Turns VCR off, so that it no longer handles every HTTP request.195 #196 # @param options [Hash] hash of options197 # @option options :ignore_cassettes [Boolean] controls what happens when a cassette is198 # inserted while VCR is turned off. If +true+ is passed, the cassette insertion199 # will be ignored; otherwise a {VCR::Errors::TurnedOffError} will be raised.200 #201 # @return [void]202 # @raise [VCR::Errors::CassetteInUseError] if there is currently a cassette in use203 # @raise [ArgumentError] if you pass an invalid option204 def turn_off!(options = {})205 if VCR.current_cassette206 raise CassetteInUseError.new("A VCR cassette is currently in use. You must eject it before you can turn VCR off.")207 end208 @ignore_cassettes = options[:ignore_cassettes]209 invalid_options = options.keys - [:ignore_cassettes]210 if invalid_options.any?211 raise ArgumentError.new("You passed some invalid options: #{invalid_options.inspect}")212 end213 @turned_off = true214 end215 # Turns on VCR, if it has previously been turned off.216 # @return [void]217 # @see #turn_off!218 # @see #turned_off219 # @see #turned_on?220 def turn_on!221 @turned_off = false222 end223 # @return whether or not VCR is turned on224 # @note Normally VCR is _always_ turned on; it will only be off if you have225 # explicitly turned it off.226 # @see #turn_on!227 # @see #turn_off!228 # @see #turned_off229 def turned_on?230 !@turned_off231 end232 # @private233 def http_interactions234 return current_cassette.http_interactions if current_cassette235 VCR::Cassette::HTTPInteractionList::NullList236 end237 # @private238 def real_http_connections_allowed?239 return current_cassette.recording? if current_cassette240 configuration.allow_http_connections_when_no_cassette? || @turned_off241 end242 # @return [RequestMatcherRegistry] the request matcher registry243 def request_matchers244 @request_matchers ||= RequestMatcherRegistry.new245 end246 # @private247 def request_ignorer248 @request_ignorer ||= RequestIgnorer.new249 end250 # @private251 def library_hooks252 @library_hooks ||= LibraryHooks.new253 end254 # @private255 def cassette_serializers256 @cassette_serializers ||= Cassette::Serializers.new257 end258 # @private259 def record_http_interaction(interaction)260 return unless cassette = current_cassette261 return if VCR.request_ignorer.ignore?(interaction.request)262 cassette.record_http_interaction(interaction)263 end264private265 def ignore_cassettes?266 @ignore_cassettes267 end268 def cassettes269 @cassettes ||= []270 end271 def initialize_ivars272 @turned_off = false273 end274 initialize_ivars # to avoid warnings275end...
ignore
Using AI Code Generation
1VCR.use_cassette('test') do2VCR.use_cassette('test') do3VCR.use_cassette('test') do4VCR.use_cassette('test') do5VCR.use_cassette('test') do6VCR.use_cassette('test') do
ignore
Using AI Code Generation
1VCR.use_cassette('my_cassette') do2 URI(request.uri).host == 'codeclimate.com'3VCR.use_cassette('my_cassette') do4 URI(request.uri).host == 'codeclimate.com'5VCR.use_cassette('my_cassette') do6VCR.use_cassette('my_cassette') do7VCR.use_cassette('my_cassette') do
ignore
Using AI Code Generation
1VCR.use_cassette('test') do2 Net::HTTP.get(URI.parse('http://localhost:3000'))3 URI(request.uri).host == 'localhost'4VCR.use_cassette('test') do5 Net::HTTP.get(URI.parse('http://localhost:3000'))6VCR.use_cassette('test') do7 Net::HTTP.get(URI.parse('http://localhost:3000'))8 URI(request.uri).host == 'localhost'9VCR.use_cassette('test') do10 Net::HTTP.get(URI.parse('http://localhost:3000'))11 URI(request.uri).host == 'localhost'12VCR.use_cassette('test') do13 Net::HTTP.get(URI.parse('http://localhost
ignore
Using AI Code Generation
1 VCR.use_cassette('test') do2 stub_request(:get, 'http://example.com').to_return(:body => 'test')3 stub_request(:get, 'http://example.com').to_return(:body => 'test')4 response = Net::HTTP.get_response(URI.parse('http://example.com'))5 expect(response.body).to eq('test')6 VCR.use_cassette('test') do7 stub_request(:get, 'http://example.com').to_return(:body => 'test')8 stub_request(:get, 'http://example.com').to_return(:body => 'test')9 response = Net::HTTP.get_response(URI.parse('http://example.com'))10 expect(response.body).to eq('test')11 VCR.use_cassette('test') do12 stub_request(:get, 'http://example.com').to_return(:body => 'test')13 stub_request(:get, 'http://example.com').to_return(:body => 'test')14 response = Net::HTTP.get_response(URI.parse('http://example.com'))15 expect(response.body).to
ignore
Using AI Code Generation
1VCR.use_cassette('example') do2VCR.use_cassette('example') do3VCR . configure do | c | c . ignore_request do | request | URI ( request . uri ). host == 'localhost' end end
ignore
Using AI Code Generation
1 URI(request.uri).host == 'api.example.com'2VCR.use_cassette('example') do3 stub_request(:get, 'http://api.example.com').to_return(status: 200, body: 'hello')4 stub_request(:get, 'http://example.com').to_return(status: 200, body: 'hello')5VCR.use_cassette('example') do6 uri = URI.parse('http://api.example.com')7 http = Net::HTTP.new(uri.host, uri.port)8 request = Net::HTTP::Get.new(uri.request_uri)9 response = http.request(request)10VCR.use_cassette('example') do11 uri = URI.parse('http://example.com')12 http = Net::HTTP.new(uri.host, uri.port)13 request = Net::HTTP::Get.new(uri.request_uri)14 response = http.request(request)15 URI(request.uri).host == 'api.example.com'16VCR.use_cassette('example') do17 uri = URI.parse('http://api.example.com')18 http = Net::HTTP.new(uri.host, uri.port)19 request = Net::HTTP::Get.new(uri.request_uri)20 response = http.request(request)21VCR.use_cassette('example') do22 uri = URI.parse('http://example.com')23 http = Net::HTTP.new(uri.host, uri.port)24 request = Net::HTTP::Get.new(uri.request_uri)25 response = http.request(request)
ignore
Using AI Code Generation
1VCR.use_cassette("test") do2VCR.use_cassette('test') do3 Net::HTTP.get(URI.parse('http://localhost:3000'))4 URI(request.uri).host == 'localhost'5VCR.use_cassette('test') do6 Net::HTTP.get(URI.parse('http://localhost:3000'))7VCR.use_cassette('test') do8 Net::HTTP.get(URI.parse('http://localhost:3000'))9 URI(request.uri).host == 'localhost'10VCR.use_cassette('test') do11 Net::HTTP.get(URI.parse('http://localhost:3000'))12 URI(request.uri).host == 'localhost'13VCR.use_cassette('test') do14 Net::HTTP.get(URI.parse('http://localhost
ignore
Using AI Code Generation
1 VCR.use_cassette('test') do2 stub_request(:get, 'http://example.com').to_return(:body => 'test')3 stub_request(:get, 'http://example.com').to_return(:body => 'test')4 response = Net::HTTP.get_response(URI.parse('http://example.com'))5 expect(response.body).to eq('test')6 VCR.use_cassette('test') do7 stub_request(:get, 'http://example.com').to_return(:body => 'test')8 stub_request(:get, 'http://example.com').to_return(:body => 'test')9 response = Net::HTTP.get_response(URI.parse('http://example.com'))10 expect(response.body).to eq('test')11 VCR.use_cassette('test') do12 stub_request(:get, 'http://example.com').to_return(:body => 'test')13 stub_request(:get, 'http://example.com').to_return(:body => 'test')14 response = Net::HTTP.get_response(URI.parse('http://example.com'))15 expect(response.body).to
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!!