Skip to content

Commit

Permalink
Merge branch 'update-bufferedio-method-signature-for-24-compat'
Browse files Browse the repository at this point in the history
* update-bufferedio-method-signature-for-24-compat:
  Update Net::BufferedIO patch for Ruby 2.4-compat
  Extract-method refactor

Refernces #57 and #60.
  • Loading branch information
chrisk committed Jul 9, 2017
2 parents 1311117 + 5bd70b7 commit 34d018a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
20 changes: 5 additions & 15 deletions lib/fake_web/ext/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,12 @@
module Net #:nodoc: all

class BufferedIO
def initialize_with_fakeweb(*args)
initialize_without_fakeweb(*args)

case @io
when Socket, OpenSSL::SSL::SSLSocket, StringIO, IO
# usable as-is
when String
if !@io.include?("\0") && File.exist?(@io) && !File.directory?(@io)
@io = File.open(@io, "r")
else
@io = StringIO.new(@io)
end
else
raise ArgumentError, "Unable to create fake socket from #{io}"
eval <<-RUBY
def initialize_with_fakeweb(*args#{", **opts" if RUBY_VERSION >= "2.4.0" })
initialize_without_fakeweb(*args#{", **opts" if RUBY_VERSION >= "2.4.0" })
@io = FakeWeb::Utility.io_from_fake_response_object(@io)
end
end
RUBY
alias_method :initialize_without_fakeweb, :initialize
alias_method :initialize, :initialize_with_fakeweb
end
Expand Down
15 changes: 15 additions & 0 deletions lib/fake_web/utility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ def self.produce_side_effects_of_net_http_request(request, body)
request.content_length = request.body.length unless request.body.nil?
end

def self.io_from_fake_response_object(obj)
case obj
when Socket, OpenSSL::SSL::SSLSocket, StringIO, IO
obj # usable as-is
when String
if !obj.include?("\0") && File.exist?(obj) && !File.directory?(obj)
File.open(obj, "r")
else
StringIO.new(obj)
end
else
raise ArgumentError, "Unable to create fake socket from #{obj}"
end
end

def self.puts_warning_for_net_http_around_advice_libs_if_needed
libs = {"Samuel" => defined?(Samuel)}
warnings = libs.select { |_, loaded| loaded }.map do |name, _|
Expand Down
26 changes: 26 additions & 0 deletions test/test_registering_with_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,30 @@ def test_creating_net_buffered_io_directly_with_an_unsupported_underlying_object
Net::BufferedIO.new(unsupported)
end
end

def test_creating_net_buffered_io_with_ruby_24_method_signature
# These keyword arguments were added to BufferedIO.new's params in Ruby 2.4
call_with_keyword_args = lambda do
eval <<-RUBY
Net::BufferedIO.new("", read_timeout: 1, continue_timeout: 1, debug_output: nil)
RUBY
end

if RUBY_VERSION >= "2.4.0"
# Should not raise
call_with_keyword_args.call
elsif RUBY_VERSION >= "2.0.0"
# From Ruby 2.0 to 2.3, keyword arguments are generally valid syntax, but
# had not been added to BufferedIO.new's method signature
assert_raises(ArgumentError) { call_with_keyword_args.call }
elsif RUBY_VERSION >= "1.9.0"
# Ruby 1.9 will interpret the arguments as a new-style options hash,
# which is also not in the method signature
assert_raises(ArgumentError) { call_with_keyword_args.call }
else
# Ruby 1.8 won't know how to parse this, since it had neither new-style
# hashes nor keyword arguments
assert_raises(SyntaxError) { call_with_keyword_args.call }
end
end
end

0 comments on commit 34d018a

Please sign in to comment.