Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
converts vcr cassettes to webmock 2.0-compatible format
# converts URIs in vcr cassettes from uri-based basic auth
# to header-based basic auth to be compatible with webmock 2.0.
# it will create a basic auth header with an ERB tag
# to keep user and password be editable.
#
# Authorization: Basic <%= Base64.encode64("user:password").chomp %>
#
# may not work if using VCR's filter_sensitive_data.
# in that case use https://gist.github.com/ujh/594c99385b6cbe92e32b1bbfa8578a45
#
# usage:
#
# env CASSETTES_PATH=spec/fixtures/vcr_cassettes ruby vcr-webmock-2.rb
#
# should help resolve https://github.com/vcr/vcr/issues/570
require 'yaml'
require 'uri'
require 'base64'
unless File.directory? ENV['CASSETTES_PATH']
puts "Directory not found: #{ENV['CASSETTES_PATH']}"
exit 1
end
Dir["#{ENV['CASSETTES_PATH']}/**/*"].each do |path|
converted = false
next if File.directory? path
cassette = YAML.load File.read path
cassette['http_interactions'].each do |http_interaction|
request = http_interaction['request']
uri = URI.parse request['uri']
next if uri.userinfo.nil?
converted = true
erb = "Basic <%= Base64.encode64(#{uri.userinfo.inspect}).chomp %>"
request['headers']['Authorization'] = erb
uri.userinfo = ''
request['uri'] = uri.to_s
http_interaction['request'] = request
end
File.open path, 'w' do |file|
file.write cassette.to_yaml
puts "Converted #{path}"
end if converted
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.