|
| 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