VCR: An OAuth-compatible request matcher

Posted . Visible to the public.

OAuth Show archive.org snapshot requires a set of params to be carried along requests, among which a nonce Show archive.org snapshot . Some libraries pass these along as headers, some as query parameters. All fine.

When you're using VCR Show archive.org snapshot , the latter case is an issue. By default, requests are matched on method and URI. However, no request URI will equal another when they include a nonce. You won't be able to match these requests with VCR.

Solution

Obviously you need to ignore those variable OAuth params. To do so, add a custom request matcher:

# spec/support/vcr.rb or wherever you need it

VCR.configure do |config|
  # For matching requests that include OAuth params in the query string
  config.register_request_matcher(:oauth_uri) do |reqA, reqB|
    oauth = /oauth_(nonce|signature|timestamp)=.*?(&|$)/ # Variable params
    reqA.uri.gsub(oauth, '') == reqB.uri.gsub(oauth, '')
  end
end

Then use it like this:

VCR.use_cassette('<cassette name>', match_requests_on: %i[method oauth_uri]) do
  # Your test here
end
Profile picture of Dominik Schöler
Dominik Schöler
Last edit
Dominik Schöler
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2016-04-27 09:10)