Repository navigation
Expand file tree
/
Copy pathclient_test.go
More file actions
110 lines (105 loc) · 3.92 KB
/
Copy pathclient_test.go
File metadata and controls
110 lines (105 loc) · 3.92 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package infrahub
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
)
func TestNewClientValidation(t *testing.T) {
t.Parallel()
tests := []struct {
name string
address string
options []Option
}{
{name: "relative address", address: "/infrahub"},
{name: "unsupported scheme", address: "ftp://example.com"},
{name: "nil option", address: "https://example.com", options: []Option{nil}},
{name: "nil HTTP client", address: "https://example.com", options: []Option{WithHTTPClient(nil)}},
{name: "empty branch", address: "https://example.com", options: []Option{WithDefaultBranch("")}},
{name: "empty user agent", address: "https://example.com", options: []Option{WithUserAgent("")}},
{name: "empty header name", address: "https://example.com", options: []Option{WithHeader("", "value")}},
{name: "auth header", address: "https://example.com", options: []Option{WithHeader("Authorization", "secret")}},
{name: "Infrahub auth header", address: "https://example.com", options: []Option{WithHeader("X-INFRAHUB-KEY", "secret")}},
{name: "invalid limit", address: "https://example.com", options: []Option{WithMaxResponseBytes(0)}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if _, err := NewClient(tt.address, tt.options...); err == nil {
t.Fatal("NewClient() error = nil")
}
})
}
}
func TestNewClientWiresServices(t *testing.T) {
t.Parallel()
client, err := NewClient("https://example.com")
if err != nil {
t.Fatal(err)
}
if client.Automation == nil || client.Branches == nil || client.Diffs == nil || client.Nodes == nil || client.ObjectStore == nil || client.Repositories == nil || client.ResourcePools == nil || client.Schema == nil || client.Tasks == nil || client.Telemetry == nil || client.Traversal == nil {
t.Fatalf("services not initialized: %#v", client)
}
}
func TestClientOptionsAndCompatibilityHelpers(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("User-Agent") != "custom-agent" || r.Header.Get("X-Custom") != "one" || r.Header.Get("X-INFRAHUB-KEY") != "secret" {
t.Errorf("headers = %#v", r.Header)
}
if r.Header.Get("X-Infrahub-Tracker") != "workflow" {
t.Errorf("tracker = %q", r.Header.Get("X-Infrahub-Tracker"))
}
_, _ = w.Write([]byte(`{"data":{"ok":true}}`))
}))
defer server.Close()
client, err := NewClient(server.URL,
WithHTTPClient(server.Client()),
WithAPIToken("secret"),
WithDefaultBranch("develop"),
WithUserAgent("custom-agent"),
WithHeader("X-Custom", "one"),
WithMaxResponseBytes(1024),
)
if err != nil {
t.Fatal(err)
}
if client.DefaultBranch() != "develop" {
t.Fatalf("default branch = %q", client.DefaultBranch())
}
if err := client.Execute(WithTracker(context.Background(), "workflow"), GraphQLRequest{Query: "query { ok }"}, nil); err != nil {
t.Fatal(err)
}
group, err := NewTrackingGroup(TrackingGroupOptions{Identifier: "workflow"})
if err != nil || group.Name() != "workflow" {
t.Fatalf("group=%#v err=%v", group, err)
}
}
func TestDefaultClientRefusesRedirects(t *testing.T) {
t.Parallel()
var targetCalled atomic.Bool
target := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
targetCalled.Store(true)
}))
defer target.Close()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Location", target.URL)
w.WriteHeader(http.StatusTemporaryRedirect)
}))
defer server.Close()
client, err := NewClient(server.URL, WithAPIToken("do-not-forward"))
if err != nil {
t.Fatal(err)
}
err = client.Execute(context.Background(), GraphQLRequest{Query: "query { x }"}, nil)
var httpErr *HTTPError
if !errors.As(err, &httpErr) || httpErr.StatusCode != http.StatusTemporaryRedirect {
t.Fatalf("error = %T %v", err, err)
}
if targetCalled.Load() {
t.Fatal("redirect target received a request")
}
}