Question

On all specs with requests to example.com I want to ignore the trailing id in the URI with respect the request matcher. Something like this.

VCR.configure do |c|
  # omitted
  c.register_request_matcher :uri_ignoring_trailing_id do |request_1, request_2|
    # omitted
  end

  c.before_http_request(lambda { |req| req.uri =~ /example.com/ }) do
    c.default_cassette_options = { match_requests_on: [ :uri_ignoring_trailing_id ] }
  end

end
Was it helpful?

Solution

Modifying the global configuration in before_http_request is a bad idea because it'll affect every request made after you change the config, not just the ones matching example.com. Here's how I would recommend you do this instead:

VCR.configure do |vcr|
  uri_matcher = VCR.request_matchers[:uri]
  vcr.register_request_matcher(:uri_ignoring_trailing_id_for_example_dot_com) do |req_1, req_2|
    if req_1.parsed_uri.host == "example.com" && req_2.parsed_uri.host == "example.com"
      # do your custom matching where you ignore trailing id
    else
      uri_matcher.matches?(req_1, req_2)
    end
  end

  vcr.default_cassette_options = { match_requests_on: [:method, :uri_ignoring_trailing_id_for_example_dot_com] }
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top