Configure how VCR matches requests to recorded cassettes

Updated . Posted . Visible to the public.

VCR lets you configure how it matches requests to recorded cassettes:

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.

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
Henning Koch
Last edit
Jonas Schiele
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2016-10-17 07:56)