Skip to content

Commit

Permalink
crda: clone url when creating a new matcher (#633)
Browse files Browse the repository at this point in the history
It is currently possible to modify the url after it's
assigned to the matcher struct i.e. when newMatcher is
called subsequently. This change ensures any url passed
to the constructor (via the option method) is cloned.

Signed-off-by: crozzy <[email protected]>
  • Loading branch information
crozzy authored Jun 8, 2022
1 parent de0aac8 commit dc939a0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
8 changes: 6 additions & 2 deletions crda/remotematcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,13 @@ func withClient(c *http.Client) option {
}

// WithURL sets the URL that the matcher should use for requests.
func withURL(url *url.URL) option {
func withURL(u *url.URL) option {
return func(m *matcher) error {
m.url = url
if u == nil {
return nil
}
urlClone := *u
m.url = &urlClone
return nil
}
}
Expand Down
19 changes: 19 additions & 0 deletions crda/remotematcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ func mkMatcher(t *testing.T, srv *httptest.Server) *matcher {
return m
}

func TestMatcherURL(t *testing.T) {
expectedURL := "https://gw.api.openshift.io/api/v2/vulnerability-analysis?user_key=algo"
url, err := url.Parse("https://gw.api.openshift.io/api/v2/")
if err != nil {
t.Fatal(err)
}
_, err = newMatcher("pypi", "algo", withURL(url))
if err != nil {
t.Fatal(err)
}
m2, err := newMatcher("maven", "algo", withURL(url))
if err != nil {
t.Fatal(err)
}
if m2.url.String() != expectedURL {
t.Fatalf("Invalid url %s, expected %s", m2.url.String(), expectedURL)
}
}

func TestRemoteMatcher(t *testing.T) {
t.Parallel()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit dc939a0

Please sign in to comment.