9

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.

Myron Marston
  • 21,452
  • 5
  • 64
  • 63
Andrew Stewart
  • 710
  • 1
  • 6
  • 10

1 Answers1

21

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.

Myron Marston
  • 21,452
  • 5
  • 64
  • 63