[ruby/net-http] Fixed test case for default content-type.

I changed content-type of request to "application/octet-stream" if request didn't have
content-type.

https://github.com/ruby/net-http/commit/fc5870d2ac
This commit is contained in:
Hiroshi SHIBATA 2025-06-11 10:45:41 +09:00 committed by git
parent 51b70d106a
commit 82e3312493
2 changed files with 15 additions and 12 deletions

View File

@ -494,12 +494,10 @@ module TestNetHTTP_version_1_1_methods
def test_s_post
url = "http://#{config('host')}:#{config('port')}/?q=a"
res = assert_warning(/Content-Type did not set/) do
Net::HTTP.post(
URI.parse(url),
"a=x")
end
assert_equal "application/x-www-form-urlencoded", res["Content-Type"]
res = Net::HTTP.post(
URI.parse(url),
"a=x")
assert_equal "application/octet-stream", res["Content-Type"]
assert_equal "a=x", res.body
assert_equal url, res["X-request-uri"]
@ -570,9 +568,7 @@ module TestNetHTTP_version_1_1_methods
th = Thread.new do
err = !windows? ? Net::WriteTimeout : Net::ReadTimeout
assert_raise(err) do
assert_warning(/Content-Type did not set/) do
conn.post('/', "a"*50_000_000)
end
conn.post('/', "a"*50_000_000)
end
end
assert th.join(EnvUtil.apply_timeout_scale(10))

View File

@ -71,6 +71,11 @@ module TestNetHTTPUtils
socket.write "HTTP/1.1 100 Continue\r\n\r\n"
end
# Set default Content-Type if not provided
if !headers['Content-Type'] && (method == 'POST' || method == 'PUT' || method == 'PATCH')
headers['Content-Type'] = 'application/octet-stream'
end
req = Request.new(method, path, headers, socket)
if @procs.key?(req.path) || @procs.key?("#{req.path}/")
proc = @procs[req.path] || @procs["#{req.path}/"]
@ -306,16 +311,18 @@ module TestNetHTTPUtils
scheme = headers['X-Request-Scheme'] || 'http'
host = @config['host']
port = socket.addr[1]
charset = parse_content_type(headers['Content-Type'])[1]
content_type = headers['Content-Type'] || 'application/octet-stream'
charset = parse_content_type(content_type)[1]
path = "#{scheme}://#{host}:#{port}#{path}"
path = path.encode(charset) if charset
response = "HTTP/1.1 200 OK\r\nContent-Type: #{headers['Content-Type']}\r\nContent-Length: #{body.bytesize}\r\nX-request-uri: #{path}\r\n\r\n#{body}"
response = "HTTP/1.1 200 OK\r\nContent-Type: #{content_type}\r\nContent-Length: #{body.bytesize}\r\nX-request-uri: #{path}\r\n\r\n#{body}"
socket.print(response)
end
def handle_patch(path, headers, socket)
body = socket.read(headers['Content-Length'].to_i)
response = "HTTP/1.1 200 OK\r\nContent-Type: #{headers['Content-Type']}\r\nContent-Length: #{body.bytesize}\r\n\r\n#{body}"
content_type = headers['Content-Type'] || 'application/octet-stream'
response = "HTTP/1.1 200 OK\r\nContent-Type: #{content_type}\r\nContent-Length: #{body.bytesize}\r\n\r\n#{body}"
socket.print(response)
end