Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing a web API using RSpec and VCR

Tags:

ruby

rspec

api

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.

like image 639
Andrew Stewart Avatar asked Jan 28 '12 03:01

Andrew Stewart


1 Answers

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.

like image 176
Myron Marston Avatar answered Oct 12 '22 12:10

Myron Marston