Best Vcr_ruby code snippet using Middleware.request_ignorer
vcr.rb
Source:vcr.rb
...14require 'vcr/configuration'15require 'vcr/deprecations'16require 'vcr/errors'17require 'vcr/library_hooks'18require 'vcr/request_ignorer'19require 'vcr/request_matcher_registry'20require 'vcr/structs'21require 'vcr/version'22# The main entry point for VCR.23# @note This module is extended onto itself; thus, the methods listed24# here as instance methods are available directly off of VCR.25module VCR26 include VariableArgsBlockCaller27 include Errors28 extend self29 autoload :CucumberTags, 'vcr/test_frameworks/cucumber'30 autoload :InternetConnection, 'vcr/util/internet_connection'31 module RSpec32 autoload :Metadata, 'vcr/test_frameworks/rspec'33 autoload :Macros, 'vcr/deprecations'34 end35 module Middleware36 autoload :Faraday, 'vcr/middleware/faraday'37 autoload :Rack, 'vcr/middleware/rack'38 end39 # The currently active cassette.40 #41 # @return [nil, VCR::Cassette] The current cassette or nil if there is42 # no current cassette.43 def current_cassette44 cassettes.last45 end46 # Inserts the named cassette using the given cassette options.47 # New HTTP interactions, if allowed by the cassette's `:record` option, will48 # be recorded to the cassette. The cassette's existing HTTP interactions49 # will be used to stub requests, unless prevented by the cassete's50 # `:record` option.51 #52 # @example53 # VCR.insert_cassette('twitter', :record => :new_episodes)54 #55 # # ...later, after making an HTTP request:56 #57 # VCR.eject_cassette58 #59 # @param name [#to_s] The name of the cassette. VCR will sanitize60 # this to ensure it is a valid file name.61 # @param options [Hash] The cassette options. The given options will62 # be merged with the configured default_cassette_options.63 # @option options :record [:all, :none, :new_episodes, :once] The record mode.64 # @option options :erb [Boolean, Hash] Whether or not to evaluate the65 # cassette as an ERB template. Defaults to false. A hash can be used66 # to provide the ERB template with local variables.67 # @option options :match_requests_on [Array<Symbol, #call>] List of request matchers68 # to use to determine what recorded HTTP interaction to replay. Defaults to69 # [:method, :uri]. The built-in matchers are :method, :uri, :host, :path, :headers70 # and :body. You can also pass the name of a registered custom request matcher or71 # any object that responds to #call.72 # @option options :re_record_interval [Integer] When given, the73 # cassette will be re-recorded at the given interval, in seconds.74 # @option options :tag [Symbol] Used to apply tagged `before_record`75 # and `before_playback` hooks to the cassette.76 # @option options :tags [Array<Symbol>] Used to apply multiple tags to77 # a cassette so that tagged `before_record` and `before_playback` hooks78 # will apply to the cassette.79 # @option options :update_content_length_header [Boolean] Whether or80 # not to overwrite the Content-Length header of the responses to81 # match the length of the response body. Defaults to false.82 # @option options :decode_compressed_response [Boolean] Whether or83 # not to decode compressed responses before recording the cassette.84 # This makes the cassette more human readable. Defaults to false.85 # @option options :allow_playback_repeats [Boolean] Whether or not to86 # allow a single HTTP interaction to be played back multiple times.87 # Defaults to false.88 # @option options :allow_unused_http_interactions [Boolean] If set to89 # false, an error will be raised if a cassette is ejected before all90 # previously recorded HTTP interactions have been used.91 # Defaults to true.92 # @option options :exclusive [Boolean] Whether or not to use only this93 # cassette and to completely ignore any cassettes in the cassettes stack.94 # Defaults to false.95 # @option options :serialize_with [Symbol] Which serializer to use.96 # Valid values are :yaml, :syck, :psych, :json or any registered97 # custom serializer. Defaults to :yaml.98 # @option options :persist_with [Symbol] Which cassette persister to99 # use. Defaults to :file_system. You can also register and use a100 # custom persister.101 # @option options :preserve_exact_body_bytes [Boolean] Whether or not102 # to base64 encode the bytes of the requests and responses for this cassette103 # when serializing it. See also `VCR::Configuration#preserve_exact_body_bytes`.104 #105 # @return [VCR::Cassette] the inserted cassette106 #107 # @raise [ArgumentError] when the given cassette is already being used.108 # @raise [VCR::Errors::TurnedOffError] when VCR has been turned off109 # without using the :ignore_cassettes option.110 # @raise [VCR::Errors::MissingERBVariableError] when the `:erb` option111 # is used and the ERB template requires variables that you did not provide.112 #113 # @note If you use this method you _must_ call `eject_cassette` when you114 # are done. It is generally recommended that you use {#use_cassette}115 # unless your code-under-test cannot be run as a block.116 #117 def insert_cassette(name, options = {})118 if turned_on?119 if cassettes.any? { |c| c.name == name }120 raise ArgumentError.new("There is already a cassette with the same name (#{name}). You cannot nest multiple cassettes with the same name.")121 end122 cassette = Cassette.new(name, options)123 cassettes.push(cassette)124 cassette125 elsif !ignore_cassettes?126 message = "VCR is turned off. You must turn it on before you can insert a cassette. " +127 "Or you can use the `:ignore_cassettes => true` option to completely ignore cassette insertions."128 raise TurnedOffError.new(message)129 end130 end131 # Ejects the current cassette. The cassette will no longer be used.132 # In addition, any newly recorded HTTP interactions will be written to133 # disk.134 #135 # @return [VCR::Cassette, nil] the ejected cassette if there was one136 def eject_cassette137 cassette = cassettes.last138 cassette.eject if cassette139 cassette140 ensure141 cassettes.pop142 end143 # Inserts a cassette using the given name and options, runs the given144 # block, and ejects the cassette.145 #146 # @example147 # VCR.use_cassette('twitter', :record => :new_episodes) do148 # # make an HTTP request149 # end150 #151 # @param (see #insert_cassette)152 # @option (see #insert_cassette)153 # @yield Block to run while this cassette is in use.154 # @yieldparam cassette [(optional) VCR::Cassette] the cassette that has155 # been inserted.156 # @raise (see #insert_cassette)157 # @return [void]158 # @see #insert_cassette159 # @see #eject_cassette160 def use_cassette(name, options = {}, &block)161 unless block162 raise ArgumentError, "`VCR.use_cassette` requires a block. " +163 "If you cannot wrap your code in a block, use " +164 "`VCR.insert_cassette` / `VCR.eject_cassette` instead."165 end166 cassette = insert_cassette(name, options)167 begin168 call_block(block, cassette)169 ensure170 eject_cassette171 end172 end173 # Used to configure VCR.174 #175 # @example176 # VCR.configure do |c|177 # c.some_config_option = true178 # end179 #180 # @yield the configuration block181 # @yieldparam config [VCR::Configuration] the configuration object182 # @return [void]183 def configure184 yield configuration185 end186 # @return [VCR::Configuration] the VCR configuration.187 def configuration188 @configuration ||= Configuration.new189 end190 # Sets up `Before` and `After` cucumber hooks in order to191 # use VCR with particular cucumber tags.192 #193 # @example194 # VCR.cucumber_tags do |t|195 # t.tags "tag1", "tag2"196 # t.tag "@some_other_tag", :record => :new_episodes197 # end198 #199 # @yield the cucumber tags configuration block200 # @yieldparam t [VCR::CucumberTags] Cucumber tags config object201 # @return [void]202 # @see VCR::CucumberTags#tags203 def cucumber_tags(&block)204 main_object = eval('self', block.binding)205 yield VCR::CucumberTags.new(main_object)206 end207 # Turns VCR off for the duration of a block.208 #209 # @param (see #turn_off!)210 # @return [void]211 # @raise (see #turn_off!)212 # @see #turn_off!213 # @see #turn_on!214 # @see #turned_on?215 def turned_off(options = {})216 turn_off!(options)217 begin218 yield219 ensure220 turn_on!221 end222 end223 # Turns VCR off, so that it no longer handles every HTTP request.224 #225 # @param options [Hash] hash of options226 # @option options :ignore_cassettes [Boolean] controls what happens when a cassette is227 # inserted while VCR is turned off. If `true` is passed, the cassette insertion228 # will be ignored; otherwise a {VCR::Errors::TurnedOffError} will be raised.229 #230 # @return [void]231 # @raise [VCR::Errors::CassetteInUseError] if there is currently a cassette in use232 # @raise [ArgumentError] if you pass an invalid option233 def turn_off!(options = {})234 if VCR.current_cassette235 raise CassetteInUseError, "A VCR cassette is currently in use (#{VCR.current_cassette.name}). " +236 "You must eject it before you can turn VCR off."237 end238 @ignore_cassettes = options[:ignore_cassettes]239 invalid_options = options.keys - [:ignore_cassettes]240 if invalid_options.any?241 raise ArgumentError.new("You passed some invalid options: #{invalid_options.inspect}")242 end243 @turned_off = true244 end245 # Turns on VCR, if it has previously been turned off.246 # @return [void]247 # @see #turn_off!248 # @see #turned_off249 # @see #turned_on?250 def turn_on!251 @turned_off = false252 end253 # @return whether or not VCR is turned on254 # @note Normally VCR is _always_ turned on; it will only be off if you have255 # explicitly turned it off.256 # @see #turn_on!257 # @see #turn_off!258 # @see #turned_off259 def turned_on?260 !@turned_off261 end262 # @private263 def http_interactions264 return current_cassette.http_interactions if current_cassette265 VCR::Cassette::HTTPInteractionList::NullList266 end267 # @private268 def real_http_connections_allowed?269 return current_cassette.recording? if current_cassette270 configuration.allow_http_connections_when_no_cassette? || @turned_off271 end272 # @return [RequestMatcherRegistry] the request matcher registry273 def request_matchers274 @request_matchers ||= RequestMatcherRegistry.new275 end276 # @private277 def request_ignorer278 @request_ignorer ||= RequestIgnorer.new279 end280 # @private281 def library_hooks282 @library_hooks ||= LibraryHooks.new283 end284 # @private285 def cassette_serializers286 @cassette_serializers ||= Cassette::Serializers.new287 end288 # @private289 def cassette_persisters290 @cassette_persisters ||= Cassette::Persisters.new291 end292 # @private293 def record_http_interaction(interaction)294 return unless cassette = current_cassette295 return if VCR.request_ignorer.ignore?(interaction.request)296 cassette.record_http_interaction(interaction)297 end298private299 def ignore_cassettes?300 @ignore_cassettes301 end302 def cassettes303 @cassettes ||= []304 end305 def initialize_ivars306 @turned_off = false307 end308 initialize_ivars # to avoid warnings309end...
request_ignorer
Using AI Code Generation
1 def initialize(app)2 def call(env)3 request_ignorer(env)4 @app.call(env)5 def request_ignorer(env)6 [404, {'Content-Type' => 'text/html'}, []]7 backports (3.6.8)8 builder (3.2.2)9 bundler (1.13.6)10 daemons (1.2.4)11 eventmachine (
request_ignorer
Using AI Code Generation
1 def initialize(app)2 def call(env)3 if request_ignorer(env)4 [200, {'Content-Type' => 'text/plain'}, ["The request is ignored"]]5 @app.call(env)6 def request_ignorer(env)7 def call(env)8 [200, {'Content-Type' => 'text/plain'}, ["Hello World!"]]9[2012-07-24 11:17:13] INFO ruby 1.9.3 (2012-04-20) [x86_64-linux]10Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)11 def initialize(app)12 def call(env)13 if request_ignorer(env)14 [200, {'Content-Type' => 'text/plain'}, ["The request is ignored"]]15 @app.call(env)16 def request_ignorer(env)
request_ignorer
Using AI Code Generation
1 def initialize(app)2 def call(env)3 @app.call(env)
request_ignorer
Using AI Code Generation
1 def initialize(app)2 def call(env)3 [404, {"Content-Type" => "text/html"}, ["Not Found"]]4 @app.call(env)5 @app.call(env)6 def call(env)7 [200, {"Content-Type" => "text/html"}, ["Hello World"]]8Rack::Server.start(9 :app => Middleware.new(App.new),
request_ignorer
Using AI Code Generation
1 def initialize(app)2 def call(env)3 if request_ignorer(env)4 [200, {'Content-Type' => 'text/html'}, ['OK']]5 @app.call(env)6 def request_ignorer(env)
request_ignorer
Using AI Code Generation
1 def initialize(app)2 def call(env)3 if request_ignorer(env)4 [200, {'Content-Type' => 'text/html'}, ['OK']]5 @app.call(env)6 def request_ignorer(env)
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!!