Skip to content

Instantly share code, notes, and snippets.

@lucasmazza
Created November 24, 2017 18:25
Show Gist options
  • Save lucasmazza/801e607d42a78253a85bc3e1bb5e5f2d to your computer and use it in GitHub Desktop.
Save lucasmazza/801e607d42a78253a85bc3e1bb5e5f2d to your computer and use it in GitHub Desktop.
# Rewrite VCR Cassettes to move the URL:PASSWORD pair from
# the 'uri' to the 'Authorization' header.
# This is required when moving from WebMock 1 to 2+.
require 'yaml'
require 'uri'
require 'base64'
Dir["spec/fixtures/vcr_cassettes/*.yml"].each do |path|
cassette = YAML.load_file(path)
cassette['http_interactions'].each do |interaction|
request = interaction['request']
uri = URI.parse(request['uri'])
next if uri.password.nil? && uri.user.nil?
credentials = Base64.encode64("#{uri.user}:#{uri.password}").chomp
uri.user = nil
uri.password = nil
unless request['headers'].key?('Authorization')
request['headers'].merge!('Authorization' => ["Basic #{credentials}"])
end
request['uri'] = uri.to_s
end
File.open(path, 'w') { |io| io.write(cassette.to_yaml) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment