Skip to content

Commit fc5870d

Browse files
committed
Fixed test case for default content-type.
I changed content-type of request to "application/octet-stream" if request didn't have content-type.
1 parent 002441d commit fc5870d

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

test/net/http/test_http.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -494,12 +494,10 @@ def _test_post__no_data(http)
494494

495495
def test_s_post
496496
url = "http://#{config('host')}:#{config('port')}/?q=a"
497-
res = assert_warning(/Content-Type did not set/) do
498-
Net::HTTP.post(
499-
URI.parse(url),
500-
"a=x")
501-
end
502-
assert_equal "application/x-www-form-urlencoded", res["Content-Type"]
497+
res = Net::HTTP.post(
498+
URI.parse(url),
499+
"a=x")
500+
assert_equal "application/octet-stream", res["Content-Type"]
503501
assert_equal "a=x", res.body
504502
assert_equal url, res["X-request-uri"]
505503

@@ -570,9 +568,7 @@ def test_timeout_during_HTTP_session_write
570568
th = Thread.new do
571569
err = !windows? ? Net::WriteTimeout : Net::ReadTimeout
572570
assert_raise(err) do
573-
assert_warning(/Content-Type did not set/) do
574-
conn.post('/', "a"*50_000_000)
575-
end
571+
conn.post('/', "a"*50_000_000)
576572
end
577573
end
578574
assert th.join(EnvUtil.apply_timeout_scale(10))

test/net/http/utils.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ def handle_request(socket)
7171
socket.write "HTTP/1.1 100 Continue\r\n\r\n"
7272
end
7373

74+
# Set default Content-Type if not provided
75+
if !headers['Content-Type'] && (method == 'POST' || method == 'PUT' || method == 'PATCH')
76+
headers['Content-Type'] = 'application/octet-stream'
77+
end
78+
7479
req = Request.new(method, path, headers, socket)
7580
if @procs.key?(req.path) || @procs.key?("#{req.path}/")
7681
proc = @procs[req.path] || @procs["#{req.path}/"]
@@ -306,16 +311,18 @@ def handle_post(path, headers, socket)
306311
scheme = headers['X-Request-Scheme'] || 'http'
307312
host = @config['host']
308313
port = socket.addr[1]
309-
charset = parse_content_type(headers['Content-Type'])[1]
314+
content_type = headers['Content-Type'] || 'application/octet-stream'
315+
charset = parse_content_type(content_type)[1]
310316
path = "#{scheme}://#{host}:#{port}#{path}"
311317
path = path.encode(charset) if charset
312-
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}"
318+
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}"
313319
socket.print(response)
314320
end
315321

316322
def handle_patch(path, headers, socket)
317323
body = socket.read(headers['Content-Length'].to_i)
318-
response = "HTTP/1.1 200 OK\r\nContent-Type: #{headers['Content-Type']}\r\nContent-Length: #{body.bytesize}\r\n\r\n#{body}"
324+
content_type = headers['Content-Type'] || 'application/octet-stream'
325+
response = "HTTP/1.1 200 OK\r\nContent-Type: #{content_type}\r\nContent-Length: #{body.bytesize}\r\n\r\n#{body}"
319326
socket.print(response)
320327
end
321328

0 commit comments

Comments
 (0)