Search code examples
ruby-on-railsrspecrspec-railsvcr

After filtering sensitive data using VCR, re-running the spec fails with bad URI error


According to what I'm reading here: https://relishapp.com/vcr/vcr/v/1-11-3/docs/configuration/filter-sensitive-data,

When the interactions are replayed, the sensitive text will replace the substitution string so that the interaction will be identical to what was originally recorded.

My vcr_setup.rb file looks like this:

require 'vcr'

VCR.config do |c|
  c.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
  c.stub_with :fakeweb
  c.default_cassette_options = { :record => :once }

  c.filter_sensitive_data("<DOMAIN>") do |interaction|
     interaction.request.uri.split(":")[1][2..-1]
   end

   c.filter_sensitive_data("<PASSWORD>") do |interaction|
     interaction.request.uri.split(":")[2].split("@")[0]
   end
end

interaction.request.uri looks like this (with the sensitive info replaced):

"https://<my_secret_domain>:<my_secret_password>@services.someprovider.com:443/enterprise/composite/some_api_call"

After I run my rspec test the first time, the cassette correctly shows this:

uri: https://<DOMAIN>:<PASSWORD>@services.someprovider.com:443/enterprise/composite/some_api_call

However, when I try to run the test again, I get this error:

Failure/Error: VCR.use_cassette("#{provider_name}_mail_dispatcher_points_already_awarded", record: :all) do
 URI::InvalidURIError:
   bad URI(is not URI?): https://<DOMAIN>:<PASSWORD>@services.someprovider.com:443/enterprise/composite/some_api_call
 # ./spec/models/mail_dispatcher_spec.rb:130:in `block (3 levels) in <top (required)>'

This is contrary to the above paragraph from the documentation. What am I doing wrong?

Thanks in advance for any help.

Louise


Solution

  • The filter_sensitive_data is a wrapper for two methods: before_record and before_playback. I was able to use those methods to find and replace usernames and passwords in the interaction - the first before writing to the YAML file, and the second before playing a cassette back when re-running a test.

    This link: https://groups.google.com/forum/#!searchin/vcr-ruby/sensitive/vcr-ruby/uSm8HDBiWYw/OWIJk2_krVMJ, especially the comment from Myron Marston, provided a rough outline that I then modified to fit my specific API calls.