Skip to content

Instantly share code, notes, and snippets.

@jrafanie
Forked from glaszig/vcr-webmock-2.rb
Created October 26, 2017 13:22
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 jrafanie/018a7024df387962a21ace09cc304665 to your computer and use it in GitHub Desktop.
Save jrafanie/018a7024df387962a21ace09cc304665 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.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