Skip to content

Commit

Permalink
[ruby/net-http] Net::HTTPResponse nil checking
Browse files Browse the repository at this point in the history
Fix nil handling in read_body and stream_check.

Fixes: #70

ruby/net-http@36f916ac18
  • Loading branch information
BrianHawley authored and matzbot committed Oct 5, 2023
1 parent d088b9f commit 9d58f93
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/net/http/response.rb
Expand Up @@ -366,6 +366,7 @@ def read_body(dest = nil, &block)
@body = nil
end
@read = true
return if @body.nil?

case enc = @body_encoding
when Encoding, false, nil
Expand Down Expand Up @@ -639,7 +640,7 @@ def read_chunked(dest, chunk_data_io) # :nodoc:
end

def stream_check
raise IOError, 'attempt to read body out of block' if @socket.closed?
raise IOError, 'attempt to read body out of block' if @socket.nil? || @socket.closed?
end

def procdest(dest, block)
Expand Down
35 changes: 35 additions & 0 deletions test/net/http/test_httpresponse.rb
Expand Up @@ -589,6 +589,41 @@ def test_read_body_string
assert_equal 'hello', body
end

def test_read_body_receiving_no_body
io = dummy_io(<<EOS)
HTTP/1.1 204 OK
Connection: close
EOS

res = Net::HTTPResponse.read_new(io)
res.body_encoding = 'utf-8'

body = 'something to override'

res.reading_body io, true do
body = res.read_body
end

assert_equal nil, body
assert_equal nil, res.body
end

def test_read_body_outside_of_reading_body
io = dummy_io(<<EOS)
HTTP/1.1 200 OK
Connection: close
Content-Length: 0
EOS

res = Net::HTTPResponse.read_new(io)

assert_raise IOError do
res.read_body
end
end

def test_uri_equals
uri = URI 'http://example'

Expand Down

0 comments on commit 9d58f93

Please sign in to comment.