Search code examples
rubyapirspecvcr

Testing a web API using RSpec and VCR


I'm writing an API wrapper as a gem, and I want to test API responses using RSpec.

The problem with this is that all API requests are made using GET, and contain an API key in the url:

e.g. game/metadata/{api_key}

This presents problems for testing, as I don't want to keep the API key in the git repository history. Is there any way I can do these spec tests, preferably with RSpec/VCR, and not store the API key in version control?

I've tried using environment variables, but VCR still stores the entire request, not just the response body.


Solution

  • VCR has a configuration option specifically for cases like these:

    VCR.configure do |c|
      c.filter_sensitive_data("<API_KEY>") { MyAPIClient.api_key }
    end
    

    See https://www.relishapp.com/myronmarston/vcr/docs/configuration/filter-sensitive-data for a larger example.