[ruby/net-http] Support chunked data and fixed test failure with multipart/form-data

https://github.com/ruby/net-http/commit/b38c2795a9
This commit is contained in:
Hiroshi SHIBATA 2024-07-10 17:00:17 +09:00 коммит произвёл git
Родитель 87a45af105
Коммит 4e6463ad7a
2 изменённых файлов: 17 добавлений и 2 удалений

Просмотреть файл

@ -843,6 +843,7 @@ Content-Type: application/octet-stream
__EOM__
start {|http|
_test_set_form_urlencoded(http, data.reject{|k,v|!v.is_a?(String)})
@server.mount('/', lambda {|req, res| res.body = req.body })
_test_set_form_multipart(http, false, data, expected)
_test_set_form_multipart(http, true, data, expected)
}
@ -887,6 +888,7 @@ __EOM__
expected.sub!(/<filename>/, filename)
expected.sub!(/<data>/, $test_net_http_data)
start {|http|
@server.mount('/', lambda {|req, res| res.body = req.body })
data.each{|k,v|v.rewind rescue nil}
req = Net::HTTP::Post.new('/')
req.set_form(data, 'multipart/form-data')

Просмотреть файл

@ -96,8 +96,12 @@ module TestNetHTTPUtils
@path, @query = parse_path_and_query(path)
@headers = headers
@socket = socket
if method == 'POST' && @path == '/continue'
@body = read_body
if method == 'POST' && (@path == '/continue' || @headers['Content-Type'].include?('multipart/form-data'))
if @headers['Transfer-Encoding'] == 'chunked'
@body = read_chunked_body
else
@body = read_body
end
@query = @body.split('&').each_with_object({}) do |pair, hash|
key, value = pair.split('=')
hash[key] = value
@ -148,6 +152,15 @@ module TestNetHTTPUtils
return unless content_length && content_length > 0
@socket.read(content_length)
end
def read_chunked_body
body = ""
while (chunk_size = @socket.gets.strip.to_i(16)) > 0
body << @socket.read(chunk_size)
@socket.read(2) # read \r\n after each chunk
end
body
end
end
class Response