-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudlayer_test.go
More file actions
141 lines (128 loc) · 3.11 KB
/
cloudlayer_test.go
File metadata and controls
141 lines (128 loc) · 3.11 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package cloudlayer
import (
"errors"
"net/http"
"testing"
"time"
)
func TestNewClient_Valid(t *testing.T) {
c, err := NewClient("test-key", V2)
if err != nil {
t.Fatalf("NewClient() error = %v", err)
}
if c.apiKey != "test-key" {
t.Errorf("apiKey = %q, want test-key", c.apiKey)
}
if c.apiVersion != V2 {
t.Errorf("apiVersion = %q, want v2", c.apiVersion)
}
if c.baseURL != defaultBaseURL {
t.Errorf("baseURL = %q, want %q", c.baseURL, defaultBaseURL)
}
if c.maxRetries != 2 {
t.Errorf("maxRetries = %d, want 2", c.maxRetries)
}
}
func TestNewClient_EmptyAPIKey(t *testing.T) {
_, err := NewClient("", V2)
if err == nil {
t.Fatal("expected error for empty API key")
}
var cfgErr *ConfigError
if !errors.As(err, &cfgErr) {
t.Errorf("expected *ConfigError, got %T", err)
}
}
func TestNewClient_InvalidAPIVersion(t *testing.T) {
_, err := NewClient("key", APIVersion("v3"))
if err == nil {
t.Fatal("expected error for invalid API version")
}
var cfgErr *ConfigError
if !errors.As(err, &cfgErr) {
t.Errorf("expected *ConfigError, got %T", err)
}
}
func TestNewClient_V1(t *testing.T) {
c, err := NewClient("key", V1)
if err != nil {
t.Fatal(err)
}
if c.apiVersion != V1 {
t.Errorf("apiVersion = %q, want v1", c.apiVersion)
}
}
func TestNewClient_V2(t *testing.T) {
c, err := NewClient("key", V2)
if err != nil {
t.Fatal(err)
}
if c.apiVersion != V2 {
t.Errorf("apiVersion = %q, want v2", c.apiVersion)
}
}
func TestNewClient_WithBaseURL(t *testing.T) {
c, err := NewClient("key", V2, WithBaseURL("https://custom.api.com"))
if err != nil {
t.Fatal(err)
}
if c.baseURL != "https://custom.api.com" {
t.Errorf("baseURL = %q", c.baseURL)
}
}
func TestNewClient_WithTimeout(t *testing.T) {
c, err := NewClient("key", V2, WithTimeout(10*time.Second))
if err != nil {
t.Fatal(err)
}
if c.httpClient.Timeout != 10*time.Second {
t.Errorf("timeout = %v", c.httpClient.Timeout)
}
}
func TestNewClient_WithMaxRetries_Zero(t *testing.T) {
c, err := NewClient("key", V2, WithMaxRetries(0))
if err != nil {
t.Fatal(err)
}
if c.maxRetries != 0 {
t.Errorf("maxRetries = %d, want 0", c.maxRetries)
}
}
func TestNewClient_WithMaxRetries_ClampedToFive(t *testing.T) {
c, err := NewClient("key", V2, WithMaxRetries(10))
if err != nil {
t.Fatal(err)
}
if c.maxRetries != 5 {
t.Errorf("maxRetries = %d, want 5", c.maxRetries)
}
}
func TestNewClient_WithHTTPClient(t *testing.T) {
custom := &http.Client{Timeout: 60 * time.Second}
c, err := NewClient("key", V2, WithHTTPClient(custom))
if err != nil {
t.Fatal(err)
}
if c.httpClient != custom {
t.Error("httpClient not set to custom client")
}
}
func TestNewClient_WithHeaders(t *testing.T) {
headers := map[string]string{"X-Custom": "test"}
c, err := NewClient("key", V2, WithHeaders(headers))
if err != nil {
t.Fatal(err)
}
if c.customHeaders["X-Custom"] != "test" {
t.Error("custom headers not set")
}
}
func TestNewClient_WithUserAgent(t *testing.T) {
c, err := NewClient("key", V2, WithUserAgent("my-app/1.0"))
if err != nil {
t.Fatal(err)
}
if c.userAgent != "my-app/1.0" {
t.Errorf("userAgent = %q", c.userAgent)
}
}