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 UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
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)