-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathmicrocks_client_test.go
More file actions
94 lines (83 loc) · 2.86 KB
/
microcks_client_test.go
File metadata and controls
94 lines (83 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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)
}
}
func TestDeleteServiceReturnsNoErrorOnSuccess(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && r.URL.Path == "/api/services/search" {
if got := r.URL.Query().Get("name"); got != "Simple" {
t.Fatalf("unexpected service name: %s", got)
}
if got := r.URL.Query().Get("version"); got != "1.1" {
t.Fatalf("unexpected service version: %s", got)
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`[{"id": "test-id-123", "name": "Simple", "version": "1.1"}]`))
return
}
if r.Method == http.MethodDelete && r.URL.Path == "/api/services/test-id-123" {
w.WriteHeader(http.StatusNoContent)
return
}
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
}))
defer server.Close()
client := NewMicrocksClient(server.URL)
err := client.DeleteService("Simple", "1.1")
if err != nil {
t.Fatalf("DeleteService returned error: %v", err)
}
}
func TestDeleteServiceReturnsErrorOnNotFound(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && r.URL.Path == "/api/services/search" {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`[]`))
return
}
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
}))
defer server.Close()
client := NewMicrocksClient(server.URL)
err := client.DeleteService("Simple", "1.1")
if err == nil {
t.Fatalf("expected DeleteService to return error on not found, got nil")
}
if !strings.Contains(err.Error(), "not found") {
t.Fatalf("expected error to contain 'not found', got: %v", err)
}
}