Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Net::HTTPResponse nil checking #71

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/net/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,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 @@ -504,7 +505,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
Original file line number Diff line number Diff line change
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