-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_test.go
More file actions
212 lines (192 loc) · 4.94 KB
/
client_test.go
File metadata and controls
212 lines (192 loc) · 4.94 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"testing"
)
// Sample valid CA certificate for testing (self-signed)
const testCACert = `-----BEGIN CERTIFICATE-----
MIIBkTCB+wIJAKHBfpFoSdLGMA0GCSqGSIb3DQEBCwUAMBExDzANBgNVBAMMBnRl
c3RjYTAeFw0yMzAxMDEwMDAwMDBaFw0yNDAxMDEwMDAwMDBaMBExDzANBgNVBAMM
BnRlc3RjYTBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQC5Q5mJKL8nKL8nKL8nKL8n
KL8nKL8nKL8nKL8nKL8nKL8nKL8nKL8nKL8nKL8nKL8nKL8nKL8nKL8nKL8nKL8n
AgMBAAEwDQYJKoZIhvcNAQELBQADQQBRvYFpCgK/G8K/G8K/G8K/G8K/G8K/G8K/
G8K/G8K/G8K/G8K/G8K/G8K/G8K/G8K/G8K/G8K/G8K/G8K/G8K/G8K/
-----END CERTIFICATE-----`
func TestNewClient(t *testing.T) {
tests := []struct {
name string
config *Config
expectError bool
}{
{
name: "Default OpenAI config",
config: &Config{
BaseURL: "https://api.openai.com/v1",
APIKey: "test-key",
SkipSSLVerify: false,
},
expectError: false,
},
{
name: "Custom base URL",
config: &Config{
BaseURL: "http://localhost:8080/v1",
APIKey: "test-key",
SkipSSLVerify: false,
},
expectError: false,
},
{
name: "Skip SSL verification",
config: &Config{
BaseURL: "https://api.openai.com/v1",
APIKey: "test-key",
SkipSSLVerify: true,
},
expectError: false,
},
{
name: "With invalid CA certificate",
config: &Config{
BaseURL: "https://api.openai.com/v1",
APIKey: "test-key",
CACert: "invalid-cert",
SkipSSLVerify: false,
},
expectError: true,
},
{
name: "Skip SSL with CA certificate",
config: &Config{
BaseURL: "https://api.openai.com/v1",
APIKey: "test-key",
CACert: testCACert,
SkipSSLVerify: true,
},
expectError: true, // Invalid test cert will fail
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, err := NewClient(tt.config)
if tt.expectError {
if err == nil {
t.Error("expected error, got nil")
}
return
}
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
if client == nil {
t.Error("expected client to be created, got nil")
}
})
}
}
func TestCreateHTTPClient(t *testing.T) {
tests := []struct {
name string
caCert string
skipSSLVerify bool
customHeaders map[string]string
expectError bool
}{
{
name: "Default config with default headers",
caCert: "",
skipSSLVerify: false,
customHeaders: nil,
expectError: false,
},
{
name: "Skip SSL only",
caCert: "",
skipSSLVerify: true,
customHeaders: nil,
expectError: false,
},
{
name: "Invalid CA certificate",
caCert: "invalid-cert",
skipSSLVerify: false,
customHeaders: nil,
expectError: true,
},
{
name: "Invalid CA certificate with skip SSL",
caCert: "invalid-cert",
skipSSLVerify: true,
customHeaders: nil,
expectError: true,
},
{
name: "Custom headers only",
caCert: "",
skipSSLVerify: false,
customHeaders: map[string]string{"X-Custom": "value"},
expectError: false,
},
{
name: "Custom headers with skip SSL",
caCert: "",
skipSSLVerify: true,
customHeaders: map[string]string{"X-Request-ID": "test123"},
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, err := createHTTPClient(tt.caCert, tt.skipSSLVerify, tt.customHeaders)
if tt.expectError {
if err == nil {
t.Error("expected error, got nil")
}
return
}
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
// Client should always be non-nil (default headers are always added)
if client == nil {
t.Error("expected non-nil client, got nil")
return
}
if client.Transport == nil {
t.Error("expected transport to be set, got nil")
}
})
}
}
func TestGetDefaultHeaders(t *testing.T) {
headers := getDefaultHeaders()
if headers["User-Agent"] == "" {
t.Error("expected User-Agent header to be set")
}
if headers["X-Action-Name"] != ActionName {
t.Errorf("expected X-Action-Name to be %s, got %s", ActionName, headers["X-Action-Name"])
}
if headers["X-Action-Version"] == "" {
t.Error("expected X-Action-Version header to be set")
}
}
func TestMergeHeaders(t *testing.T) {
customHeaders := map[string]string{
"X-Custom": "value",
"User-Agent": "custom-agent", // Override default
}
merged := mergeHeaders(customHeaders)
// Custom headers should be present
if merged["X-Custom"] != "value" {
t.Errorf("expected X-Custom to be 'value', got '%s'", merged["X-Custom"])
}
// Custom User-Agent should override default
if merged["User-Agent"] != "custom-agent" {
t.Errorf("expected User-Agent to be 'custom-agent', got '%s'", merged["User-Agent"])
}
// Default headers should still be present
if merged["X-Action-Name"] != ActionName {
t.Errorf("expected X-Action-Name to be %s, got %s", ActionName, merged["X-Action-Name"])
}
}