Skip to content

Commit 687540e

Browse files
fredbiclaude
andcommitted
test(normalize): rebase remote refs against a local httptest server
The remote ref cases used a hardcoded https://example.com base, a domain we don't control. They now rebase against an httptest TLS server, so the base URL is locally owned and carries a port, a shape the previous fixture never covered. A closing check fetches the rebased ref to confirm it resolves against that server. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent d3354b0 commit 687540e

1 file changed

Lines changed: 75 additions & 31 deletions

File tree

‎internal/flatten/normalize/normalize_test.go‎

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
package normalize
55

66
import (
7+
"io"
8+
"net/http"
9+
"net/http/httptest"
710
"net/url"
811
"path/filepath"
912
"runtime"
@@ -12,13 +15,13 @@ import (
1215
_ "github.com/go-openapi/analysis/internal/antest"
1316
"github.com/go-openapi/spec"
1417
"github.com/go-openapi/testify/v2/assert"
18+
"github.com/go-openapi/testify/v2/require"
1519
)
1620

1721
const (
1822
definitionA = "#/definitions/A"
1923
definitionABC = "#/definitions/abc"
2024
definitionBase = "#/definitions/base"
21-
exampleBase = "https://example.com/base"
2225
)
2326

2427
func TestNormalize_Path(t *testing.T) {
@@ -42,36 +45,77 @@ func TestNormalize_Path(t *testing.T) {
4245
func TestNormalize_RebaseRef(t *testing.T) {
4346
t.Parallel()
4447

45-
assert.EqualT(t, definitionABC, RebaseRef(definitionBase, definitionABC))
46-
assert.EqualT(t, definitionABC, RebaseRef("", definitionABC))
47-
assert.EqualT(t, definitionABC, RebaseRef(".", definitionABC))
48-
assert.EqualT(t, "otherfile"+definitionABC, RebaseRef("file"+definitionBase, "otherfile"+definitionABC))
49-
assert.EqualT(t,
50-
wrapWindowsPath("../otherfile")+definitionABC,
51-
RebaseRef(wrapWindowsPath("../file")+definitionBase, wrapWindowsPath("./otherfile")+definitionABC),
52-
)
53-
assert.EqualT(t,
54-
wrapWindowsPath("../otherfile")+definitionABC,
55-
RebaseRef(wrapWindowsPath("../file")+definitionBase, wrapWindowsPath("otherfile")+definitionABC),
56-
)
57-
assert.EqualT(t,
58-
wrapWindowsPath("local/remote/otherfile")+definitionABC,
59-
RebaseRef(wrapWindowsPath("local/file")+definitionBase, wrapWindowsPath("remote/otherfile")+definitionABC),
60-
)
61-
assert.EqualT(t,
62-
wrapWindowsPath("local/remote/otherfile.yaml"),
63-
RebaseRef(wrapWindowsPath("local/file.yaml"), wrapWindowsPath("remote/otherfile.yaml")),
64-
)
65-
66-
assert.EqualT(t, "file#/definitions/abc", RebaseRef("file#/definitions/base", definitionABC))
67-
68-
// with remote
69-
assert.EqualT(t, exampleBase+definitionABC, RebaseRef(exampleBase, exampleBase+definitionABC))
70-
assert.EqualT(t, exampleBase+definitionABC, RebaseRef(exampleBase, definitionABC))
71-
assert.EqualT(t, exampleBase+"#/dir/definitions/abc", RebaseRef(exampleBase, "#/dir/definitions/abc"))
72-
assert.EqualT(t, exampleBase+"/dir/definitions/abc", RebaseRef(exampleBase+"/spec.yaml", "dir/definitions/abc"))
73-
assert.EqualT(t, exampleBase+"/dir/definitions/abc", RebaseRef(exampleBase+"/", "dir/definitions/abc"))
74-
assert.EqualT(t, "https://example.com/dir/definitions/abc", RebaseRef(exampleBase, "dir/definitions/abc"))
48+
t.Run("with local refs", func(t *testing.T) {
49+
t.Parallel()
50+
51+
assert.EqualT(t, definitionABC, RebaseRef(definitionBase, definitionABC))
52+
assert.EqualT(t, definitionABC, RebaseRef("", definitionABC))
53+
assert.EqualT(t, definitionABC, RebaseRef(".", definitionABC))
54+
assert.EqualT(t, "otherfile"+definitionABC, RebaseRef("file"+definitionBase, "otherfile"+definitionABC))
55+
assert.EqualT(t,
56+
wrapWindowsPath("../otherfile")+definitionABC,
57+
RebaseRef(wrapWindowsPath("../file")+definitionBase, wrapWindowsPath("./otherfile")+definitionABC),
58+
)
59+
assert.EqualT(t,
60+
wrapWindowsPath("../otherfile")+definitionABC,
61+
RebaseRef(wrapWindowsPath("../file")+definitionBase, wrapWindowsPath("otherfile")+definitionABC),
62+
)
63+
assert.EqualT(t,
64+
wrapWindowsPath("local/remote/otherfile")+definitionABC,
65+
RebaseRef(wrapWindowsPath("local/file")+definitionBase, wrapWindowsPath("remote/otherfile")+definitionABC),
66+
)
67+
assert.EqualT(t,
68+
wrapWindowsPath("local/remote/otherfile.yaml"),
69+
RebaseRef(wrapWindowsPath("local/file.yaml"), wrapWindowsPath("remote/otherfile.yaml")),
70+
)
71+
72+
assert.EqualT(t, "file#/definitions/abc", RebaseRef("file#/definitions/base", definitionABC))
73+
})
74+
75+
t.Run("with remote refs", func(t *testing.T) {
76+
t.Parallel()
77+
78+
server := remoteSpecServer(t)
79+
remoteBase := server.URL + "/base"
80+
81+
assert.EqualT(t, remoteBase+definitionABC, RebaseRef(remoteBase, remoteBase+definitionABC))
82+
assert.EqualT(t, remoteBase+definitionABC, RebaseRef(remoteBase, definitionABC))
83+
assert.EqualT(t, remoteBase+"#/dir/definitions/abc", RebaseRef(remoteBase, "#/dir/definitions/abc"))
84+
assert.EqualT(t, remoteBase+"/dir/definitions/abc", RebaseRef(remoteBase+"/spec.yaml", "dir/definitions/abc"))
85+
assert.EqualT(t, remoteBase+"/dir/definitions/abc", RebaseRef(remoteBase+"/", "dir/definitions/abc"))
86+
assert.EqualT(t, server.URL+"/dir/definitions/abc", RebaseRef(remoteBase, "dir/definitions/abc"))
87+
88+
t.Run("rebased ref should point to a reachable document", func(t *testing.T) {
89+
rebased := RebaseRef(remoteBase, "dir/definitions/abc")
90+
91+
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, rebased, nil)
92+
require.NoError(t, err)
93+
94+
resp, err := server.Client().Do(req)
95+
require.NoError(t, err)
96+
t.Cleanup(func() { _ = resp.Body.Close() })
97+
98+
assert.EqualT(t, http.StatusOK, resp.StatusCode)
99+
})
100+
})
101+
}
102+
103+
// remoteSpecServer starts a local TLS server standing in for a remote spec host.
104+
//
105+
// It serves a stub document at any path, so that refs rebased against it
106+
// resolve to a real, locally owned origin instead of a domain we don't control.
107+
func remoteSpecServer(t testing.TB) *httptest.Server {
108+
t.Helper()
109+
110+
const stub = `{"definitions":{"abc":{"type":"string"}}}`
111+
112+
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
113+
w.Header().Set("Content-Type", "application/json")
114+
_, _ = io.WriteString(w, stub)
115+
}))
116+
t.Cleanup(server.Close)
117+
118+
return server
75119
}
76120

77121
// wrapWindowsPath adapts path expectations for tests running on windows.

0 commit comments

Comments
 (0)