Read more

VCR: An OAuth-compatible request matcher

Dominik Schöler
April 27, 2016Software engineer at makandra GmbH

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.

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

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
Posted by Dominik Schöler to makandra dev (2016-04-27 11:10)