Skip to content

Commit f4175a1

Browse files
author
Gustavo Brunoro
committed
more tests for IPv6
1 parent ffbcf67 commit f4175a1

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

lib/elixir/lib/uri.ex

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,19 +189,15 @@ defmodule URI do
189189
]
190190
end
191191

192-
defp has_ipv6_addr?(s), do: Regex.match?(%r/\[[0-9a-zA-Z:]*\]/, s)
193192
# Split an authority into its userinfo, host and port parts.
194193
defp split_authority(s) do
195194
s = s || ""
196-
components_regex = if has_ipv6_addr?(s) do
197-
%r/(^(.*)@)?\[([a-zA-Z0-9:]*)\](:(\d*))?/
198-
else
199-
%r/(^(.*)@)?([^:]*)(:(\d*))?/
200-
end
195+
components = Regex.run %r/(^(.*)@)?(\[[a-zA-Z0-9:]*\]|[^:]*)(:(\d*))?/, s
201196

202-
components = Regex.run components_regex, s
203197
destructure [_, _, userinfo, host, _, port], nillify(components)
204198
port = if port, do: binary_to_integer(port)
199+
host = if host, do: host |> String.lstrip(?[) |> String.rstrip(?])
200+
205201
{ userinfo, host, port }
206202
end
207203

lib/elixir/test/elixir/uri_test.exs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,29 @@ defmodule URITest do
133133
end
134134

135135
test :ipv6_addresses do
136-
simple_uri = URI.parse("http://[2607:f3f0:2:0:216:3cff:fef0:174a]/")
137-
assert simple_uri.host == "2607:f3f0:2:0:216:3cff:fef0:174a"
138-
139-
userinfo_uri = URI.parse("http://user:pass@[2607:f3f0:2:0:216:3cff:fef0:174a]/")
140-
assert userinfo_uri.host == "2607:f3f0:2:0:216:3cff:fef0:174a"
141-
assert userinfo_uri.userinfo == "user:pass"
142-
143-
port_uri = URI.parse("http://[2607:f3f0:2:0:216:3cff:fef0:174a]:2222/")
144-
assert port_uri.host == "2607:f3f0:2:0:216:3cff:fef0:174a"
145-
assert port_uri.port == 2222
146-
147-
userinfo_port_uri = URI.parse("http://user:pass@[2607:f3f0:2:0:216:3cff:fef0:174a]:2222/")
148-
assert userinfo_port_uri.host == "2607:f3f0:2:0:216:3cff:fef0:174a"
149-
assert userinfo_port_uri.userinfo == "user:pass"
150-
assert userinfo_port_uri.port == 2222
136+
addrs = [
137+
"::1",
138+
"2607:f3f0:2:0:216:3cff:fef0:174a",
139+
"2051:0db8:2d5a:3521:8313:ffad:1242:8e2e"
140+
]
141+
142+
Enum.each addrs, fn(addr) ->
143+
simple_uri = URI.parse("http://[#{addr}]/")
144+
assert simple_uri.host == addr
145+
146+
userinfo_uri = URI.parse("http://user:pass@[#{addr}]/")
147+
assert userinfo_uri.host == addr
148+
assert userinfo_uri.userinfo == "user:pass"
149+
150+
port_uri = URI.parse("http://[#{addr}]:2222/")
151+
assert port_uri.host == addr
152+
assert port_uri.port == 2222
153+
154+
userinfo_port_uri = URI.parse("http://user:pass@[#{addr}]:2222/")
155+
assert userinfo_port_uri.host == addr
156+
assert userinfo_port_uri.userinfo == "user:pass"
157+
assert userinfo_port_uri.port == 2222
158+
end
151159
end
152160

153161
test :downcase_scheme do

0 commit comments

Comments
 (0)