Skip to content

Commit 46ca992

Browse files
authored
Merge pull request #71 from BrianHawley/fixes_70
Net::HTTPResponse nil checking
2 parents beb20c0 + 36f916a commit 46ca992

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/net/http/response.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ def read_body(dest = nil, &block)
366366
@body = nil
367367
end
368368
@read = true
369+
return if @body.nil?
369370

370371
case enc = @body_encoding
371372
when Encoding, false, nil
@@ -639,7 +640,7 @@ def read_chunked(dest, chunk_data_io) # :nodoc:
639640
end
640641

641642
def stream_check
642-
raise IOError, 'attempt to read body out of block' if @socket.closed?
643+
raise IOError, 'attempt to read body out of block' if @socket.nil? || @socket.closed?
643644
end
644645

645646
def procdest(dest, block)

test/net/http/test_httpresponse.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,41 @@ def test_read_body_string
589589
assert_equal 'hello', body
590590
end
591591

592+
def test_read_body_receiving_no_body
593+
io = dummy_io(<<EOS)
594+
HTTP/1.1 204 OK
595+
Connection: close
596+
597+
EOS
598+
599+
res = Net::HTTPResponse.read_new(io)
600+
res.body_encoding = 'utf-8'
601+
602+
body = 'something to override'
603+
604+
res.reading_body io, true do
605+
body = res.read_body
606+
end
607+
608+
assert_equal nil, body
609+
assert_equal nil, res.body
610+
end
611+
612+
def test_read_body_outside_of_reading_body
613+
io = dummy_io(<<EOS)
614+
HTTP/1.1 200 OK
615+
Connection: close
616+
Content-Length: 0
617+
618+
EOS
619+
620+
res = Net::HTTPResponse.read_new(io)
621+
622+
assert_raise IOError do
623+
res.read_body
624+
end
625+
end
626+
592627
def test_uri_equals
593628
uri = URI 'http://example'
594629

0 commit comments

Comments
 (0)