根据请求URI在VCR中设置默认选项

时间:2014-02-28 20:21:13

标签: rspec vcr

在对example.com的请求的所有规范中,我想忽略URI中关于请求匹配器的尾随id。这样的事情。

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

1 个答案:

答案 0 :(得分:6)

修改before_http_request中的全局配置是个坏主意,因为它会影响更改配置后发出的每个请求,而不仅仅是匹配example.com的请求。以下是我建议你这样做的方法:

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