diff --git a/artifactory/services/utils/tests/xray/server.go b/artifactory/services/utils/tests/xray/server.go index 97bd271ce..79a4aa724 100644 --- a/artifactory/services/utils/tests/xray/server.go +++ b/artifactory/services/utils/tests/xray/server.go @@ -8,11 +8,13 @@ import ( "os" "strconv" "strings" + "testing" "github.com/buger/jsonparser" "github.com/jfrog/jfrog-client-go/utils/log" clienttests "github.com/jfrog/jfrog-client-go/utils/tests" "github.com/jfrog/jfrog-client-go/xray/services" + "github.com/stretchr/testify/assert" ) const ( @@ -184,58 +186,47 @@ func buildScanHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, "Invalid reports request", http.StatusBadRequest) } -func xscGetVersionHandler(w http.ResponseWriter, r *http.Request) { - if r.Method == http.MethodGet { - _, err := fmt.Fprint(w, xscVersionResponse) - if err != nil { - log.Error(err) - http.Error(w, err.Error(), http.StatusInternalServerError) +func xscGetVersionHandlerFunc(t *testing.T) func(w http.ResponseWriter, r *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet { + _, err := fmt.Fprint(w, xscVersionResponse) + assert.NoError(t, err) + return } - return + http.Error(w, "Invalid xsc request", http.StatusBadRequest) } - http.Error(w, "Invalid xsc request", http.StatusBadRequest) } -func xscGitInfoHandler(w http.ResponseWriter, r *http.Request) { - req, err := io.ReadAll(r.Body) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - if r.Method == http.MethodPost { - var reqBody services.XscGitInfoContext - err = json.Unmarshal(req, &reqBody) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - if reqBody.GitRepoUrl == "" || reqBody.BranchName == "" || reqBody.CommitHash == "" { - w.WriteHeader(http.StatusBadRequest) - _, err := fmt.Fprint(w, XscGitInfoBadResponse) - if err != nil { - log.Error(err) - http.Error(w, err.Error(), http.StatusInternalServerError) +func xscGitInfoHandlerFunc(t *testing.T) func(w http.ResponseWriter, r *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + req, err := io.ReadAll(r.Body) + assert.NoError(t, err) + if r.Method == http.MethodPost { + var reqBody services.XscGitInfoContext + err = json.Unmarshal(req, &reqBody) + assert.NoError(t, err) + if reqBody.GitRepoUrl == "" || reqBody.BranchName == "" || reqBody.CommitHash == "" { + w.WriteHeader(http.StatusBadRequest) + _, err := fmt.Fprint(w, XscGitInfoBadResponse) + assert.NoError(t, err) + return } + w.WriteHeader(http.StatusCreated) + _, err = fmt.Fprint(w, XscGitInfoResponse) + assert.NoError(t, err) return } - w.WriteHeader(http.StatusCreated) - _, err = fmt.Fprint(w, XscGitInfoResponse) - if err != nil { - log.Error(err) - http.Error(w, err.Error(), http.StatusInternalServerError) - } - return + http.Error(w, "Invalid xsc request", http.StatusBadRequest) } - http.Error(w, "Invalid xsc request", http.StatusBadRequest) } -func StartXrayMockServer() int { +func StartXrayMockServer(t *testing.T) int { handlers := clienttests.HttpServerHandlers{} handlers["/api/xray/scanBuild"] = scanBuildHandler handlers["/api/v2/summary/artifact"] = artifactSummaryHandler handlers["/api/v1/entitlements/feature/"] = entitlementsHandler - handlers["/xsc/api/v1/system/version"] = xscGetVersionHandler - handlers["/xsc/api/v1/gitinfo"] = xscGitInfoHandler + handlers["/xsc/api/v1/system/version"] = xscGetVersionHandlerFunc(t) + handlers["/xsc/api/v1/gitinfo"] = xscGitInfoHandlerFunc(t) handlers[fmt.Sprintf("/%s/", services.ReportsAPI)] = reportHandler handlers[fmt.Sprintf("/%s/", services.BuildScanAPI)] = buildScanHandler handlers["/"] = http.NotFound diff --git a/tests/xray_test.go b/tests/xray_test.go index fb05cd149..7447dccba 100644 --- a/tests/xray_test.go +++ b/tests/xray_test.go @@ -27,7 +27,7 @@ func TestXrayVersion(t *testing.T) { func TestXrayEntitlementsService(t *testing.T) { initXrayTest(t) - xrayServerPort := xray.StartXrayMockServer() + xrayServerPort := xray.StartXrayMockServer(t) xrayDetails := GetXrayDetails() client, err := jfroghttpclient.JfrogClientBuilder(). SetClientCertPath(xrayDetails.GetClientCertPath()). @@ -69,7 +69,7 @@ func testEntitlements(t *testing.T, featureId string, expected bool) { func TestScanBuild(t *testing.T) { initXrayTest(t) - xrayServerPort := xray.StartXrayMockServer() + xrayServerPort := xray.StartXrayMockServer(t) xrayDetails := newTestXrayDetails(GetXrayDetails()) client, err := jfroghttpclient.JfrogClientBuilder(). SetClientCertPath(xrayDetails.GetClientCertPath()). diff --git a/tests/xrayreport_test.go b/tests/xrayreport_test.go index ab5a5bd58..00ff4e71d 100644 --- a/tests/xrayreport_test.go +++ b/tests/xrayreport_test.go @@ -16,7 +16,7 @@ var testXrayReportService *services.ReportService func TestXrayReport(t *testing.T) { initXrayTest(t) - xrayServerPort := xray.StartXrayMockServer() + xrayServerPort := xray.StartXrayMockServer(t) xrayDetails := GetXrayDetails() client, err := jfroghttpclient.JfrogClientBuilder(). SetClientCertPath(xrayDetails.GetClientCertPath()). diff --git a/tests/xrayscan_test.go b/tests/xrayscan_test.go index b6fd76663..0930fb6b6 100644 --- a/tests/xrayscan_test.go +++ b/tests/xrayscan_test.go @@ -18,7 +18,7 @@ var testsScanService *xrayServices.ScanService func TestNewXrayScanService(t *testing.T) { initXrayTest(t) - xrayServerPort := xray.StartXrayMockServer() + xrayServerPort := xray.StartXrayMockServer(t) artDetails := GetRtDetails() client, err := jfroghttpclient.JfrogClientBuilder(). SetClientCertPath(artDetails.GetClientCertPath()). @@ -118,7 +118,7 @@ func sendGitInfoContext(t *testing.T, gitInfoContext *xrayServices.XscGitInfoCon func initXrayScanTest(t *testing.T) (xrayServerPort int, xrayDetails auth.ServiceDetails, client *jfroghttpclient.JfrogHttpClient) { var err error initXrayTest(t) - xrayServerPort = xray.StartXrayMockServer() + xrayServerPort = xray.StartXrayMockServer(t) xrayDetails = GetXrayDetails() client, err = jfroghttpclient.JfrogClientBuilder(). SetClientCertPath(xrayDetails.GetClientCertPath()). diff --git a/tests/xraysummary_test.go b/tests/xraysummary_test.go index f5d7ea4f3..037bb9b77 100644 --- a/tests/xraysummary_test.go +++ b/tests/xraysummary_test.go @@ -17,7 +17,7 @@ var testsXraySummaryService *services.SummaryService func TestNewXraySummaryService(t *testing.T) { initXrayTest(t) - xrayServerPort := xray.StartXrayMockServer() + xrayServerPort := xray.StartXrayMockServer(t) xrayDetails := GetXrayDetails() client, err := jfroghttpclient.JfrogClientBuilder(). SetClientCertPath(xrayDetails.GetClientCertPath()).