diff options
-rw-r--r-- | lib/net/http/response.rb | 5 | ||||
-rw-r--r-- | spec/ruby/library/net/http/http/get_spec.rb | 67 |
2 files changed, 70 insertions, 2 deletions
diff --git a/lib/net/http/response.rb b/lib/net/http/response.rb index 5a94f95694..08eaeb2cac 100644 --- a/lib/net/http/response.rb +++ b/lib/net/http/response.rb @@ -268,12 +268,13 @@ class Net::HTTPResponse begin yield inflate_body_io + success = true ensure - orig_err = $! begin inflate_body_io.finish rescue => err - raise orig_err || err + # Ignore #finish's error if there is an exception from yield + raise err if success end end when 'none', 'identity' then diff --git a/spec/ruby/library/net/http/http/get_spec.rb b/spec/ruby/library/net/http/http/get_spec.rb index 2d6e56242b..35bd51d9a3 100644 --- a/spec/ruby/library/net/http/http/get_spec.rb +++ b/spec/ruby/library/net/http/http/get_spec.rb @@ -24,3 +24,70 @@ describe "Net::HTTP.get" do end end end + +describe "Net::HTTP.get" do + describe "when reading gzipped contents" do + def start_threads + require 'zlib' + + server = nil + server_thread = Thread.new do + server = TCPServer.new("127.0.0.1", 0) + begin + c = server.accept + ensure + server.close + end + c.print "HTTP/1.1 200\r\n" + c.print "Content-Type: text/plain\r\n" + c.print "Content-Encoding: gzip\r\n" + s = StringIO.new + z = Zlib::GzipWriter.new(s) + begin + z.write 'Hello World!' + ensure + z.close + end + c.print "Content-Length: #{s.length}\r\n\r\n" + # Write partial gzip content + c.write s.string.byteslice(0..-2) + c.flush + c + end + Thread.pass until server && server_thread.stop? + + client_thread = Thread.new do + Thread.current.report_on_exception = false + Net::HTTP.get("127.0.0.1", '/', server.connect_address.ip_port) + end + Thread.pass until client_thread.stop? + + [server_thread, client_thread] + end + + it "propagates exceptions interrupting the thread and does not replace it with Zlib::BufError" do + my_exception = Class.new(RuntimeError) + server_thread, client_thread = start_threads + socket = server_thread.value + begin + client_thread.raise my_exception, "my exception" + -> { client_thread.value }.should raise_error(my_exception) + ensure + socket.close + end + end + + ruby_version_is "2.8" do # https://bugs.ruby-lang.org/issues/13882#note-6 + it "lets the kill Thread exception goes through and does not replace it with Zlib::BufError" do + server_thread, client_thread = start_threads + socket = server_thread.value + begin + client_thread.kill + client_thread.value.should == nil + ensure + socket.close + end + end + end + end +end |