[ruby/uri] Optimize URI#hostname and URI#hostname=
https://github.com/ruby/uri/commit/3b7ccfd835
This commit is contained in:
parent
6e06c980da
commit
c46a4b8c7f
@ -643,7 +643,7 @@ module URI
|
|||||||
#
|
#
|
||||||
def hostname
|
def hostname
|
||||||
v = self.host
|
v = self.host
|
||||||
/\A\[(.*)\]\z/ =~ v ? $1 : v
|
v&.start_with?('[') && v.end_with?(']') ? v[1..-2] : v
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sets the host part of the URI as the argument with brackets for IPv6 addresses.
|
# Sets the host part of the URI as the argument with brackets for IPv6 addresses.
|
||||||
@ -659,7 +659,7 @@ module URI
|
|||||||
# it is wrapped with brackets.
|
# it is wrapped with brackets.
|
||||||
#
|
#
|
||||||
def hostname=(v)
|
def hostname=(v)
|
||||||
v = "[#{v}]" if /\A\[.*\]\z/ !~ v && /:/ =~ v
|
v = "[#{v}]" if !(v&.start_with?('[') && v&.end_with?(']')) && v&.index(':')
|
||||||
self.host = v
|
self.host = v
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -799,8 +799,12 @@ class URI::TestGeneric < Test::Unit::TestCase
|
|||||||
|
|
||||||
u = URI("http://foo/bar")
|
u = URI("http://foo/bar")
|
||||||
assert_equal("http://foo/bar", u.to_s)
|
assert_equal("http://foo/bar", u.to_s)
|
||||||
|
u.hostname = "[::1]"
|
||||||
|
assert_equal("http://[::1]/bar", u.to_s)
|
||||||
u.hostname = "::1"
|
u.hostname = "::1"
|
||||||
assert_equal("http://[::1]/bar", u.to_s)
|
assert_equal("http://[::1]/bar", u.to_s)
|
||||||
|
u.hostname = ""
|
||||||
|
assert_equal("http:///bar", u.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_build
|
def test_build
|
||||||
|
Loading…
x
Reference in New Issue
Block a user