Skip to content

Commit 1461e75

Browse files
authored
chore: upgrade go1.26 (#120)
test: refactored URL normalizer test. Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent f341b9a commit 1461e75

4 files changed

Lines changed: 59 additions & 34 deletions

File tree

‎.golangci.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ linters:
44
disable:
55
- depguard
66
- funlen
7+
- goconst
78
- godox
9+
- gomodguard
10+
- gomodguard_v2
811
- exhaustruct
12+
- exhaustruct_v5
913
- nlreturn
1014
- nonamedreturns
1115
- noinlineerr

‎go.mod‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module github.com/go-openapi/jsonreference
22

33
require (
4-
github.com/go-openapi/jsonpointer v1.0.0
4+
github.com/go-openapi/jsonpointer v1.0.1
55
github.com/go-openapi/testify/v2 v2.7.0
66
)
77

8-
go 1.25.0
8+
go 1.26.0

‎go.sum‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
github.com/go-openapi/jsonpointer v1.0.0 h1:kR9tHqY0CtZaOPVFm622dPVNhrvYpwr4uCxgL3h1H8s=
2-
github.com/go-openapi/jsonpointer v1.0.0/go.mod h1:Z3rw7dWu1p9IgitXCFamSlA5lmDiklEB6vkaxcNZW5Y=
1+
github.com/go-openapi/jsonpointer v1.0.1 h1:2KxywRmNwJkT/FMBa3iRNHEaAxSJvjqoufQZy3au1Mg=
2+
github.com/go-openapi/jsonpointer v1.0.1/go.mod h1:wI7ZYsFmbIi9nBXOZqgDaS/bqOchRGZjqxFli7FBYxY=
33
github.com/go-openapi/testify/v2 v2.7.0 h1:bycOreEj6wfBvijg3YFogZ/sFjTCDmQnwSodSzHa3X8=
44
github.com/go-openapi/testify/v2 v2.7.0/go.mod h1:SgsVHtfooshd0tublTtJ50FPKhujf47YRqauXXOUxfw=

‎internal/normalize_url_test.go‎

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,47 @@
44
package internal
55

66
import (
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+
1544
const (
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

Comments
 (0)
⌂ 换网址