ruby/test/net/http/test_http_request.rb
Taketo Takashima 2ab21f56f9 [ruby/net-http] Fix handling of IPv6 literal hosts in Net::HTTPGenericRequest
Update uri dependency to version 0.11.0 or later to use `URI::HTTP#authority` and `URI#parse` without scheme

https://github.com/ruby/net-http/commit/3d4f06bd7f

Co-authored-by: 0x1eef <0x1eef@users.noreply.github.com>
Co-authored-by: Sorah Fukumori <sora134@gmail.com>
2025-11-13 14:33:22 +00:00

126 lines
3.7 KiB
Ruby

# frozen_string_literal: false
require 'net/http'
require 'test/unit'
class HTTPRequestTest < Test::Unit::TestCase
def test_initialize_GET
req = Net::HTTP::Get.new '/'
assert_equal 'GET', req.method
assert_not_predicate req, :request_body_permitted?
assert_predicate req, :response_body_permitted?
expected = {
'accept' => %w[*/*],
'user-agent' => %w[Ruby],
}
expected['accept-encoding'] = %w[gzip;q=1.0,deflate;q=0.6,identity;q=0.3] if
Net::HTTP::HAVE_ZLIB
assert_equal expected, req.to_hash
end
def test_initialize_GET_range
req = Net::HTTP::Get.new '/', 'Range' => 'bytes=0-9'
assert_equal 'GET', req.method
assert_not_predicate req, :request_body_permitted?
assert_predicate req, :response_body_permitted?
expected = {
'accept' => %w[*/*],
'user-agent' => %w[Ruby],
'range' => %w[bytes=0-9],
}
assert_equal expected, req.to_hash
end
def test_initialize_HEAD
req = Net::HTTP::Head.new '/'
assert_equal 'HEAD', req.method
assert_not_predicate req, :request_body_permitted?
assert_not_predicate req, :response_body_permitted?
expected = {
'accept' => %w[*/*],
"accept-encoding" => %w[gzip;q=1.0,deflate;q=0.6,identity;q=0.3],
'user-agent' => %w[Ruby],
}
assert_equal expected, req.to_hash
end
def test_initialize_accept_encoding
req1 = Net::HTTP::Get.new '/'
assert req1.decode_content, 'Bug #7831 - automatically decode content'
req2 = Net::HTTP::Get.new '/', 'accept-encoding' => 'identity'
assert_not_predicate req2, :decode_content,
'Bug #7381 - do not decode content if the user overrides'
end if Net::HTTP::HAVE_ZLIB
def test_initialize_GET_uri
req = Net::HTTP::Get.new(URI("http://example.com/foo"))
assert_equal "/foo", req.path
assert_equal "example.com", req['Host']
req = Net::HTTP::Get.new(URI("https://example.com/foo"))
assert_equal "/foo", req.path
assert_equal "example.com", req['Host']
req = Net::HTTP::Get.new(URI("https://203.0.113.1/foo"))
assert_equal "/foo", req.path
assert_equal "203.0.113.1", req['Host']
req = Net::HTTP::Get.new(URI("https://203.0.113.1:8000/foo"))
assert_equal "/foo", req.path
assert_equal "203.0.113.1:8000", req['Host']
req = Net::HTTP::Get.new(URI("https://[2001:db8::1]:8000/foo"))
assert_equal "/foo", req.path
assert_equal "[2001:db8::1]:8000", req['Host']
assert_raise(ArgumentError){ Net::HTTP::Get.new(URI("urn:ietf:rfc:7231")) }
assert_raise(ArgumentError){ Net::HTTP::Get.new(URI("http://")) }
end
def test_header_set
req = Net::HTTP::Get.new '/'
assert req.decode_content, 'Bug #7831 - automatically decode content'
req['accept-encoding'] = 'identity'
assert_not_predicate req, :decode_content,
'Bug #7831 - do not decode content if the user overrides'
end if Net::HTTP::HAVE_ZLIB
def test_update_uri
req = Net::HTTP::Get.new(URI.parse("http://203.0.113.1"))
req.update_uri("test", 8080, false)
assert_equal "203.0.113.1", req.uri.host
assert_equal 8080, req.uri.port
req = Net::HTTP::Get.new(URI.parse("http://203.0.113.1:2020"))
req.update_uri("test", 8080, false)
assert_equal "203.0.113.1", req.uri.host
assert_equal 8080, req.uri.port
req = Net::HTTP::Get.new(URI.parse("http://[2001:db8::1]"))
req.update_uri("test", 8080, false)
assert_equal "[2001:db8::1]", req.uri.host
assert_equal 8080, req.uri.port
req = Net::HTTP::Get.new(URI.parse("http://[2001:db8::1]:2020"))
req.update_uri("test", 8080, false)
assert_equal "[2001:db8::1]", req.uri.host
assert_equal 8080, req.uri.port
end
end