Testing a web API using RSpec and VCR

2.8k views Asked by At

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.

1

There are 1 answers

0
Myron Marston On BEST ANSWER

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.