Created
April 27, 2016 07:51
-
-
Save ujh/594c99385b6cbe92e32b1bbfa8578a45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# Convert VCR cassettes that use basic auth to work with Webmock 2. Takes the folder where the VCR | |
#cassettes are stored as an argument. | |
require 'yaml' | |
require 'base64' | |
FOLDER = File.expand_path(ARGV[0]) | |
FILES = Dir["#{File.join(FOLDER, '**', '*.yml')}"] | |
def transform_request!(request) | |
uri = request['request']['uri'] | |
# Don't use the URI class as that only works with well formed URIs. It's not guaranteed that we | |
# have well formed URIs however as we can replace any part of the cassette using the VCR | |
# filter_sensitive_data feature. | |
return unless uri =~ %r{//([^@]+)@} | |
userinfo = $1 | |
base64userinfo = Base64.encode64(userinfo) | |
uri = uri.gsub("#{userinfo}@", '') | |
request['request']['uri'] = uri | |
request['request']['headers']['Authorization'] = "Basic #{base64userinfo}" | |
end | |
def transform!(path) | |
print "." | |
yaml = YAML.load_file(path) | |
yaml['http_interactions'].each do |request| | |
transform_request!(request) | |
end | |
File.open(path, 'w') do |f| | |
YAML.dump(yaml, f) | |
end | |
end | |
FILES.each do |file| | |
transform!(file) | |
end |
Thanks! Saved me a lot of time!
I had to make some minor changes to get it to work: https://gist.github.com/betesh/c4e19eedbdcccca5915b1f7de60f2637
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great script! Thanks for putting it out there, really solved some issues when upgrading to Ruby 2.4 and WebMock needed to get upgraded, as well.
We ran into an issue where the auth had trailing whitespace, and it was line 19 we had to change.
From
base64userinfo = Base64.encode64(userinfo)
tobase64userinfo = Base64.encode64(userinfo).chomp
Thanks again!
💖