Skip to content

Commit f2fcb31

Browse files
fix: read response body in DownloadArtifact (#259)
Signed-off-by: puneeth_aditya_5656 <myakampuneeth@gmail.com>
1 parent f9f232c commit f2fcb31

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

pkg/connectors/microcks_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ func (c *microcksClient) DownloadArtifact(artifactURL string, mainArtifact bool,
522522
// Dump response if verbose required.
523523
config.DumpResponseIfRequired("Microcks for uploading artifact", resp, true)
524524

525-
respBody, err := io.ReadAll(req.Body)
525+
respBody, err := io.ReadAll(resp.Body)
526526
if err != nil {
527527
panic(err.Error())
528528
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package connectors
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"strings"
7+
"testing"
8+
)
9+
10+
func TestDownloadArtifactReturnsResponseBody(t *testing.T) {
11+
const expectedBody = "artifact downloaded"
12+
13+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
14+
if r.URL.Path != "/api/artifact/download" {
15+
t.Fatalf("unexpected path: %s", r.URL.Path)
16+
}
17+
if r.Method != http.MethodPost {
18+
t.Fatalf("unexpected method: %s", r.Method)
19+
}
20+
if err := r.ParseMultipartForm(1024); err != nil {
21+
t.Fatalf("failed to parse multipart form: %v", err)
22+
}
23+
if got := r.FormValue("url"); got != "https://example.com/openapi.yaml" {
24+
t.Fatalf("unexpected artifact url: %s", got)
25+
}
26+
if got := r.FormValue("mainArtifact"); got != "true" {
27+
t.Fatalf("unexpected mainArtifact value: %s", got)
28+
}
29+
w.WriteHeader(http.StatusCreated)
30+
_, _ = w.Write([]byte(expectedBody))
31+
}))
32+
defer server.Close()
33+
34+
client := NewMicrocksClient(server.URL)
35+
36+
msg, err := client.DownloadArtifact("https://example.com/openapi.yaml", true, "")
37+
if err != nil {
38+
t.Fatalf("DownloadArtifact returned error: %v", err)
39+
}
40+
if strings.TrimSpace(msg) != expectedBody {
41+
t.Fatalf("expected response body %q, got %q", expectedBody, msg)
42+
}
43+
}

0 commit comments

Comments
 (0)