diff --git a/tools/apitester/internal/vcr/interactions.go b/tools/apitester/internal/vcr/interactions.go index 3c8a71a837b..8d27139c6c0 100644 --- a/tools/apitester/internal/vcr/interactions.go +++ b/tools/apitester/internal/vcr/interactions.go @@ -3,6 +3,7 @@ package vcr import ( "net/http" "os" + "strings" "testing" "gopkg.in/dnaeon/go-vcr.v4/pkg/cassette" @@ -34,8 +35,9 @@ func Play(t *testing.T, interaction *cassette.Interaction) *http.Response { req.URL.Host = fetchAPIBaseURL() req.Header.Set("User-Agent", "osv.dev/apitester") req.ContentLength = -1 + baseHost, _, _ := strings.Cut(req.URL.Host, ":") - if req.URL.Host == "localhost" || req.URL.Host == "127.0.0.1" { + if baseHost == "localhost" || baseHost == "127.0.0.1" { req.URL.Scheme = "http" } diff --git a/tools/apitester/main_test.go b/tools/apitester/main_test.go index fc87405766e..4d0e0ec2e53 100644 --- a/tools/apitester/main_test.go +++ b/tools/apitester/main_test.go @@ -16,34 +16,52 @@ import ( func jsonReplaceRules(t *testing.T, resp *http.Response) []jsonreplace.Rule { t.Helper() - if resp.Request.URL.Path != "/v1/query" || strings.Contains(t.Name(), "/Invalid") { + if strings.Contains(t.Name(), "/Invalid") { return nil } - - return []jsonreplace.Rule{ - { - Path: "vulns.#.affected.#.database_specific", - ReplaceFunc: func(_ gjson.Result) any { - return "" + if resp.Request.URL.Path == "/v1/query" { + return []jsonreplace.Rule{ + { + Path: "vulns.#.affected.#.database_specific", + ReplaceFunc: func(_ gjson.Result) any { + return "" + }, }, - }, - { - Path: "vulns.#.database_specific", - ReplaceFunc: func(_ gjson.Result) any { - return "" + { + Path: "vulns.#.database_specific", + ReplaceFunc: func(_ gjson.Result) any { + return "" + }, }, - }, - { - Path: "vulns.#.affected.#.versions", - ReplaceFunc: func(toReplace gjson.Result) any { - if toReplace.IsArray() { - return len(toReplace.Array()) - } - - return 0 + { + Path: "vulns.#.affected.#.versions", + ReplaceFunc: func(toReplace gjson.Result) any { + if toReplace.IsArray() { + return len(toReplace.Array()) + } + + return 0 + }, }, - }, + } } + + if resp.Request.URL.Path == "/v1/querybatch" { + return []jsonreplace.Rule{ + { + Path: "results.#.vulns.#.modified", + ReplaceFunc: func(toReplace gjson.Result) any { + if len(toReplace.String()) < 5 { + return toReplace.String() + } + + return toReplace.String()[:5] + "..." + }, + }, + } + } + + return nil } func normalizeJSONBody(t *testing.T, resp *http.Response) string {