Project

General

Profile

Feature #5778 ยป httpresponse.patch

regularfry (Alex Young), 12/20/2011 02:02 AM

View differences:

lib/webrick/httpresponse.rb
attr_accessor :reason_phrase
##
# Body may be a String or IO subclass.
# Body may be a String or IO subclass, or anything which
# implements #read and #close methods to mimic an IO. If body
# implements a #bytesize method, that will be used to determine
# the Content-Length header.
attr_accessor :body
......
elsif %r{^multipart/byteranges} =~ @header['content-type']
@header.delete('content-length')
elsif @header['content-length'].nil?
unless @body.is_a?(IO)
if @body.respond_to?( :bytesize )
@header['content-length'] = @body ? @body.bytesize : 0
end
end
......
def send_body(socket)
case @body
when IO then send_body_io(socket)
else send_body_string(socket)
when String then send_body_string(socket)
else send_body_io(socket)
end
end
test/webrick/test_httpresponse.rb
require "webrick"
require "stringio"
require "minitest/autorun"
module WEBrick
......
def warn msg
@messages << msg
end
def error msg
fail msg
end
end
attr_reader :config, :logger, :res
......
assert_equal 0, logger.messages.length
end
def test_can_send_string_response
res.body = "tulips!"
socket = StringIO.new
res.send_response( socket )
assert_match /tulips!/, socket.string
end
def test_can_send_io_response
io_r, io_w = IO.pipe
io_w.write "beards!"
io_w.close_write
res.body = io_r
socket = StringIO.new
res.send_response( socket )
assert_match /beards!/, socket.string
end
def test_can_send_stringio_response
res.body = StringIO.new("octopodes!")
socket = StringIO.new
res.send_response( socket )
assert_match /octopodes!/, socket.string
end
def test_can_send_arbitrary_io_like_object
thing = Class.new do
def read(n); @sent = (@sent ? nil : "MIGHTY GIRAFFES"); end
def close; end
end.new
res.body = thing
socket = StringIO.new
res.send_response( socket )
assert_match /MIGHTY GIRAFFES/, socket.string
end
end
end
    (1-1/1)