2012-11-05 19:49:57 -05:00
|
|
|
# coding: US-ASCII
|
2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2005-09-23 22:45:02 -04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'net/http'
|
|
|
|
require 'stringio'
|
2011-01-05 19:39:50 -05:00
|
|
|
require_relative 'utils'
|
2005-09-23 22:45:02 -04:00
|
|
|
|
2012-07-19 21:56:21 -04:00
|
|
|
class TestNetHTTP < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def test_class_Proxy
|
|
|
|
no_proxy_class = Net::HTTP.Proxy nil
|
|
|
|
|
|
|
|
assert_equal Net::HTTP, no_proxy_class
|
|
|
|
|
|
|
|
proxy_class = Net::HTTP.Proxy 'proxy.example', 8000, 'user', 'pass'
|
|
|
|
|
2016-02-19 02:45:58 -05:00
|
|
|
assert_not_equal Net::HTTP, proxy_class
|
2012-07-19 21:56:21 -04:00
|
|
|
|
|
|
|
assert_operator proxy_class, :<, Net::HTTP
|
|
|
|
|
|
|
|
assert_equal 'proxy.example', proxy_class.proxy_address
|
|
|
|
assert_equal 8000, proxy_class.proxy_port
|
|
|
|
assert_equal 'user', proxy_class.proxy_user
|
|
|
|
assert_equal 'pass', proxy_class.proxy_pass
|
|
|
|
|
2015-12-09 16:54:21 -05:00
|
|
|
http = proxy_class.new 'hostname.example'
|
2012-07-19 21:56:21 -04:00
|
|
|
|
2016-02-19 02:45:58 -05:00
|
|
|
assert_not_predicate http, :proxy_from_env?
|
2012-07-21 11:45:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
proxy_class = Net::HTTP.Proxy 'proxy.example'
|
|
|
|
assert_equal 'proxy.example', proxy_class.proxy_address
|
|
|
|
assert_equal 80, proxy_class.proxy_port
|
2012-07-19 21:56:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_class_Proxy_from_ENV
|
|
|
|
clean_http_proxy_env do
|
|
|
|
ENV['http_proxy'] = 'http://proxy.example:8000'
|
|
|
|
|
|
|
|
# These are ignored on purpose. See Bug 4388 and Feature 6546
|
|
|
|
ENV['http_proxy_user'] = 'user'
|
|
|
|
ENV['http_proxy_pass'] = 'pass'
|
|
|
|
|
|
|
|
proxy_class = Net::HTTP.Proxy :ENV
|
|
|
|
|
2016-02-19 02:45:58 -05:00
|
|
|
assert_not_equal Net::HTTP, proxy_class
|
2012-07-19 21:56:21 -04:00
|
|
|
|
|
|
|
assert_operator proxy_class, :<, Net::HTTP
|
|
|
|
|
|
|
|
assert_nil proxy_class.proxy_address
|
|
|
|
assert_nil proxy_class.proxy_user
|
|
|
|
assert_nil proxy_class.proxy_pass
|
|
|
|
|
2016-02-19 02:45:58 -05:00
|
|
|
assert_not_equal 8000, proxy_class.proxy_port
|
2012-07-19 21:56:21 -04:00
|
|
|
|
2015-12-09 16:54:21 -05:00
|
|
|
http = proxy_class.new 'hostname.example'
|
2012-07-19 21:56:21 -04:00
|
|
|
|
|
|
|
assert http.proxy_from_env?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_edit_path
|
2015-12-09 16:54:21 -05:00
|
|
|
http = Net::HTTP.new 'hostname.example', nil, nil
|
2012-07-19 21:56:21 -04:00
|
|
|
|
|
|
|
edited = http.send :edit_path, '/path'
|
|
|
|
|
|
|
|
assert_equal '/path', edited
|
|
|
|
|
|
|
|
http.use_ssl = true
|
|
|
|
|
|
|
|
edited = http.send :edit_path, '/path'
|
|
|
|
|
|
|
|
assert_equal '/path', edited
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_edit_path_proxy
|
2015-12-09 16:54:21 -05:00
|
|
|
http = Net::HTTP.new 'hostname.example', nil, 'proxy.example'
|
2012-07-19 21:56:21 -04:00
|
|
|
|
|
|
|
edited = http.send :edit_path, '/path'
|
|
|
|
|
2015-12-09 16:54:21 -05:00
|
|
|
assert_equal 'http://hostname.example/path', edited
|
2012-07-19 21:56:21 -04:00
|
|
|
|
|
|
|
http.use_ssl = true
|
|
|
|
|
|
|
|
edited = http.send :edit_path, '/path'
|
|
|
|
|
|
|
|
assert_equal '/path', edited
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_proxy_address
|
2012-09-29 07:19:06 -04:00
|
|
|
clean_http_proxy_env do
|
2015-12-09 16:54:21 -05:00
|
|
|
http = Net::HTTP.new 'hostname.example', nil, 'proxy.example'
|
2012-09-29 07:19:06 -04:00
|
|
|
assert_equal 'proxy.example', http.proxy_address
|
2012-07-21 11:45:10 -04:00
|
|
|
|
2015-12-09 16:54:21 -05:00
|
|
|
http = Net::HTTP.new 'hostname.example', nil
|
2012-09-29 07:19:06 -04:00
|
|
|
assert_equal nil, http.proxy_address
|
|
|
|
end
|
2012-07-19 21:56:21 -04:00
|
|
|
end
|
|
|
|
|
2015-12-09 14:41:47 -05:00
|
|
|
def test_proxy_from_env_ENV
|
|
|
|
clean_http_proxy_env do
|
|
|
|
ENV['http_proxy'] = 'http://proxy.example:8000'
|
|
|
|
|
|
|
|
assert_equal false, Net::HTTP.proxy_class?
|
2015-12-09 16:54:21 -05:00
|
|
|
http = Net::HTTP.new 'hostname.example'
|
2015-12-09 14:41:47 -05:00
|
|
|
|
|
|
|
assert_equal true, http.proxy_from_env?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-19 21:56:21 -04:00
|
|
|
def test_proxy_address_ENV
|
|
|
|
clean_http_proxy_env do
|
|
|
|
ENV['http_proxy'] = 'http://proxy.example:8000'
|
|
|
|
|
2015-12-09 16:54:21 -05:00
|
|
|
http = Net::HTTP.new 'hostname.example'
|
2012-07-19 21:56:21 -04:00
|
|
|
|
|
|
|
assert_equal 'proxy.example', http.proxy_address
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_proxy_eh_no_proxy
|
|
|
|
clean_http_proxy_env do
|
2015-12-09 16:54:21 -05:00
|
|
|
assert_equal false, Net::HTTP.new('hostname.example', nil, nil).proxy?
|
2012-07-19 21:56:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_proxy_eh_ENV
|
|
|
|
clean_http_proxy_env do
|
|
|
|
ENV['http_proxy'] = 'http://proxy.example:8000'
|
|
|
|
|
2015-12-09 16:54:21 -05:00
|
|
|
http = Net::HTTP.new 'hostname.example'
|
2012-07-19 21:56:21 -04:00
|
|
|
|
2012-07-21 11:45:10 -04:00
|
|
|
assert_equal true, http.proxy?
|
2012-07-19 21:56:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_proxy_eh_ENV_none_set
|
|
|
|
clean_http_proxy_env do
|
2015-12-09 16:54:21 -05:00
|
|
|
assert_equal false, Net::HTTP.new('hostname.example').proxy?
|
2012-07-19 21:56:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_proxy_eh_ENV_no_proxy
|
|
|
|
clean_http_proxy_env do
|
|
|
|
ENV['http_proxy'] = 'http://proxy.example:8000'
|
2015-12-09 16:54:21 -05:00
|
|
|
ENV['no_proxy'] = 'hostname.example'
|
2012-07-19 21:56:21 -04:00
|
|
|
|
2015-12-09 16:54:21 -05:00
|
|
|
assert_equal false, Net::HTTP.new('hostname.example').proxy?
|
2012-07-19 21:56:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_proxy_port
|
2012-09-29 07:19:06 -04:00
|
|
|
clean_http_proxy_env do
|
2015-12-24 18:23:46 -05:00
|
|
|
http = Net::HTTP.new 'example', nil, 'proxy.example'
|
2012-09-29 07:19:06 -04:00
|
|
|
assert_equal 'proxy.example', http.proxy_address
|
|
|
|
assert_equal 80, http.proxy_port
|
2015-12-24 18:23:46 -05:00
|
|
|
http = Net::HTTP.new 'example', nil, 'proxy.example', 8000
|
2012-09-29 07:19:06 -04:00
|
|
|
assert_equal 8000, http.proxy_port
|
2015-12-24 18:23:46 -05:00
|
|
|
http = Net::HTTP.new 'example', nil
|
2012-09-29 07:19:06 -04:00
|
|
|
assert_equal nil, http.proxy_port
|
|
|
|
end
|
2012-07-19 21:56:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_proxy_port_ENV
|
|
|
|
clean_http_proxy_env do
|
|
|
|
ENV['http_proxy'] = 'http://proxy.example:8000'
|
|
|
|
|
2015-12-09 16:54:21 -05:00
|
|
|
http = Net::HTTP.new 'hostname.example'
|
2012-07-19 21:56:21 -04:00
|
|
|
|
|
|
|
assert_equal 8000, http.proxy_port
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-21 11:45:10 -04:00
|
|
|
def test_newobj
|
|
|
|
clean_http_proxy_env do
|
|
|
|
ENV['http_proxy'] = 'http://proxy.example:8000'
|
|
|
|
|
2015-12-09 16:54:21 -05:00
|
|
|
http = Net::HTTP.newobj 'hostname.example'
|
2012-07-21 11:45:10 -04:00
|
|
|
|
|
|
|
assert_equal false, http.proxy?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-19 21:56:21 -04:00
|
|
|
def clean_http_proxy_env
|
|
|
|
orig = {
|
|
|
|
'http_proxy' => ENV['http_proxy'],
|
|
|
|
'http_proxy_user' => ENV['http_proxy_user'],
|
|
|
|
'http_proxy_pass' => ENV['http_proxy_pass'],
|
|
|
|
'no_proxy' => ENV['no_proxy'],
|
|
|
|
}
|
|
|
|
|
|
|
|
orig.each_key do |key|
|
|
|
|
ENV.delete key
|
|
|
|
end
|
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
orig.each do |key, value|
|
|
|
|
ENV[key] = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-02 20:38:59 -05:00
|
|
|
def test_failure_message_includes_failed_domain_and_port
|
2015-01-13 00:16:37 -05:00
|
|
|
# hostname to be included in the error message
|
|
|
|
host = Struct.new(:to_s).new("<example>")
|
|
|
|
port = 2119
|
|
|
|
# hack to let TCPSocket.open fail
|
|
|
|
def host.to_str; raise SocketError, "open failure"; end
|
|
|
|
uri = Struct.new(:scheme, :hostname, :port).new("http", host, port)
|
|
|
|
assert_raise_with_message(SocketError, /#{host}:#{port}/) do
|
|
|
|
Net::HTTP.get(uri)
|
2015-01-02 20:38:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-19 21:56:21 -04:00
|
|
|
end
|
|
|
|
|
2005-09-23 22:45:02 -04:00
|
|
|
module TestNetHTTP_version_1_1_methods
|
|
|
|
|
|
|
|
def test_s_get
|
|
|
|
assert_equal $test_net_http_data,
|
|
|
|
Net::HTTP.get(config('host'), '/', config('port'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_head
|
|
|
|
start {|http|
|
|
|
|
res = http.head('/')
|
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
assert_equal $test_net_http_data_type, res['Content-Type']
|
2010-05-03 19:42:26 -04:00
|
|
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
|
|
|
assert_equal $test_net_http_data.size, res['Content-Length'].to_i
|
|
|
|
end
|
2005-09-23 22:45:02 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_get
|
|
|
|
start {|http|
|
|
|
|
_test_get__get http
|
|
|
|
_test_get__iter http
|
|
|
|
_test_get__chunked http
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def _test_get__get(http)
|
2010-12-09 20:08:45 -05:00
|
|
|
res = http.get('/')
|
2005-09-23 22:45:02 -04:00
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
assert_kind_of String, res.body
|
2010-05-03 19:42:26 -04:00
|
|
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
|
|
|
assert_not_nil res['content-length']
|
|
|
|
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
|
|
|
end
|
2005-09-23 22:45:02 -04:00
|
|
|
assert_equal $test_net_http_data_type, res['Content-Type']
|
|
|
|
assert_equal $test_net_http_data.size, res.body.size
|
|
|
|
assert_equal $test_net_http_data, res.body
|
2011-01-04 23:58:57 -05:00
|
|
|
|
|
|
|
assert_nothing_raised {
|
2011-12-22 04:48:50 -05:00
|
|
|
http.get('/', { 'User-Agent' => 'test' }.freeze)
|
2011-01-04 23:58:57 -05:00
|
|
|
}
|
2013-02-24 01:15:05 -05:00
|
|
|
|
2013-07-09 22:38:17 -04:00
|
|
|
assert res.decode_content, '[Bug #7924]' if Net::HTTP::HAVE_ZLIB
|
2005-09-23 22:45:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def _test_get__iter(http)
|
|
|
|
buf = ''
|
2010-12-09 20:08:45 -05:00
|
|
|
res = http.get('/') {|s| buf << s }
|
2005-09-23 22:45:02 -04:00
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
# assert_kind_of String, res.body
|
2010-05-03 19:42:26 -04:00
|
|
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
|
|
|
assert_not_nil res['content-length']
|
|
|
|
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
|
|
|
end
|
2005-09-23 22:45:02 -04:00
|
|
|
assert_equal $test_net_http_data_type, res['Content-Type']
|
|
|
|
assert_equal $test_net_http_data.size, buf.size
|
|
|
|
assert_equal $test_net_http_data, buf
|
|
|
|
# assert_equal $test_net_http_data.size, res.body.size
|
|
|
|
# assert_equal $test_net_http_data, res.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def _test_get__chunked(http)
|
|
|
|
buf = ''
|
2010-12-09 20:08:45 -05:00
|
|
|
res = http.get('/') {|s| buf << s }
|
2005-09-23 22:45:02 -04:00
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
# assert_kind_of String, res.body
|
2010-05-03 19:42:26 -04:00
|
|
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
|
|
|
assert_not_nil res['content-length']
|
|
|
|
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
|
|
|
end
|
2005-09-23 22:45:02 -04:00
|
|
|
assert_equal $test_net_http_data_type, res['Content-Type']
|
|
|
|
assert_equal $test_net_http_data.size, buf.size
|
|
|
|
assert_equal $test_net_http_data, buf
|
|
|
|
# assert_equal $test_net_http_data.size, res.body.size
|
|
|
|
# assert_equal $test_net_http_data, res.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_get__break
|
|
|
|
i = 0
|
|
|
|
start {|http|
|
|
|
|
http.get('/') do |str|
|
|
|
|
i += 1
|
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
|
|
|
assert_equal 1, i
|
2014-11-09 06:51:06 -05:00
|
|
|
@log_tester = nil # server may encount ECONNRESET
|
2005-09-23 22:45:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_get__implicit_start
|
2010-12-09 20:08:45 -05:00
|
|
|
res = new().get('/')
|
2005-09-23 22:45:02 -04:00
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
assert_kind_of String, res.body
|
2010-05-03 19:42:26 -04:00
|
|
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
|
|
|
assert_not_nil res['content-length']
|
|
|
|
end
|
2005-09-23 22:45:02 -04:00
|
|
|
assert_equal $test_net_http_data_type, res['Content-Type']
|
|
|
|
assert_equal $test_net_http_data.size, res.body.size
|
|
|
|
assert_equal $test_net_http_data, res.body
|
|
|
|
end
|
|
|
|
|
2016-07-05 20:01:20 -04:00
|
|
|
def test_get__crlf
|
|
|
|
start {|http|
|
|
|
|
assert_raise(ArgumentError) do
|
|
|
|
http.get("\r")
|
|
|
|
end
|
2016-07-05 20:16:17 -04:00
|
|
|
assert_raise(ArgumentError) do
|
|
|
|
http.get("\n")
|
|
|
|
end
|
2016-07-05 20:01:20 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-09-23 22:45:02 -04:00
|
|
|
def test_get2
|
|
|
|
start {|http|
|
|
|
|
http.get2('/') {|res|
|
2013-01-05 10:04:46 -05:00
|
|
|
EnvUtil.suppress_warning do
|
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
assert_kind_of Net::HTTPResponse, res.header
|
|
|
|
end
|
|
|
|
|
2010-05-03 19:42:26 -04:00
|
|
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
|
|
|
assert_not_nil res['content-length']
|
|
|
|
end
|
2005-09-23 22:45:02 -04:00
|
|
|
assert_equal $test_net_http_data_type, res['Content-Type']
|
|
|
|
assert_kind_of String, res.body
|
|
|
|
assert_kind_of String, res.entity
|
|
|
|
assert_equal $test_net_http_data.size, res.body.size
|
|
|
|
assert_equal $test_net_http_data, res.body
|
|
|
|
assert_equal $test_net_http_data, res.entity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_post
|
|
|
|
start {|http|
|
|
|
|
_test_post__base http
|
|
|
|
_test_post__file http
|
2012-12-23 14:58:44 -05:00
|
|
|
_test_post__no_data http
|
2005-09-23 22:45:02 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def _test_post__base(http)
|
|
|
|
uheader = {}
|
|
|
|
uheader['Accept'] = 'application/octet-stream'
|
2011-12-22 04:48:50 -05:00
|
|
|
uheader['Content-Type'] = 'application/x-www-form-urlencoded'
|
2005-09-23 22:45:02 -04:00
|
|
|
data = 'post data'
|
2011-12-22 04:48:50 -05:00
|
|
|
res = http.post('/', data, uheader)
|
2005-09-23 22:45:02 -04:00
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
assert_kind_of String, res.body
|
|
|
|
assert_equal data, res.body
|
|
|
|
assert_equal data, res.entity
|
|
|
|
end
|
|
|
|
|
|
|
|
def _test_post__file(http)
|
|
|
|
data = 'post data'
|
|
|
|
f = StringIO.new
|
2011-12-22 04:48:50 -05:00
|
|
|
http.post('/', data, {'content-type' => 'application/x-www-form-urlencoded'}, f)
|
2005-09-23 22:45:02 -04:00
|
|
|
assert_equal data, f.string
|
|
|
|
end
|
|
|
|
|
2012-12-23 14:58:44 -05:00
|
|
|
def _test_post__no_data(http)
|
|
|
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
2013-01-05 10:04:46 -05:00
|
|
|
EnvUtil.suppress_warning do
|
|
|
|
data = nil
|
|
|
|
res = http.post('/', data)
|
|
|
|
assert_not_equal '411', res.code
|
|
|
|
end
|
2012-12-23 14:58:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-05 10:39:06 -04:00
|
|
|
def test_s_post
|
|
|
|
url = "http://#{config('host')}:#{config('port')}/"
|
|
|
|
res = Net::HTTP.post(
|
|
|
|
URI.parse(url),
|
|
|
|
"a=x")
|
|
|
|
assert_equal "application/x-www-form-urlencoded", res["Content-Type"]
|
|
|
|
assert_equal "a=x", res.body
|
|
|
|
|
|
|
|
res = Net::HTTP.post(
|
|
|
|
URI.parse(url),
|
|
|
|
"hello world",
|
|
|
|
"Content-Type" => "text/plain; charset=US-ASCII")
|
|
|
|
assert_equal "text/plain; charset=US-ASCII", res["Content-Type"]
|
|
|
|
assert_equal "hello world", res.body
|
|
|
|
end
|
|
|
|
|
2007-05-12 05:25:56 -04:00
|
|
|
def test_s_post_form
|
2011-06-21 13:44:44 -04:00
|
|
|
url = "http://#{config('host')}:#{config('port')}/"
|
2007-05-12 05:25:56 -04:00
|
|
|
res = Net::HTTP.post_form(
|
2011-06-21 13:44:44 -04:00
|
|
|
URI.parse(url),
|
2007-05-12 05:25:56 -04:00
|
|
|
"a" => "x")
|
|
|
|
assert_equal ["a=x"], res.body.split(/[;&]/).sort
|
|
|
|
|
|
|
|
res = Net::HTTP.post_form(
|
2011-06-21 13:44:44 -04:00
|
|
|
URI.parse(url),
|
2007-05-12 05:25:56 -04:00
|
|
|
"a" => "x",
|
|
|
|
"b" => "y")
|
|
|
|
assert_equal ["a=x", "b=y"], res.body.split(/[;&]/).sort
|
|
|
|
|
|
|
|
res = Net::HTTP.post_form(
|
2011-06-21 13:44:44 -04:00
|
|
|
URI.parse(url),
|
2007-05-12 05:25:56 -04:00
|
|
|
"a" => ["x1", "x2"],
|
|
|
|
"b" => "y")
|
2011-06-21 13:44:44 -04:00
|
|
|
assert_equal url, res['X-request-uri']
|
2007-05-12 05:25:56 -04:00
|
|
|
assert_equal ["a=x1", "a=x2", "b=y"], res.body.split(/[;&]/).sort
|
2011-06-21 13:44:44 -04:00
|
|
|
|
|
|
|
res = Net::HTTP.post_form(
|
|
|
|
URI.parse(url + '?a=x'),
|
|
|
|
"b" => "y")
|
|
|
|
assert_equal url + '?a=x', res['X-request-uri']
|
|
|
|
assert_equal ["b=y"], res.body.split(/[;&]/).sort
|
2007-05-12 05:25:56 -04:00
|
|
|
end
|
|
|
|
|
2010-06-01 08:30:52 -04:00
|
|
|
def test_patch
|
|
|
|
start {|http|
|
|
|
|
_test_patch__base http
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def _test_patch__base(http)
|
|
|
|
uheader = {}
|
|
|
|
uheader['Accept'] = 'application/octet-stream'
|
2011-12-22 04:48:50 -05:00
|
|
|
uheader['Content-Type'] = 'application/x-www-form-urlencoded'
|
2010-06-01 08:30:52 -04:00
|
|
|
data = 'patch data'
|
2011-12-22 04:48:50 -05:00
|
|
|
res = http.patch('/', data, uheader)
|
2010-06-01 08:30:52 -04:00
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
assert_kind_of String, res.body
|
|
|
|
assert_equal data, res.body
|
|
|
|
assert_equal data, res.entity
|
|
|
|
end
|
|
|
|
|
2011-01-12 02:26:18 -05:00
|
|
|
def test_timeout_during_HTTP_session
|
|
|
|
bug4246 = "expected the HTTP session to have timed out but have not. c.f. [ruby-core:34203]"
|
|
|
|
|
2014-05-26 01:38:12 -04:00
|
|
|
th = nil
|
2012-02-27 23:51:37 -05:00
|
|
|
# listen for connections... but deliberately do not read
|
2011-08-01 18:35:05 -04:00
|
|
|
TCPServer.open('localhost', 0) {|server|
|
2011-01-12 02:26:18 -05:00
|
|
|
port = server.addr[1]
|
|
|
|
|
|
|
|
conn = Net::HTTP.new('localhost', port)
|
2012-02-27 23:51:37 -05:00
|
|
|
conn.read_timeout = 0.01
|
2013-10-07 02:36:27 -04:00
|
|
|
conn.open_timeout = 0.1
|
2011-01-12 02:26:18 -05:00
|
|
|
|
|
|
|
th = Thread.new do
|
2012-04-11 17:20:51 -04:00
|
|
|
assert_raise(Net::ReadTimeout) {
|
2011-01-12 02:26:18 -05:00
|
|
|
conn.get('/')
|
|
|
|
}
|
|
|
|
end
|
|
|
|
assert th.join(10), bug4246
|
|
|
|
}
|
2014-05-26 01:38:12 -04:00
|
|
|
ensure
|
|
|
|
th.kill
|
|
|
|
th.join
|
2011-01-12 02:26:18 -05:00
|
|
|
end
|
2005-09-23 22:45:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
module TestNetHTTP_version_1_2_methods
|
|
|
|
|
|
|
|
def test_request
|
|
|
|
start {|http|
|
|
|
|
_test_request__GET http
|
2013-02-13 20:08:19 -05:00
|
|
|
_test_request__accept_encoding http
|
2005-09-23 22:45:02 -04:00
|
|
|
_test_request__file http
|
|
|
|
# _test_request__range http # WEBrick does not support Range: header.
|
|
|
|
_test_request__HEAD http
|
|
|
|
_test_request__POST http
|
|
|
|
_test_request__stream_body http
|
2012-12-21 15:36:07 -05:00
|
|
|
_test_request__uri http
|
|
|
|
_test_request__uri_host http
|
2005-09-23 22:45:02 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def _test_request__GET(http)
|
|
|
|
req = Net::HTTP::Get.new('/')
|
|
|
|
http.request(req) {|res|
|
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
assert_kind_of String, res.body
|
2010-05-03 19:42:26 -04:00
|
|
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
|
|
|
assert_not_nil res['content-length']
|
|
|
|
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
|
|
|
end
|
2005-09-23 22:45:02 -04:00
|
|
|
assert_equal $test_net_http_data.size, res.body.size
|
|
|
|
assert_equal $test_net_http_data, res.body
|
2013-02-13 20:08:19 -05:00
|
|
|
|
2013-02-14 00:32:58 -05:00
|
|
|
assert res.decode_content, 'Bug #7831' if Net::HTTP::HAVE_ZLIB
|
2013-02-13 20:08:19 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def _test_request__accept_encoding(http)
|
|
|
|
req = Net::HTTP::Get.new('/', 'accept-encoding' => 'deflate')
|
|
|
|
http.request(req) {|res|
|
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
assert_kind_of String, res.body
|
|
|
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
|
|
|
assert_not_nil res['content-length']
|
|
|
|
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
|
|
|
end
|
|
|
|
assert_equal $test_net_http_data.size, res.body.size
|
|
|
|
assert_equal $test_net_http_data, res.body
|
|
|
|
|
2016-02-19 02:45:58 -05:00
|
|
|
assert_not_predicate res, :decode_content, 'Bug #7831' if Net::HTTP::HAVE_ZLIB
|
2005-09-23 22:45:02 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def _test_request__file(http)
|
|
|
|
req = Net::HTTP::Get.new('/')
|
|
|
|
http.request(req) {|res|
|
|
|
|
assert_kind_of Net::HTTPResponse, res
|
2010-05-03 19:42:26 -04:00
|
|
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
|
|
|
assert_not_nil res['content-length']
|
|
|
|
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
|
|
|
end
|
2008-06-29 14:02:24 -04:00
|
|
|
f = StringIO.new("".force_encoding("ASCII-8BIT"))
|
2005-09-23 22:45:02 -04:00
|
|
|
res.read_body f
|
2008-06-29 14:02:24 -04:00
|
|
|
assert_equal $test_net_http_data.bytesize, f.string.bytesize
|
2008-06-19 10:49:39 -04:00
|
|
|
assert_equal $test_net_http_data.encoding, f.string.encoding
|
2005-09-23 22:45:02 -04:00
|
|
|
assert_equal $test_net_http_data, f.string
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def _test_request__range(http)
|
|
|
|
req = Net::HTTP::Get.new('/')
|
|
|
|
req['range'] = 'bytes=0-5'
|
|
|
|
assert_equal $test_net_http_data[0,6], http.request(req).body
|
|
|
|
end
|
|
|
|
|
|
|
|
def _test_request__HEAD(http)
|
|
|
|
req = Net::HTTP::Head.new('/')
|
|
|
|
http.request(req) {|res|
|
|
|
|
assert_kind_of Net::HTTPResponse, res
|
2010-05-03 19:42:26 -04:00
|
|
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
|
|
|
assert_not_nil res['content-length']
|
|
|
|
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
|
|
|
end
|
2005-09-23 22:45:02 -04:00
|
|
|
assert_nil res.body
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def _test_request__POST(http)
|
|
|
|
data = 'post data'
|
|
|
|
req = Net::HTTP::Post.new('/')
|
|
|
|
req['Accept'] = $test_net_http_data_type
|
2011-12-22 04:48:50 -05:00
|
|
|
req['Content-Type'] = 'application/x-www-form-urlencoded'
|
2005-09-23 22:45:02 -04:00
|
|
|
http.request(req, data) {|res|
|
|
|
|
assert_kind_of Net::HTTPResponse, res
|
2010-05-03 19:42:26 -04:00
|
|
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
|
|
|
assert_equal data.size, res['content-length'].to_i
|
|
|
|
end
|
2005-09-23 22:45:02 -04:00
|
|
|
assert_kind_of String, res.body
|
|
|
|
assert_equal data, res.body
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def _test_request__stream_body(http)
|
|
|
|
req = Net::HTTP::Post.new('/')
|
|
|
|
data = $test_net_http_data
|
|
|
|
req.content_length = data.size
|
2011-12-22 04:48:50 -05:00
|
|
|
req['Content-Type'] = 'application/x-www-form-urlencoded'
|
2005-09-23 22:45:02 -04:00
|
|
|
req.body_stream = StringIO.new(data)
|
|
|
|
res = http.request(req)
|
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
assert_kind_of String, res.body
|
|
|
|
assert_equal data.size, res.body.size
|
|
|
|
assert_equal data, res.body
|
|
|
|
end
|
|
|
|
|
2012-12-21 15:36:07 -05:00
|
|
|
def _test_request__path(http)
|
2015-12-09 16:54:21 -05:00
|
|
|
uri = URI 'https://hostname.example/'
|
2012-12-21 15:36:07 -05:00
|
|
|
req = Net::HTTP::Get.new('/')
|
|
|
|
|
|
|
|
res = http.request(req)
|
|
|
|
|
|
|
|
assert_kind_of URI::Generic, req.uri
|
|
|
|
|
2016-02-19 02:45:58 -05:00
|
|
|
assert_not_equal uri, req.uri
|
2012-12-21 15:36:07 -05:00
|
|
|
|
|
|
|
assert_equal uri, res.uri
|
|
|
|
|
2016-02-19 02:45:58 -05:00
|
|
|
assert_not_same uri, req.uri
|
|
|
|
assert_not_same req.uri, res.uri
|
2012-12-21 15:36:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def _test_request__uri(http)
|
2015-12-09 16:54:21 -05:00
|
|
|
uri = URI 'https://hostname.example/'
|
2012-12-21 15:36:07 -05:00
|
|
|
req = Net::HTTP::Get.new(uri)
|
|
|
|
|
|
|
|
res = http.request(req)
|
|
|
|
|
|
|
|
assert_kind_of URI::Generic, req.uri
|
|
|
|
|
2016-02-19 02:45:58 -05:00
|
|
|
assert_not_equal uri, req.uri
|
2012-12-21 15:36:07 -05:00
|
|
|
|
|
|
|
assert_equal req.uri, res.uri
|
|
|
|
|
2016-02-19 02:45:58 -05:00
|
|
|
assert_not_same uri, req.uri
|
|
|
|
assert_not_same req.uri, res.uri
|
2012-12-21 15:36:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def _test_request__uri_host(http)
|
2014-08-05 15:10:23 -04:00
|
|
|
uri = URI 'http://other.example/'
|
2012-12-21 15:36:07 -05:00
|
|
|
|
|
|
|
req = Net::HTTP::Get.new(uri)
|
2015-12-09 16:54:21 -05:00
|
|
|
req['host'] = 'hostname.example'
|
2012-12-21 15:36:07 -05:00
|
|
|
|
|
|
|
res = http.request(req)
|
|
|
|
|
|
|
|
assert_kind_of URI::Generic, req.uri
|
|
|
|
|
2015-12-09 16:54:21 -05:00
|
|
|
assert_equal URI("http://hostname.example:#{http.port}"), res.uri
|
2012-12-21 15:36:07 -05:00
|
|
|
end
|
|
|
|
|
2005-09-23 22:45:02 -04:00
|
|
|
def test_send_request
|
|
|
|
start {|http|
|
|
|
|
_test_send_request__GET http
|
2015-01-02 07:04:53 -05:00
|
|
|
_test_send_request__HEAD http
|
2005-09-23 22:45:02 -04:00
|
|
|
_test_send_request__POST http
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def _test_send_request__GET(http)
|
|
|
|
res = http.send_request('GET', '/')
|
|
|
|
assert_kind_of Net::HTTPResponse, res
|
2010-05-03 19:42:26 -04:00
|
|
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
|
|
|
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
|
|
|
end
|
2005-09-23 22:45:02 -04:00
|
|
|
assert_kind_of String, res.body
|
|
|
|
assert_equal $test_net_http_data, res.body
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2015-01-02 07:04:53 -05:00
|
|
|
def _test_send_request__HEAD(http)
|
|
|
|
res = http.send_request('HEAD', '/')
|
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
|
|
|
assert_not_nil res['content-length']
|
|
|
|
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
|
|
|
end
|
|
|
|
assert_nil res.body
|
|
|
|
end
|
|
|
|
|
2005-09-23 22:45:02 -04:00
|
|
|
def _test_send_request__POST(http)
|
|
|
|
data = 'aaabbb cc ddddddddddd lkjoiu4j3qlkuoa'
|
2011-12-22 04:48:50 -05:00
|
|
|
res = http.send_request('POST', '/', data, 'content-type' => 'application/x-www-form-urlencoded')
|
2005-09-23 22:45:02 -04:00
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
assert_kind_of String, res.body
|
|
|
|
assert_equal data.size, res.body.size
|
|
|
|
assert_equal data, res.body
|
|
|
|
end
|
2010-12-12 20:04:37 -05:00
|
|
|
|
|
|
|
def test_set_form
|
|
|
|
require 'tempfile'
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create('ruby-test') {|file|
|
|
|
|
file << "\u{30c7}\u{30fc}\u{30bf}"
|
|
|
|
data = [
|
|
|
|
['name', 'Gonbei Nanashi'],
|
|
|
|
['name', "\u{540d}\u{7121}\u{3057}\u{306e}\u{6a29}\u{5175}\u{885b}"],
|
|
|
|
['s"i\o', StringIO.new("\u{3042 3044 4e9c 925b}")],
|
|
|
|
["file", file, filename: "ruby-test"]
|
|
|
|
]
|
|
|
|
expected = <<"__EOM__".gsub(/\n/, "\r\n")
|
2010-12-12 20:04:37 -05:00
|
|
|
--<boundary>
|
|
|
|
Content-Disposition: form-data; name="name"
|
|
|
|
|
|
|
|
Gonbei Nanashi
|
|
|
|
--<boundary>
|
|
|
|
Content-Disposition: form-data; name="name"
|
|
|
|
|
|
|
|
\xE5\x90\x8D\xE7\x84\xA1\xE3\x81\x97\xE3\x81\xAE\xE6\xA8\xA9\xE5\x85\xB5\xE8\xA1\x9B
|
|
|
|
--<boundary>
|
|
|
|
Content-Disposition: form-data; name="s\\"i\\\\o"
|
|
|
|
|
|
|
|
\xE3\x81\x82\xE3\x81\x84\xE4\xBA\x9C\xE9\x89\x9B
|
|
|
|
--<boundary>
|
|
|
|
Content-Disposition: form-data; name="file"; filename="ruby-test"
|
|
|
|
Content-Type: application/octet-stream
|
|
|
|
|
|
|
|
\xE3\x83\x87\xE3\x83\xBC\xE3\x82\xBF
|
|
|
|
--<boundary>--
|
|
|
|
__EOM__
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
start {|http|
|
|
|
|
_test_set_form_urlencoded(http, data.reject{|k,v|!v.is_a?(String)})
|
|
|
|
_test_set_form_multipart(http, false, data, expected)
|
|
|
|
_test_set_form_multipart(http, true, data, expected)
|
|
|
|
}
|
2010-12-12 20:04:37 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def _test_set_form_urlencoded(http, data)
|
|
|
|
req = Net::HTTP::Post.new('/')
|
|
|
|
req.set_form(data)
|
|
|
|
res = http.request req
|
|
|
|
assert_equal "name=Gonbei+Nanashi&name=%E5%90%8D%E7%84%A1%E3%81%97%E3%81%AE%E6%A8%A9%E5%85%B5%E8%A1%9B", res.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def _test_set_form_multipart(http, chunked_p, data, expected)
|
|
|
|
data.each{|k,v|v.rewind rescue nil}
|
|
|
|
req = Net::HTTP::Post.new('/')
|
|
|
|
req.set_form(data, 'multipart/form-data')
|
|
|
|
req['Transfer-Encoding'] = 'chunked' if chunked_p
|
|
|
|
res = http.request req
|
|
|
|
body = res.body
|
|
|
|
assert_match(/\A--(?<boundary>\S+)/, body)
|
|
|
|
/\A--(?<boundary>\S+)/ =~ body
|
|
|
|
expected = expected.gsub(/<boundary>/, boundary)
|
|
|
|
assert_equal(expected, body)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_set_form_with_file
|
|
|
|
require 'tempfile'
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create('ruby-test') {|file|
|
|
|
|
file.binmode
|
|
|
|
file << $test_net_http_data
|
|
|
|
filename = File.basename(file.to_path)
|
|
|
|
data = [['file', file]]
|
|
|
|
expected = <<"__EOM__".gsub(/\n/, "\r\n")
|
2010-12-12 20:04:37 -05:00
|
|
|
--<boundary>
|
|
|
|
Content-Disposition: form-data; name="file"; filename="<filename>"
|
|
|
|
Content-Type: application/octet-stream
|
|
|
|
|
|
|
|
<data>
|
|
|
|
--<boundary>--
|
|
|
|
__EOM__
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
expected.sub!(/<filename>/, filename)
|
|
|
|
expected.sub!(/<data>/, $test_net_http_data)
|
|
|
|
start {|http|
|
|
|
|
data.each{|k,v|v.rewind rescue nil}
|
|
|
|
req = Net::HTTP::Post.new('/')
|
|
|
|
req.set_form(data, 'multipart/form-data')
|
|
|
|
res = http.request req
|
|
|
|
body = res.body
|
|
|
|
header, _ = body.split(/\r\n\r\n/, 2)
|
|
|
|
assert_match(/\A--(?<boundary>\S+)/, body)
|
|
|
|
/\A--(?<boundary>\S+)/ =~ body
|
|
|
|
expected = expected.gsub(/<boundary>/, boundary)
|
|
|
|
assert_match(/^--(?<boundary>\S+)\r\n/, header)
|
|
|
|
assert_match(
|
|
|
|
/^Content-Disposition: form-data; name="file"; filename="#{filename}"\r\n/,
|
|
|
|
header)
|
|
|
|
assert_equal(expected, body)
|
|
|
|
|
|
|
|
data.each{|k,v|v.rewind rescue nil}
|
|
|
|
req['Transfer-Encoding'] = 'chunked'
|
|
|
|
res = http.request req
|
|
|
|
#assert_equal(expected, res.body)
|
|
|
|
}
|
2010-12-12 20:04:37 -05:00
|
|
|
}
|
|
|
|
end
|
2005-09-23 22:45:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class TestNetHTTP_v1_2 < Test::Unit::TestCase
|
|
|
|
CONFIG = {
|
|
|
|
'host' => '127.0.0.1',
|
|
|
|
'proxy_host' => nil,
|
|
|
|
'proxy_port' => nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
include TestNetHTTPUtils
|
|
|
|
include TestNetHTTP_version_1_1_methods
|
|
|
|
include TestNetHTTP_version_1_2_methods
|
|
|
|
|
|
|
|
def new
|
|
|
|
Net::HTTP.version_1_2
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-03 19:42:26 -04:00
|
|
|
class TestNetHTTP_v1_2_chunked < Test::Unit::TestCase
|
|
|
|
CONFIG = {
|
|
|
|
'host' => '127.0.0.1',
|
|
|
|
'proxy_host' => nil,
|
|
|
|
'proxy_port' => nil,
|
|
|
|
'chunked' => true,
|
|
|
|
}
|
|
|
|
|
|
|
|
include TestNetHTTPUtils
|
|
|
|
include TestNetHTTP_version_1_1_methods
|
|
|
|
include TestNetHTTP_version_1_2_methods
|
|
|
|
|
|
|
|
def new
|
|
|
|
Net::HTTP.version_1_2
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chunked_break
|
|
|
|
assert_nothing_raised("[ruby-core:29229]") {
|
|
|
|
start {|http|
|
|
|
|
http.request_get('/') {|res|
|
|
|
|
res.read_body {|chunk|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
* lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait
server returning '100 continue' response befor sending HTTP request
body. See NEWS for more detail. See #3622.
Original patch is made by Eric Hodel <drbrain@segment7.net>.
* test/net/http/test_http.rb: test it.
* NEWS: Add new feature.
On my env (Ubuntu 11.04 64bit),
9510 tests, 2203824 assertions, 0 failures, 0 errors, 29 skips
->
9514 tests, 2203836 assertions, 0 failures, 0 errors, 29 skips
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-31 04:10:42 -04:00
|
|
|
|
|
|
|
class TestNetHTTPContinue < Test::Unit::TestCase
|
|
|
|
CONFIG = {
|
|
|
|
'host' => '127.0.0.1',
|
|
|
|
'proxy_host' => nil,
|
|
|
|
'proxy_port' => nil,
|
|
|
|
'chunked' => true,
|
|
|
|
}
|
|
|
|
|
|
|
|
include TestNetHTTPUtils
|
|
|
|
|
|
|
|
def logfile
|
|
|
|
@debug = StringIO.new('')
|
|
|
|
end
|
|
|
|
|
|
|
|
def mount_proc(&block)
|
|
|
|
@server.mount('/continue', WEBrick::HTTPServlet::ProcHandler.new(block.to_proc))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expect_continue
|
|
|
|
mount_proc {|req, res|
|
|
|
|
req.continue
|
|
|
|
res.body = req.query['body']
|
|
|
|
}
|
|
|
|
start {|http|
|
2011-12-22 04:48:50 -05:00
|
|
|
uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'}
|
* lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait
server returning '100 continue' response befor sending HTTP request
body. See NEWS for more detail. See #3622.
Original patch is made by Eric Hodel <drbrain@segment7.net>.
* test/net/http/test_http.rb: test it.
* NEWS: Add new feature.
On my env (Ubuntu 11.04 64bit),
9510 tests, 2203824 assertions, 0 failures, 0 errors, 29 skips
->
9514 tests, 2203836 assertions, 0 failures, 0 errors, 29 skips
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-31 04:10:42 -04:00
|
|
|
http.continue_timeout = 0.2
|
2011-12-22 04:48:50 -05:00
|
|
|
http.request_post('/continue', 'body=BODY', uheader) {|res|
|
* lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait
server returning '100 continue' response befor sending HTTP request
body. See NEWS for more detail. See #3622.
Original patch is made by Eric Hodel <drbrain@segment7.net>.
* test/net/http/test_http.rb: test it.
* NEWS: Add new feature.
On my env (Ubuntu 11.04 64bit),
9510 tests, 2203824 assertions, 0 failures, 0 errors, 29 skips
->
9514 tests, 2203836 assertions, 0 failures, 0 errors, 29 skips
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-31 04:10:42 -04:00
|
|
|
assert_equal('BODY', res.read_body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_match(/Expect: 100-continue/, @debug.string)
|
|
|
|
assert_match(/HTTP\/1.1 100 continue/, @debug.string)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expect_continue_timeout
|
|
|
|
mount_proc {|req, res|
|
|
|
|
sleep 0.2
|
|
|
|
req.continue # just ignored because it's '100'
|
|
|
|
res.body = req.query['body']
|
|
|
|
}
|
|
|
|
start {|http|
|
2011-12-22 04:48:50 -05:00
|
|
|
uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'}
|
* lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait
server returning '100 continue' response befor sending HTTP request
body. See NEWS for more detail. See #3622.
Original patch is made by Eric Hodel <drbrain@segment7.net>.
* test/net/http/test_http.rb: test it.
* NEWS: Add new feature.
On my env (Ubuntu 11.04 64bit),
9510 tests, 2203824 assertions, 0 failures, 0 errors, 29 skips
->
9514 tests, 2203836 assertions, 0 failures, 0 errors, 29 skips
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-31 04:10:42 -04:00
|
|
|
http.continue_timeout = 0
|
2011-12-22 04:48:50 -05:00
|
|
|
http.request_post('/continue', 'body=BODY', uheader) {|res|
|
* lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait
server returning '100 continue' response befor sending HTTP request
body. See NEWS for more detail. See #3622.
Original patch is made by Eric Hodel <drbrain@segment7.net>.
* test/net/http/test_http.rb: test it.
* NEWS: Add new feature.
On my env (Ubuntu 11.04 64bit),
9510 tests, 2203824 assertions, 0 failures, 0 errors, 29 skips
->
9514 tests, 2203836 assertions, 0 failures, 0 errors, 29 skips
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-31 04:10:42 -04:00
|
|
|
assert_equal('BODY', res.read_body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_match(/Expect: 100-continue/, @debug.string)
|
|
|
|
assert_match(/HTTP\/1.1 100 continue/, @debug.string)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_expect_continue_error
|
|
|
|
mount_proc {|req, res|
|
|
|
|
res.status = 501
|
|
|
|
res.body = req.query['body']
|
|
|
|
}
|
|
|
|
start {|http|
|
2011-12-22 04:48:50 -05:00
|
|
|
uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'}
|
* lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait
server returning '100 continue' response befor sending HTTP request
body. See NEWS for more detail. See #3622.
Original patch is made by Eric Hodel <drbrain@segment7.net>.
* test/net/http/test_http.rb: test it.
* NEWS: Add new feature.
On my env (Ubuntu 11.04 64bit),
9510 tests, 2203824 assertions, 0 failures, 0 errors, 29 skips
->
9514 tests, 2203836 assertions, 0 failures, 0 errors, 29 skips
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-31 04:10:42 -04:00
|
|
|
http.continue_timeout = 0
|
2011-12-22 04:48:50 -05:00
|
|
|
http.request_post('/continue', 'body=ERROR', uheader) {|res|
|
* lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait
server returning '100 continue' response befor sending HTTP request
body. See NEWS for more detail. See #3622.
Original patch is made by Eric Hodel <drbrain@segment7.net>.
* test/net/http/test_http.rb: test it.
* NEWS: Add new feature.
On my env (Ubuntu 11.04 64bit),
9510 tests, 2203824 assertions, 0 failures, 0 errors, 29 skips
->
9514 tests, 2203836 assertions, 0 failures, 0 errors, 29 skips
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-31 04:10:42 -04:00
|
|
|
assert_equal('ERROR', res.read_body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_match(/Expect: 100-continue/, @debug.string)
|
|
|
|
assert_not_match(/HTTP\/1.1 100 continue/, @debug.string)
|
|
|
|
end
|
|
|
|
|
2015-11-20 00:57:10 -05:00
|
|
|
def test_expect_continue_error_before_body
|
|
|
|
@log_tester = nil
|
|
|
|
mount_proc {|req, res|
|
|
|
|
raise WEBrick::HTTPStatus::Forbidden
|
|
|
|
}
|
|
|
|
start {|http|
|
|
|
|
uheader = {'content-length' => '5', 'expect' => '100-continue'}
|
|
|
|
http.continue_timeout = 1 # allow the server to respond before sending
|
|
|
|
http.request_post('/continue', 'data', uheader) {|res|
|
|
|
|
assert_equal(res.code, '403')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_match(/Expect: 100-continue/, @debug.string)
|
|
|
|
assert_not_match(/HTTP\/1.1 100 continue/, @debug.string)
|
|
|
|
end
|
|
|
|
|
* lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait
server returning '100 continue' response befor sending HTTP request
body. See NEWS for more detail. See #3622.
Original patch is made by Eric Hodel <drbrain@segment7.net>.
* test/net/http/test_http.rb: test it.
* NEWS: Add new feature.
On my env (Ubuntu 11.04 64bit),
9510 tests, 2203824 assertions, 0 failures, 0 errors, 29 skips
->
9514 tests, 2203836 assertions, 0 failures, 0 errors, 29 skips
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-31 04:10:42 -04:00
|
|
|
def test_expect_continue_error_while_waiting
|
|
|
|
mount_proc {|req, res|
|
|
|
|
res.status = 501
|
|
|
|
res.body = req.query['body']
|
|
|
|
}
|
|
|
|
start {|http|
|
2011-12-22 04:48:50 -05:00
|
|
|
uheader = {'content-type' => 'application/x-www-form-urlencoded', 'expect' => '100-continue'}
|
* lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait
server returning '100 continue' response befor sending HTTP request
body. See NEWS for more detail. See #3622.
Original patch is made by Eric Hodel <drbrain@segment7.net>.
* test/net/http/test_http.rb: test it.
* NEWS: Add new feature.
On my env (Ubuntu 11.04 64bit),
9510 tests, 2203824 assertions, 0 failures, 0 errors, 29 skips
->
9514 tests, 2203836 assertions, 0 failures, 0 errors, 29 skips
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-31 04:10:42 -04:00
|
|
|
http.continue_timeout = 0.5
|
2011-12-22 04:48:50 -05:00
|
|
|
http.request_post('/continue', 'body=ERROR', uheader) {|res|
|
* lib/net/http.rb, lib/net/protocol.rb: Allow to configure to wait
server returning '100 continue' response befor sending HTTP request
body. See NEWS for more detail. See #3622.
Original patch is made by Eric Hodel <drbrain@segment7.net>.
* test/net/http/test_http.rb: test it.
* NEWS: Add new feature.
On my env (Ubuntu 11.04 64bit),
9510 tests, 2203824 assertions, 0 failures, 0 errors, 29 skips
->
9514 tests, 2203836 assertions, 0 failures, 0 errors, 29 skips
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-05-31 04:10:42 -04:00
|
|
|
assert_equal('ERROR', res.read_body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_match(/Expect: 100-continue/, @debug.string)
|
|
|
|
assert_not_match(/HTTP\/1.1 100 continue/, @debug.string)
|
|
|
|
end
|
|
|
|
end
|
2012-01-20 00:56:35 -05:00
|
|
|
|
2016-11-05 10:08:16 -04:00
|
|
|
class TestNetHTTPSwitchingProtocols < Test::Unit::TestCase
|
|
|
|
CONFIG = {
|
|
|
|
'host' => '127.0.0.1',
|
|
|
|
'proxy_host' => nil,
|
|
|
|
'proxy_port' => nil,
|
|
|
|
'chunked' => true,
|
|
|
|
}
|
|
|
|
|
|
|
|
include TestNetHTTPUtils
|
|
|
|
|
|
|
|
def logfile
|
|
|
|
@debug = StringIO.new('')
|
|
|
|
end
|
|
|
|
|
|
|
|
def mount_proc(&block)
|
|
|
|
@server.mount('/continue', WEBrick::HTTPServlet::ProcHandler.new(block.to_proc))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_info
|
|
|
|
mount_proc {|req, res|
|
|
|
|
req.instance_variable_get(:@socket) << "HTTP/1.1 101 Switching Protocols\r\n\r\n"
|
|
|
|
res.body = req.query['body']
|
|
|
|
}
|
|
|
|
start {|http|
|
|
|
|
http.continue_timeout = 0.2
|
|
|
|
http.request_post('/continue', 'body=BODY') {|res|
|
|
|
|
assert_equal('BODY', res.read_body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_match(/HTTP\/1.1 101 Switching Protocols/, @debug.string)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-20 00:56:35 -05:00
|
|
|
class TestNetHTTPKeepAlive < Test::Unit::TestCase
|
|
|
|
CONFIG = {
|
|
|
|
'host' => '127.0.0.1',
|
|
|
|
'proxy_host' => nil,
|
|
|
|
'proxy_port' => nil,
|
|
|
|
'RequestTimeout' => 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
include TestNetHTTPUtils
|
|
|
|
|
|
|
|
def test_keep_alive_get_auto_reconnect
|
|
|
|
start {|http|
|
|
|
|
res = http.get('/')
|
|
|
|
http.keep_alive_timeout = 1
|
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
assert_kind_of String, res.body
|
|
|
|
sleep 1.5
|
|
|
|
assert_nothing_raised {
|
|
|
|
res = http.get('/')
|
|
|
|
}
|
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
assert_kind_of String, res.body
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-11-27 09:07:35 -05:00
|
|
|
def test_server_closed_connection_auto_reconnect
|
|
|
|
start {|http|
|
|
|
|
res = http.get('/')
|
|
|
|
http.keep_alive_timeout = 5
|
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
assert_kind_of String, res.body
|
|
|
|
sleep 1.5
|
|
|
|
assert_nothing_raised {
|
|
|
|
# Net::HTTP should detect the closed connection before attempting the
|
|
|
|
# request, since post requests cannot be retried.
|
|
|
|
res = http.post('/', 'query=foo', 'content-type' => 'application/x-www-form-urlencoded')
|
|
|
|
}
|
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
assert_kind_of String, res.body
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-01-20 00:56:35 -05:00
|
|
|
def test_keep_alive_get_auto_retry
|
|
|
|
start {|http|
|
|
|
|
res = http.get('/')
|
|
|
|
http.keep_alive_timeout = 5
|
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
assert_kind_of String, res.body
|
|
|
|
sleep 1.5
|
2012-02-27 23:51:37 -05:00
|
|
|
res = http.get('/')
|
2012-01-20 00:56:35 -05:00
|
|
|
assert_kind_of Net::HTTPResponse, res
|
|
|
|
assert_kind_of String, res.body
|
|
|
|
}
|
|
|
|
end
|
2012-02-23 20:10:07 -05:00
|
|
|
|
2012-02-27 23:51:37 -05:00
|
|
|
def test_keep_alive_server_close
|
2012-02-23 20:10:07 -05:00
|
|
|
def @server.run(sock)
|
|
|
|
sock.close
|
|
|
|
end
|
|
|
|
|
|
|
|
start {|http|
|
2015-06-01 22:18:44 -04:00
|
|
|
assert_raise(EOFError, Errno::ECONNRESET, IOError) {
|
2012-07-18 12:48:54 -04:00
|
|
|
http.get('/')
|
2012-02-23 20:10:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2012-01-20 00:56:35 -05:00
|
|
|
end
|
2012-07-11 14:37:18 -04:00
|
|
|
|
|
|
|
class TestNetHTTPLocalBind < Test::Unit::TestCase
|
|
|
|
CONFIG = {
|
2012-07-18 12:48:54 -04:00
|
|
|
'host' => 'localhost',
|
2012-07-11 14:37:18 -04:00
|
|
|
'proxy_host' => nil,
|
|
|
|
'proxy_port' => nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
include TestNetHTTPUtils
|
|
|
|
|
|
|
|
def test_bind_to_local_host
|
|
|
|
@server.mount_proc('/show_ip') { |req, res| res.body = req.remote_ip }
|
|
|
|
|
|
|
|
http = Net::HTTP.new(config('host'), config('port'))
|
2012-07-18 12:48:54 -04:00
|
|
|
http.local_host = Addrinfo.tcp(config('host'), config('port')).ip_address
|
2012-07-11 14:37:18 -04:00
|
|
|
assert_not_nil(http.local_host)
|
|
|
|
assert_nil(http.local_port)
|
|
|
|
|
|
|
|
res = http.get('/show_ip')
|
|
|
|
assert_equal(http.local_host, res.body)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_bind_to_local_port
|
|
|
|
@server.mount_proc('/show_port') { |req, res| res.body = req.peeraddr[1].to_s }
|
|
|
|
|
|
|
|
http = Net::HTTP.new(config('host'), config('port'))
|
2012-07-18 12:48:54 -04:00
|
|
|
http.local_host = Addrinfo.tcp(config('host'), config('port')).ip_address
|
2013-09-01 08:19:15 -04:00
|
|
|
http.local_port = Addrinfo.tcp(config('host'), 0).bind {|s|
|
|
|
|
s.local_address.ip_port.to_s
|
|
|
|
}
|
2012-07-11 14:37:18 -04:00
|
|
|
assert_not_nil(http.local_host)
|
|
|
|
assert_not_nil(http.local_port)
|
|
|
|
|
|
|
|
res = http.get('/show_port')
|
|
|
|
assert_equal(http.local_port, res.body)
|
|
|
|
end
|
|
|
|
end
|
2012-12-21 15:36:07 -05:00
|
|
|
|