Skip to content

Instantly share code, notes, and snippets.

@reagent
Created August 8, 2019 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reagent/46cb4bc4fc2ed1e5e38b04a7e08cb783 to your computer and use it in GitHub Desktop.
Save reagent/46cb4bc4fc2ed1e5e38b04a7e08cb783 to your computer and use it in GitHub Desktop.
Example of stripping credentials / sensitive data from GET params when using VCR (5.0.x)
class URLSanitizer
def self.equal?(first, second)
new(first) == new(second)
end
def self.sanitize(url)
new(url).to_s
end
def initialize(url)
@url = url
end
def ==(other)
hash == other.hash
end
def hash
[self.class, uri.scheme, uri.host, uri.path, params].hash
end
def params
parsed_params.reject {|k| k == "token" }
end
def to_s
uri.dup.tap {|uri| uri.query = params.to_query if params.any? }.to_s
end
private
def parsed_params
Rack::Utils.parse_query(uri.query)
end
def uri
@uri ||= URI(@url)
end
end
VCR.configure do |config|
matcher = lambda do |one, two|
one.method == two.method && URLSanitizer.equal?(one.uri, two.uri)
end
config.default_cassette_options = {
match_requests_on: [matcher]
}
config.before_record do |interaction|
interaction.request.uri = URLSanitizer.sanitize(interaction.request.uri)
end
config.cassette_library_dir = "spec/integration/fixtures"
config.hook_into :webmock
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment