@@ -422,10 +422,10 @@ var urltests = []URLTest{
422
422
},
423
423
// worst case host, still round trips
424
424
{
425
- "scheme://!$&'()*+,;=hello!:port /path" ,
425
+ "scheme://!$&'()*+,;=hello!:1 /path" ,
426
426
& URL {
427
427
Scheme : "scheme" ,
428
- Host : "!$&'()*+,;=hello!:port " ,
428
+ Host : "!$&'()*+,;=hello!:1 " ,
429
429
Path : "/path" ,
430
430
},
431
431
"" ,
@@ -1425,11 +1425,13 @@ func TestParseErrors(t *testing.T) {
1425
1425
{"http://[::1]" , false },
1426
1426
{"http://[::1]:80" , false },
1427
1427
{"http://[::1]:namedport" , true }, // rfc3986 3.2.3
1428
+ {"http://x:namedport" , true }, // rfc3986 3.2.3
1428
1429
{"http://[::1]/" , false },
1429
1430
{"http://[::1]a" , true },
1430
1431
{"http://[::1]%23" , true },
1431
1432
{"http://[::1%25en0]" , false }, // valid zone id
1432
1433
{"http://[::1]:" , false }, // colon, but no port OK
1434
+ {"http://x:" , false }, // colon, but no port OK
1433
1435
{"http://[::1]:%38%30" , true }, // not allowed: % encoding only for non-ASCII
1434
1436
{"http://[::1%25%41]" , false }, // RFC 6874 allows over-escaping in zone
1435
1437
{"http://[%10::1]" , true }, // no %xx escapes in IP address
@@ -1621,52 +1623,46 @@ func TestURLErrorImplementsNetError(t *testing.T) {
1621
1623
}
1622
1624
}
1623
1625
1624
- func TestURLHostname (t * testing.T ) {
1626
+ func TestURLHostnameAndPort (t * testing.T ) {
1625
1627
tests := []struct {
1626
- host string // URL.Host field
1627
- want string
1628
+ in string // URL.Host field
1629
+ host string
1630
+ port string
1628
1631
}{
1629
- {"foo.com:80" , "foo.com" },
1630
- {"foo.com" , "foo.com" },
1631
- {"FOO.COM" , "FOO.COM" }, // no canonicalization (yet?)
1632
- {"1.2.3.4" , "1.2.3.4" },
1633
- {"1.2.3.4:80" , "1.2.3.4" },
1634
- {"[1:2:3:4]" , "1:2:3:4" },
1635
- {"[1:2:3:4]:80" , "1:2:3:4" },
1636
- {"[::1]:80" , "::1" },
1637
- {"[::1]" , "::1" },
1638
- {"localhost" , "localhost" },
1639
- {"localhost:443" , "localhost" },
1640
- {"some.super.long.domain.example.org:8080" , "some.super.long.domain.example.org" },
1641
- {"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:17000" , "2001:0db8:85a3:0000:0000:8a2e:0370:7334" },
1642
- {"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]" , "2001:0db8:85a3:0000:0000:8a2e:0370:7334" },
1632
+ {"foo.com:80" , "foo.com" , "80" },
1633
+ {"foo.com" , "foo.com" , "" },
1634
+ {"foo.com:" , "foo.com" , "" },
1635
+ {"FOO.COM" , "FOO.COM" , "" }, // no canonicalization
1636
+ {"1.2.3.4" , "1.2.3.4" , "" },
1637
+ {"1.2.3.4:80" , "1.2.3.4" , "80" },
1638
+ {"[1:2:3:4]" , "1:2:3:4" , "" },
1639
+ {"[1:2:3:4]:80" , "1:2:3:4" , "80" },
1640
+ {"[::1]:80" , "::1" , "80" },
1641
+ {"[::1]" , "::1" , "" },
1642
+ {"[::1]:" , "::1" , "" },
1643
+ {"localhost" , "localhost" , "" },
1644
+ {"localhost:443" , "localhost" , "443" },
1645
+ {"some.super.long.domain.example.org:8080" , "some.super.long.domain.example.org" , "8080" },
1646
+ {"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:17000" , "2001:0db8:85a3:0000:0000:8a2e:0370:7334" , "17000" },
1647
+ {"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]" , "2001:0db8:85a3:0000:0000:8a2e:0370:7334" , "" },
1648
+
1649
+ // Ensure that even when not valid, Host is one of "Hostname",
1650
+ // "Hostname:Port", "[Hostname]" or "[Hostname]:Port".
1651
+ // See https://golang.org/issue/29098.
1652
+ {"[google.com]:80" , "google.com" , "80" },
1653
+ {"google.com]:80" , "google.com]" , "80" },
1654
+ {"google.com:80_invalid_port" , "google.com:80_invalid_port" , "" },
1655
+ {"[::1]extra]:80" , "::1]extra" , "80" },
1656
+ {"google.com]extra:extra" , "google.com]extra:extra" , "" },
1643
1657
}
1644
1658
for _ , tt := range tests {
1645
- u := & URL {Host : tt .host }
1646
- got := u .Hostname ()
1647
- if got != tt .want {
1648
- t .Errorf ("Hostname for Host %q = %q; want %q" , tt .host , got , tt .want )
1659
+ u := & URL {Host : tt .in }
1660
+ host , port := u .Hostname (), u . Port ()
1661
+ if host != tt .host {
1662
+ t .Errorf ("Hostname for Host %q = %q; want %q" , tt .in , host , tt .host )
1649
1663
}
1650
- }
1651
- }
1652
-
1653
- func TestURLPort (t * testing.T ) {
1654
- tests := []struct {
1655
- host string // URL.Host field
1656
- want string
1657
- }{
1658
- {"foo.com" , "" },
1659
- {"foo.com:80" , "80" },
1660
- {"1.2.3.4" , "" },
1661
- {"1.2.3.4:80" , "80" },
1662
- {"[1:2:3:4]" , "" },
1663
- {"[1:2:3:4]:80" , "80" },
1664
- }
1665
- for _ , tt := range tests {
1666
- u := & URL {Host : tt .host }
1667
- got := u .Port ()
1668
- if got != tt .want {
1669
- t .Errorf ("Port for Host %q = %q; want %q" , tt .host , got , tt .want )
1664
+ if port != tt .port {
1665
+ t .Errorf ("Port for Host %q = %q; want %q" , tt .in , port , tt .port )
1670
1666
}
1671
1667
}
1672
1668
}
0 commit comments