Skip to content

Instantly share code, notes, and snippets.

@english
Created February 2, 2017 19:58
Show Gist options
  • Save english/0fcdaa9be05c88e35feb4ed8197ba07e to your computer and use it in GitHub Desktop.
Save english/0fcdaa9be05c88e35feb4ed8197ba07e to your computer and use it in GitHub Desktop.
Reproduction of "ArgumentError: invalid byte sequence in UTF-8" webmock bug
#!/usr/bin/env ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'webmock', '1.24.6'
end
include WebMock::API
stub_http_request(:get, /foo/).to_return(body: "abc")
begin
Net::HTTP.start("example.com", 9000, use_ssl: true) do |http|
request = Net::HTTP::Get.new('/foo')
request['Authorization'] = 'Basic foo'
http.request(request)
end
rescue => e
puts "ruby: #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} failed: #{e.inspect}"
else
puts "ruby: #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} passed"
end
@english
Copy link
Author

english commented Feb 2, 2017

Running the above across various Ruby (MRI) versions:

$ rvm 2.0.0-p247,2.0.0-p353,2.1.0,2.1.5,2.2.3,2.2.4 do ./webmock-bug.rb
ruby: 2.0.0-p247 passed
ruby: 2.0.0-p353 passed
ruby: 2.1.0-p0 failed: #<ArgumentError: invalid byte sequence in UTF-8>
ruby: 2.1.5-p273 failed: #<ArgumentError: invalid byte sequence in UTF-8>
ruby: 2.2.3-p173 failed: #<ArgumentError: invalid byte sequence in UTF-8>
ruby: 2.2.4-p230 failed: #<ArgumentError: invalid byte sequence in UTF-8>

The full backtrace for the above exceptions look like:

/home/jamie/.rvm/gems/ruby-2.1.5/gems/webmock-1.24.6/lib/webmock/util/uri.rb:54:in `gsub'
/home/jamie/.rvm/gems/ruby-2.1.5/gems/webmock-1.24.6/lib/webmock/util/uri.rb:54:in `block in variations_of_uri_as_strings'
/home/jamie/.rvm/gems/ruby-2.1.5/gems/webmock-1.24.6/lib/webmock/util/uri.rb:54:in `map'
/home/jamie/.rvm/gems/ruby-2.1.5/gems/webmock-1.24.6/lib/webmock/util/uri.rb:54:in `variations_of_uri_as_strings'
/home/jamie/.rvm/gems/ruby-2.1.5/gems/webmock-1.24.6/lib/webmock/request_pattern.rb:121:in `matches?'
/home/jamie/.rvm/gems/ruby-2.1.5/gems/webmock-1.24.6/lib/webmock/request_pattern.rb:33:in `matches?'
/home/jamie/.rvm/gems/ruby-2.1.5/gems/webmock-1.24.6/lib/webmock/stub_registry.rb:58:in `block in request_stub_for'
/home/jamie/.rvm/gems/ruby-2.1.5/gems/webmock-1.24.6/lib/webmock/stub_registry.rb:57:in `each'
/home/jamie/.rvm/gems/ruby-2.1.5/gems/webmock-1.24.6/lib/webmock/stub_registry.rb:57:in `detect'
/home/jamie/.rvm/gems/ruby-2.1.5/gems/webmock-1.24.6/lib/webmock/stub_registry.rb:57:in `request_stub_for'
/home/jamie/.rvm/gems/ruby-2.1.5/gems/webmock-1.24.6/lib/webmock/stub_registry.rb:50:in `response_for_request'
/home/jamie/.rvm/gems/ruby-2.1.5/gems/webmock-1.24.6/lib/webmock/http_lib_adapters/net_http.rb:79:in `request'
./bug.rb:17:in `block in <main>'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment