mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/net/http.rb (Net::HTTPResponse#read_chunked): ensure to skip the
last newline of chunk. [ruby-core:29229] * test/net/http/utils.rb: add an option for chunked response test. * test/net/http/test_http.rb: add tests for chunked response. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27605 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
e6ccffd966
commit
57c1406697
4 changed files with 94 additions and 20 deletions
|
@ -1,3 +1,12 @@
|
||||||
|
Tue May 4 07:52:33 2010 Yusuke Endoh <mame@tsg.ne.jp>
|
||||||
|
|
||||||
|
* lib/net/http.rb (Net::HTTPResponse#read_chunked): ensure to skip the
|
||||||
|
last newline of chunk. [ruby-core:29229]
|
||||||
|
|
||||||
|
* test/net/http/utils.rb: add an option for chunked response test.
|
||||||
|
|
||||||
|
* test/net/http/test_http.rb: add tests for chunked response.
|
||||||
|
|
||||||
Tue May 4 03:37:54 2010 NARUSE, Yui <naruse@ruby-lang.org>
|
Tue May 4 03:37:54 2010 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
* ext/nkf/nkf-utf8/nkf.c: Update nkf 2010-04-28.
|
* ext/nkf/nkf-utf8/nkf.c: Update nkf 2010-04-28.
|
||||||
|
|
|
@ -2433,8 +2433,12 @@ module Net #:nodoc:
|
||||||
raise HTTPBadResponse, "wrong chunk size line: #{line}"
|
raise HTTPBadResponse, "wrong chunk size line: #{line}"
|
||||||
len = hexlen.hex
|
len = hexlen.hex
|
||||||
break if len == 0
|
break if len == 0
|
||||||
@socket.read len, dest; total += len
|
begin
|
||||||
@socket.read 2 # \r\n
|
@socket.read len, dest
|
||||||
|
ensure
|
||||||
|
total += len
|
||||||
|
@socket.read 2 # \r\n
|
||||||
|
end
|
||||||
end
|
end
|
||||||
until @socket.readline.empty?
|
until @socket.readline.empty?
|
||||||
# none
|
# none
|
||||||
|
|
|
@ -17,7 +17,9 @@ module TestNetHTTP_version_1_1_methods
|
||||||
res = http.head('/')
|
res = http.head('/')
|
||||||
assert_kind_of Net::HTTPResponse, res
|
assert_kind_of Net::HTTPResponse, res
|
||||||
assert_equal $test_net_http_data_type, res['Content-Type']
|
assert_equal $test_net_http_data_type, res['Content-Type']
|
||||||
assert_equal $test_net_http_data.size, res['Content-Length'].to_i
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
||||||
|
assert_equal $test_net_http_data.size, res['Content-Length'].to_i
|
||||||
|
end
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -34,8 +36,10 @@ module TestNetHTTP_version_1_1_methods
|
||||||
assert_kind_of Net::HTTPResponse, res
|
assert_kind_of Net::HTTPResponse, res
|
||||||
assert_kind_of String, res.body
|
assert_kind_of String, res.body
|
||||||
assert_kind_of String, body
|
assert_kind_of String, body
|
||||||
assert_not_nil res['content-length']
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
||||||
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
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_type, res['Content-Type']
|
assert_equal $test_net_http_data_type, res['Content-Type']
|
||||||
assert_equal $test_net_http_data.size, body.size
|
assert_equal $test_net_http_data.size, body.size
|
||||||
assert_equal $test_net_http_data, body
|
assert_equal $test_net_http_data, body
|
||||||
|
@ -49,8 +53,10 @@ module TestNetHTTP_version_1_1_methods
|
||||||
assert_kind_of Net::HTTPResponse, res
|
assert_kind_of Net::HTTPResponse, res
|
||||||
# assert_kind_of String, res.body
|
# assert_kind_of String, res.body
|
||||||
# assert_kind_of String, body
|
# assert_kind_of String, body
|
||||||
assert_not_nil res['content-length']
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
||||||
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
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_type, res['Content-Type']
|
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.size, buf.size
|
||||||
assert_equal $test_net_http_data, buf
|
assert_equal $test_net_http_data, buf
|
||||||
|
@ -64,8 +70,10 @@ module TestNetHTTP_version_1_1_methods
|
||||||
assert_kind_of Net::HTTPResponse, res
|
assert_kind_of Net::HTTPResponse, res
|
||||||
# assert_kind_of String, res.body
|
# assert_kind_of String, res.body
|
||||||
# assert_kind_of String, body
|
# assert_kind_of String, body
|
||||||
assert_not_nil res['content-length']
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
||||||
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
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_type, res['Content-Type']
|
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.size, buf.size
|
||||||
assert_equal $test_net_http_data, buf
|
assert_equal $test_net_http_data, buf
|
||||||
|
@ -89,7 +97,9 @@ module TestNetHTTP_version_1_1_methods
|
||||||
assert_kind_of Net::HTTPResponse, res
|
assert_kind_of Net::HTTPResponse, res
|
||||||
assert_kind_of String, body
|
assert_kind_of String, body
|
||||||
assert_kind_of String, res.body
|
assert_kind_of String, res.body
|
||||||
assert_not_nil res['content-length']
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
||||||
|
assert_not_nil res['content-length']
|
||||||
|
end
|
||||||
assert_equal $test_net_http_data_type, res['Content-Type']
|
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.size, res.body.size
|
||||||
assert_equal $test_net_http_data, res.body
|
assert_equal $test_net_http_data, res.body
|
||||||
|
@ -100,7 +110,9 @@ module TestNetHTTP_version_1_1_methods
|
||||||
http.get2('/') {|res|
|
http.get2('/') {|res|
|
||||||
assert_kind_of Net::HTTPResponse, res
|
assert_kind_of Net::HTTPResponse, res
|
||||||
assert_kind_of Net::HTTPResponse, res.header
|
assert_kind_of Net::HTTPResponse, res.header
|
||||||
assert_not_nil res['content-length']
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
||||||
|
assert_not_nil res['content-length']
|
||||||
|
end
|
||||||
assert_equal $test_net_http_data_type, res['Content-Type']
|
assert_equal $test_net_http_data_type, res['Content-Type']
|
||||||
assert_kind_of String, res.body
|
assert_kind_of String, res.body
|
||||||
assert_kind_of String, res.entity
|
assert_kind_of String, res.entity
|
||||||
|
@ -178,8 +190,10 @@ module TestNetHTTP_version_1_2_methods
|
||||||
http.request(req) {|res|
|
http.request(req) {|res|
|
||||||
assert_kind_of Net::HTTPResponse, res
|
assert_kind_of Net::HTTPResponse, res
|
||||||
assert_kind_of String, res.body
|
assert_kind_of String, res.body
|
||||||
assert_not_nil res['content-length']
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
||||||
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
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.size, res.body.size
|
||||||
assert_equal $test_net_http_data, res.body
|
assert_equal $test_net_http_data, res.body
|
||||||
}
|
}
|
||||||
|
@ -189,8 +203,10 @@ module TestNetHTTP_version_1_2_methods
|
||||||
req = Net::HTTP::Get.new('/')
|
req = Net::HTTP::Get.new('/')
|
||||||
http.request(req) {|res|
|
http.request(req) {|res|
|
||||||
assert_kind_of Net::HTTPResponse, res
|
assert_kind_of Net::HTTPResponse, res
|
||||||
assert_not_nil res['content-length']
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
||||||
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
assert_not_nil res['content-length']
|
||||||
|
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
||||||
|
end
|
||||||
f = StringIO.new("".force_encoding("ASCII-8BIT"))
|
f = StringIO.new("".force_encoding("ASCII-8BIT"))
|
||||||
res.read_body f
|
res.read_body f
|
||||||
assert_equal $test_net_http_data.bytesize, f.string.bytesize
|
assert_equal $test_net_http_data.bytesize, f.string.bytesize
|
||||||
|
@ -209,8 +225,10 @@ module TestNetHTTP_version_1_2_methods
|
||||||
req = Net::HTTP::Head.new('/')
|
req = Net::HTTP::Head.new('/')
|
||||||
http.request(req) {|res|
|
http.request(req) {|res|
|
||||||
assert_kind_of Net::HTTPResponse, res
|
assert_kind_of Net::HTTPResponse, res
|
||||||
assert_not_nil res['content-length']
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
||||||
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
assert_not_nil res['content-length']
|
||||||
|
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
||||||
|
end
|
||||||
assert_nil res.body
|
assert_nil res.body
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
@ -221,7 +239,9 @@ module TestNetHTTP_version_1_2_methods
|
||||||
req['Accept'] = $test_net_http_data_type
|
req['Accept'] = $test_net_http_data_type
|
||||||
http.request(req, data) {|res|
|
http.request(req, data) {|res|
|
||||||
assert_kind_of Net::HTTPResponse, res
|
assert_kind_of Net::HTTPResponse, res
|
||||||
assert_equal data.size, res['content-length'].to_i
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
||||||
|
assert_equal data.size, res['content-length'].to_i
|
||||||
|
end
|
||||||
assert_kind_of String, res.body
|
assert_kind_of String, res.body
|
||||||
assert_equal data, res.body
|
assert_equal data, res.body
|
||||||
}
|
}
|
||||||
|
@ -249,7 +269,9 @@ module TestNetHTTP_version_1_2_methods
|
||||||
def _test_send_request__GET(http)
|
def _test_send_request__GET(http)
|
||||||
res = http.send_request('GET', '/')
|
res = http.send_request('GET', '/')
|
||||||
assert_kind_of Net::HTTPResponse, res
|
assert_kind_of Net::HTTPResponse, res
|
||||||
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
unless self.is_a?(TestNetHTTP_v1_2_chunked)
|
||||||
|
assert_equal $test_net_http_data.size, res['content-length'].to_i
|
||||||
|
end
|
||||||
assert_kind_of String, res.body
|
assert_kind_of String, res.body
|
||||||
assert_equal $test_net_http_data, res.body
|
assert_equal $test_net_http_data, res.body
|
||||||
end
|
end
|
||||||
|
@ -299,6 +321,38 @@ class TestNetHTTP_v1_2 < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class TestNetHTTP_v1_2_chunked < Test::Unit::TestCase
|
||||||
|
CONFIG = {
|
||||||
|
'host' => '127.0.0.1',
|
||||||
|
'port' => 10081,
|
||||||
|
'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
|
||||||
|
i = 0
|
||||||
|
assert_nothing_raised("[ruby-core:29229]") {
|
||||||
|
start {|http|
|
||||||
|
http.request_get('/') {|res|
|
||||||
|
res.read_body {|chunk|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
=begin
|
=begin
|
||||||
class TestNetHTTP_proxy < Test::Unit::TestCase
|
class TestNetHTTP_proxy < Test::Unit::TestCase
|
||||||
CONFIG = {
|
CONFIG = {
|
||||||
|
|
|
@ -48,6 +48,7 @@ module TestNetHTTPUtils
|
||||||
:ShutdownSocketWithoutClose => true,
|
:ShutdownSocketWithoutClose => true,
|
||||||
:ServerType => Thread,
|
:ServerType => Thread,
|
||||||
}
|
}
|
||||||
|
server_config[:OutputBufferSize] = 4 if config('chunked')
|
||||||
if defined?(OpenSSL) and config('ssl_enable')
|
if defined?(OpenSSL) and config('ssl_enable')
|
||||||
server_config.update({
|
server_config.update({
|
||||||
:SSLEnable => true,
|
:SSLEnable => true,
|
||||||
|
@ -56,7 +57,7 @@ module TestNetHTTPUtils
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
@server = WEBrick::HTTPServer.new(server_config)
|
@server = WEBrick::HTTPServer.new(server_config)
|
||||||
@server.mount('/', Servlet)
|
@server.mount('/', Servlet, config('chunked'))
|
||||||
@server.start
|
@server.start
|
||||||
n_try_max = 5
|
n_try_max = 5
|
||||||
begin
|
begin
|
||||||
|
@ -75,15 +76,21 @@ module TestNetHTTPUtils
|
||||||
$test_net_http_data_type = 'application/octet-stream'
|
$test_net_http_data_type = 'application/octet-stream'
|
||||||
|
|
||||||
class Servlet < WEBrick::HTTPServlet::AbstractServlet
|
class Servlet < WEBrick::HTTPServlet::AbstractServlet
|
||||||
|
def initialize(this, chunked = false)
|
||||||
|
@chunked = chunked
|
||||||
|
end
|
||||||
|
|
||||||
def do_GET(req, res)
|
def do_GET(req, res)
|
||||||
res['Content-Type'] = $test_net_http_data_type
|
res['Content-Type'] = $test_net_http_data_type
|
||||||
res.body = $test_net_http_data
|
res.body = $test_net_http_data
|
||||||
|
res.chunked = @chunked
|
||||||
end
|
end
|
||||||
|
|
||||||
# echo server
|
# echo server
|
||||||
def do_POST(req, res)
|
def do_POST(req, res)
|
||||||
res['Content-Type'] = req['Content-Type']
|
res['Content-Type'] = req['Content-Type']
|
||||||
res.body = req.body
|
res.body = req.body
|
||||||
|
res.chunked = @chunked
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue