44package internal
55
66import (
7+ "fmt"
8+ "iter"
79 "net/url"
10+ "slices"
811 "testing"
912
1013 "github.com/go-openapi/testify/v2/assert"
1114 "github.com/go-openapi/testify/v2/require"
1215)
1316
14- // URLs spelled in more than one test in this package.
17+ func TestUrlnorm (t * testing.T ) {
18+ for testCase := range urlTestCases () {
19+ t .Run (fmt .Sprintf ("should normalize URL: %q" , testCase .url ), func (t * testing.T ) {
20+ u , err := url .Parse (testCase .url )
21+ if testCase .expectErr {
22+ require .Error (t , err , "expected url.Parse to fail" )
23+ return
24+ }
25+
26+ require .NoError (t , err )
27+
28+ NormalizeURL (u )
29+ normalized := u .String ()
30+ assert .EqualTf (t , testCase .expected , normalized , "got an unexpected normalization: %s" , normalized )
31+
32+ _ , err = url .Parse (normalized )
33+ require .NoErrorf (t , err , "normalizing %q yielded an URL that no longer parses" , testCase .url )
34+ })
35+ }
36+ }
37+
38+ type urlTestCase struct {
39+ url string
40+ expected string // expected normalized url
41+ expectErr bool // expected error when true
42+ }
43+
1544const (
1645 // url.Parse reads these as the host ":a" on a default port, so dropping the port would
1746 // leave "https://:a", which no longer parses.
18- degenerateHostHTTPS = "https://:a:443"
47+ degenerateHostHTTPS = "https://:a:443" // since go1.26 this degenerate case is no longer tolerated
1948 degenerateHostHTTP = "http://:a:80"
2049
2150 ipv6NonDefaultPort = "https://[2001:db8::1]:8443/folder"
@@ -30,11 +59,8 @@ const (
3059 userinfoNormalized = "https://user:pw@xyz.com/folder"
3160)
3261
33- func TestUrlnorm (t * testing.T ) {
34- testCases := []struct {
35- url string
36- expected string
37- }{
62+ func urlTestCases () iter.Seq [urlTestCase ] {
63+ return slices .Values ([]urlTestCase {
3864 {
3965 url : mixedCaseDefaultPort ,
4066 expected : "https://xyz.com/folder/file" ,
@@ -60,12 +86,19 @@ func TestUrlnorm(t *testing.T) {
6086 expected : ipv6NonDefaultPort ,
6187 },
6288 {
63- url : degenerateHostHTTPS ,
64- expected : degenerateHostHTTPS ,
89+ // Since go1.26, url.Parse rejects a colon outside a bracketed IPv6 host on an http or https URL
90+ // (GODEBUG urlstrictcolons=1, the default from a go.mod declaring go 1.26 or later). Both $refs below
91+ // used to parse - the first as the host ":a" on port 443, the second as "0:443" on port 443 - and both
92+ // now fail. normalizeURI logs a warning, repairs the $ref to the empty URI and resolves it against the
93+ // base, so the base itself comes back.
94+ url : degenerateHostHTTPS ,
95+ expected : degenerateHostHTTPS ,
96+ expectErr : true ,
6597 },
6698 {
67- url : degenerateHostHTTP ,
68- expected : degenerateHostHTTP ,
99+ url : degenerateHostHTTP ,
100+ expected : degenerateHostHTTP ,
101+ expectErr : true ,
69102 },
70103 {
71104 // a run of slashes collapses to one, however long the run is
@@ -77,28 +110,16 @@ func TestUrlnorm(t *testing.T) {
77110 expected : "https://xyz.com/" ,
78111 },
79112 {
80- url : "https://:]:443" ,
81- expected : "https://:]:443" ,
113+ url : "https://:]:443" ,
114+ expected : "https://:]:443" ,
115+ expectErr : true ,
82116 },
83117 {
84118 // the host is ":80" on port 80, so the removal has to run twice. Emptying the host
85119 // drops the "//" as well, which url.URL.String has always done.
86- url : "http://:80:80" ,
87- expected : "http:" ,
120+ url : "http://:80:80" ,
121+ expected : "http:" ,
122+ expectErr : true ,
88123 },
89- }
90-
91- for _ , toPin := range testCases {
92- testCase := toPin
93-
94- u , err := url .Parse (testCase .url )
95- require .NoError (t , err )
96-
97- NormalizeURL (u )
98- normalized := u .String ()
99- assert .EqualT (t , testCase .expected , normalized )
100-
101- _ , err = url .Parse (normalized )
102- require .NoErrorf (t , err , "normalizing %q yielded a URL that no longer parses" , testCase .url )
103- }
124+ })
104125}
0 commit comments