-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
103 lines (80 loc) · 2.41 KB
/
client_test.go
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
package alphasoc
import (
"net/http"
"os"
"strings"
"testing"
"time"
)
func TestNew_WithoutOptions(t *testing.T) {
expectedAPIKey := "testAPIKey"
os.Setenv(APIKeyEnvVar, expectedAPIKey)
defer os.Unsetenv(APIKeyEnvVar)
c, err := New()
if err != nil {
t.Fatal("Error creating client, expected none")
}
if c.apiKey != expectedAPIKey {
t.Fatalf("Expected apiKey: %v, got: %v", expectedAPIKey, c.apiKey)
}
}
func TestNew_WithAPIKeyOption(t *testing.T) {
expectedAPIKey := "testAPIKey"
os.Setenv(APIKeyEnvVar, "differentAPIKey")
defer os.Unsetenv(APIKeyEnvVar)
c, err := New(WithAPIKey(expectedAPIKey))
if err != nil {
t.Fatal("Error creating client, expected none")
}
if c.apiKey != expectedAPIKey {
t.Fatalf("Expected apiKey: %v, got: %v", expectedAPIKey, c.apiKey)
}
}
func TestNew_WithAPIKeyOption_EmptyEnvVar(t *testing.T) {
expectedAPIKey := "testAPIKey"
c, err := New(WithAPIKey(expectedAPIKey))
if err != nil {
t.Fatal("Error creating client, expected none")
}
if c.apiKey != expectedAPIKey {
t.Fatalf("Expected apiKey: %v, got: %v", expectedAPIKey, c.apiKey)
}
}
func TestNew_WithHTTPClientOption(t *testing.T) {
apiKey := "testAPIKey"
expectedTimeout := 15 * time.Second
os.Setenv(APIKeyEnvVar, apiKey)
defer os.Unsetenv(APIKeyEnvVar)
c, err := New(WithHTTPClient(&http.Client{Timeout: expectedTimeout}))
if err != nil {
t.Fatal("Error creating client, expected none")
}
if c.client.Timeout != expectedTimeout {
t.Fatalf("Expected Timeout: %v, got: %v", expectedTimeout, c.client.Timeout)
}
}
func TestNew_WithMultipleOptions(t *testing.T) {
expectedAPIKey := "testAPIKey"
expectedTimeout := 15 * time.Second
os.Setenv(APIKeyEnvVar, "differentAPIKey")
defer os.Unsetenv(APIKeyEnvVar)
c, err := New(WithAPIKey(expectedAPIKey), WithHTTPClient(&http.Client{Timeout: expectedTimeout}))
if err != nil {
t.Fatal("Error creating client, expected none")
}
if c.apiKey != expectedAPIKey {
t.Fatalf("Expected apiKey: %v, got: %v", expectedAPIKey, c.apiKey)
}
if c.client.Timeout != expectedTimeout {
t.Fatalf("Expected Timeout: %v, got: %v", expectedTimeout, c.client.Timeout)
}
}
func TestNew_ReturnsErrorWhenNoApiKeyProvided(t *testing.T) {
_, err := New()
if err == nil {
t.Fatal("Expected error, got none")
}
if !strings.Contains(err.Error(), "apiKey cannot be empty") {
t.Fatalf("Expected error message should contain 'apiKey cannot be empty', got %v", err.Error())
}
}