From c219ca00eb6f9f07e21893766184c07db2c133c2 Mon Sep 17 00:00:00 2001 From: puneeth_aditya_5656 Date: Sat, 2 May 2026 02:05:24 +0530 Subject: [PATCH] fix: read response body in DownloadArtifact Signed-off-by: puneeth_aditya_5656 --- pkg/connectors/microcks_client.go | 2 +- pkg/connectors/microcks_client_test.go | 43 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 pkg/connectors/microcks_client_test.go diff --git a/pkg/connectors/microcks_client.go b/pkg/connectors/microcks_client.go index cabb85f..00fba3c 100644 --- a/pkg/connectors/microcks_client.go +++ b/pkg/connectors/microcks_client.go @@ -520,7 +520,7 @@ func (c *microcksClient) DownloadArtifact(artifactURL string, mainArtifact bool, // Dump response if verbose required. config.DumpResponseIfRequired("Microcks for uploading artifact", resp, true) - respBody, err := io.ReadAll(req.Body) + respBody, err := io.ReadAll(resp.Body) if err != nil { panic(err.Error()) } diff --git a/pkg/connectors/microcks_client_test.go b/pkg/connectors/microcks_client_test.go new file mode 100644 index 0000000..9e9b6c8 --- /dev/null +++ b/pkg/connectors/microcks_client_test.go @@ -0,0 +1,43 @@ +package connectors + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestDownloadArtifactReturnsResponseBody(t *testing.T) { + const expectedBody = "artifact downloaded" + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/artifact/download" { + t.Fatalf("unexpected path: %s", r.URL.Path) + } + if r.Method != http.MethodPost { + t.Fatalf("unexpected method: %s", r.Method) + } + if err := r.ParseMultipartForm(1024); err != nil { + t.Fatalf("failed to parse multipart form: %v", err) + } + if got := r.FormValue("url"); got != "https://example.com/openapi.yaml" { + t.Fatalf("unexpected artifact url: %s", got) + } + if got := r.FormValue("mainArtifact"); got != "true" { + t.Fatalf("unexpected mainArtifact value: %s", got) + } + w.WriteHeader(http.StatusCreated) + _, _ = w.Write([]byte(expectedBody)) + })) + defer server.Close() + + client := NewMicrocksClient(server.URL) + + msg, err := client.DownloadArtifact("https://example.com/openapi.yaml", true, "") + if err != nil { + t.Fatalf("DownloadArtifact returned error: %v", err) + } + if strings.TrimSpace(msg) != expectedBody { + t.Fatalf("expected response body %q, got %q", expectedBody, msg) + } +}