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) + } +}