2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2011-12-13 02:15:31 +04:00
|
|
|
require "webrick"
|
|
|
|
require "minitest/autorun"
|
2013-08-07 22:38:39 +04:00
|
|
|
require "stringio"
|
2011-12-13 02:15:31 +04:00
|
|
|
|
|
|
|
module WEBrick
|
|
|
|
class TestHTTPResponse < MiniTest::Unit::TestCase
|
|
|
|
class FakeLogger
|
|
|
|
attr_reader :messages
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@messages = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def warn msg
|
|
|
|
@messages << msg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-13 02:33:56 +04:00
|
|
|
attr_reader :config, :logger, :res
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
@logger = FakeLogger.new
|
|
|
|
@config = Config::HTTP
|
|
|
|
@config[:Logger] = logger
|
|
|
|
@res = HTTPResponse.new config
|
|
|
|
@res.keep_alive = true
|
|
|
|
end
|
2011-12-13 02:15:31 +04:00
|
|
|
|
2011-12-13 02:33:56 +04:00
|
|
|
def test_304_does_not_log_warning
|
2011-12-13 02:15:31 +04:00
|
|
|
res.status = 304
|
|
|
|
res.setup_header
|
|
|
|
assert_equal 0, logger.messages.length
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_204_does_not_log_warning
|
|
|
|
res.status = 204
|
2011-12-13 02:33:56 +04:00
|
|
|
res.setup_header
|
|
|
|
|
|
|
|
assert_equal 0, logger.messages.length
|
|
|
|
end
|
2011-12-13 02:15:31 +04:00
|
|
|
|
2011-12-13 02:33:56 +04:00
|
|
|
def test_1xx_does_not_log_warnings
|
|
|
|
res.status = 105
|
2011-12-13 02:15:31 +04:00
|
|
|
res.setup_header
|
|
|
|
|
|
|
|
assert_equal 0, logger.messages.length
|
|
|
|
end
|
2013-08-07 22:38:39 +04:00
|
|
|
|
|
|
|
def test_send_body_io
|
2014-05-30 18:50:42 +04:00
|
|
|
IO.pipe {|body_r, body_w|
|
|
|
|
body_w.write 'hello'
|
|
|
|
body_w.close
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
@res.body = body_r
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
IO.pipe {|r, w|
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
@res.send_body w
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
w.close
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
assert_equal 'hello', r.read
|
|
|
|
}
|
|
|
|
}
|
2014-11-08 18:38:33 +03:00
|
|
|
assert_equal 0, logger.messages.length
|
2013-08-07 22:38:39 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_body_string
|
|
|
|
@res.body = 'hello'
|
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
IO.pipe {|r, w|
|
|
|
|
@res.send_body w
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
w.close
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
assert_equal 'hello', r.read
|
|
|
|
}
|
2014-11-08 18:38:33 +03:00
|
|
|
assert_equal 0, logger.messages.length
|
2013-08-07 22:38:39 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_body_string_io
|
|
|
|
@res.body = StringIO.new 'hello'
|
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
IO.pipe {|r, w|
|
|
|
|
@res.send_body w
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
w.close
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
assert_equal 'hello', r.read
|
|
|
|
}
|
2014-11-08 18:38:33 +03:00
|
|
|
assert_equal 0, logger.messages.length
|
2013-08-07 22:38:39 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_body_io_chunked
|
|
|
|
@res.chunked = true
|
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
IO.pipe {|body_r, body_w|
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
body_w.write 'hello'
|
|
|
|
body_w.close
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
@res.body = body_r
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
IO.pipe {|r, w|
|
|
|
|
@res.send_body w
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
w.close
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
r.binmode
|
|
|
|
assert_equal "5\r\nhello\r\n0\r\n\r\n", r.read
|
|
|
|
}
|
|
|
|
}
|
2014-11-08 18:38:33 +03:00
|
|
|
assert_equal 0, logger.messages.length
|
2013-08-07 22:38:39 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_body_string_chunked
|
|
|
|
@res.chunked = true
|
|
|
|
|
|
|
|
@res.body = 'hello'
|
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
IO.pipe {|r, w|
|
|
|
|
@res.send_body w
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
w.close
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
r.binmode
|
|
|
|
assert_equal "5\r\nhello\r\n0\r\n\r\n", r.read
|
|
|
|
}
|
2014-11-08 18:38:33 +03:00
|
|
|
assert_equal 0, logger.messages.length
|
2013-08-07 22:38:39 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_body_string_io_chunked
|
|
|
|
@res.chunked = true
|
|
|
|
|
|
|
|
@res.body = StringIO.new 'hello'
|
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
IO.pipe {|r, w|
|
|
|
|
@res.send_body w
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
w.close
|
2013-08-07 22:38:39 +04:00
|
|
|
|
2014-05-30 18:50:42 +04:00
|
|
|
r.binmode
|
|
|
|
assert_equal "5\r\nhello\r\n0\r\n\r\n", r.read
|
|
|
|
}
|
2014-11-08 18:38:33 +03:00
|
|
|
assert_equal 0, logger.messages.length
|
2013-08-07 22:38:39 +04:00
|
|
|
end
|
2016-03-29 06:45:32 +03:00
|
|
|
|
2017-10-31 02:56:44 +03:00
|
|
|
def test_send_body_proc
|
|
|
|
@res.body = Proc.new { |out| out.write('hello') }
|
|
|
|
IO.pipe do |r, w|
|
|
|
|
@res.send_body(w)
|
|
|
|
w.close
|
|
|
|
r.binmode
|
|
|
|
assert_equal 'hello', r.read
|
|
|
|
end
|
|
|
|
assert_equal 0, logger.messages.length
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_body_proc_chunked
|
|
|
|
@res.body = Proc.new { |out| out.write('hello') }
|
|
|
|
@res.chunked = true
|
|
|
|
IO.pipe do |r, w|
|
|
|
|
@res.send_body(w)
|
|
|
|
w.close
|
|
|
|
r.binmode
|
|
|
|
assert_equal "5\r\nhello\r\n0\r\n\r\n", r.read
|
|
|
|
end
|
|
|
|
assert_equal 0, logger.messages.length
|
|
|
|
end
|
|
|
|
|
2016-03-29 06:45:32 +03:00
|
|
|
def test_set_error
|
|
|
|
status = 400
|
|
|
|
message = 'missing attribute'
|
|
|
|
@res.status = status
|
|
|
|
error = WEBrick::HTTPStatus[status].new(message)
|
|
|
|
body = @res.set_error(error)
|
|
|
|
assert_match(/#{@res.reason_phrase}/, body)
|
|
|
|
assert_match(/#{message}/, body)
|
|
|
|
end
|
2011-12-13 02:15:31 +04:00
|
|
|
end
|
|
|
|
end
|