Read more

Configure how VCR matches requests to recorded cassettes

Henning Koch
October 17, 2016Software engineer at makandra GmbH

VCR lets you configure how it matches requests to recorded cassettes Show archive.org snapshot :

In order to properly replay previously recorded requests, VCR must match new
HTTP requests to a previously recorded one. By default, it matches on HTTP
method and URI, since that is usually deterministic and fully identifies the
resource and action for typical RESTful APIs.

You can customize how VCR matches requests using the :match_requests_on cassette option.

Illustration money motivation

Opscomplete powered by makandra brand

Save money by migrating from AWS to our fully managed hosting in Germany.

  • Trusted by over 100 customers
  • Ready to use with Ruby, Node.js, PHP
  • Proactive management by operations experts
Read more Show archive.org snapshot

There are number of predefined matching options like :uri or :path.

You can also go nuclear and provide a lambda to match requests and cassettes. Here is an example from the VCR docs that match requests by method and port:

port_matcher = lambda do |request_1, request_2|
  URI(request_1.uri).port == URI(request_2.uri).port
end

VCR.use_cassette('example', :match_requests_on => [:method, port_matcher]) do
  puts "Response for port 8000: " + response_body_for(:get, "http://example.com:8000/")
end
Posted by Henning Koch to makandra dev (2016-10-17 09:56)