Skip to content

Instantly share code, notes, and snippets.

@soupmatt
Forked from glaszig/vcr-webmock-2.rb
Last active May 10, 2018 20:45
Show Gist options
  • Save soupmatt/2a0d697c277e749a99cab12ec2e1520e to your computer and use it in GitHub Desktop.
Save soupmatt/2a0d697c277e749a99cab12ec2e1520e to your computer and use it in GitHub Desktop.
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.decode(uri.userinfo)).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