Skip to content

Instantly share code, notes, and snippets.

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
# usage:
# env CASSETTES_PATH=spec/fixtures/vcr_cassettes ruby vcr-webmock-2.rb
# should help resolve
require 'yaml'
require 'uri'
require 'base64'
puts "Directory not found: #{ENV['CASSETTES_PATH']}"
exit 1
Dir["#{ENV['CASSETTES_PATH']}/**/*"].each do |path|
converted = false
next if path
cassette = YAML.load 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 path, 'w' do |file|
file.write cassette.to_yaml
puts "Converted #{path}"
end if converted
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.