-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_test.go
144 lines (118 loc) · 3.49 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
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
package coze
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// mockAuth implements Auth interface for testing
type mockAuth struct {
token string
err error
}
func (m *mockAuth) Token(ctx context.Context) (string, error) {
return m.token, m.err
}
func TestNewCozeAPI(t *testing.T) {
// Test default initialization
t.Run("default initialization", func(t *testing.T) {
auth := &mockAuth{token: "test_token"}
api := NewCozeAPI(auth)
assert.Equal(t, ComBaseURL, api.baseURL)
assert.NotNil(t, api.Audio)
assert.NotNil(t, api.Bots)
assert.NotNil(t, api.Chat)
assert.NotNil(t, api.Conversations)
assert.NotNil(t, api.Workflows)
assert.NotNil(t, api.Workspaces)
assert.NotNil(t, api.Datasets)
assert.NotNil(t, api.Files)
})
// Test with custom base URL
t.Run("custom base URL", func(t *testing.T) {
auth := &mockAuth{token: "test_token"}
customURL := "https://custom.api.coze.com"
api := NewCozeAPI(auth, WithBaseURL(customURL))
assert.Equal(t, customURL, api.baseURL)
})
// Test with custom HTTP core
t.Run("custom HTTP core", func(t *testing.T) {
auth := &mockAuth{token: "test_token"}
customClient := &http.Client{
Timeout: 30,
}
api := NewCozeAPI(auth, WithHttpClient(customClient))
assert.NotNil(t, api)
})
// Test with custom log level
t.Run("custom log level", func(t *testing.T) {
auth := &mockAuth{token: "test_token"}
api := NewCozeAPI(auth, WithLogLevel(LogLevelDebug))
assert.NotNil(t, api)
})
// Test with custom logger
t.Run("custom logger", func(t *testing.T) {
auth := &mockAuth{token: "test_token"}
customLogger := &mockLogger{}
api := NewCozeAPI(auth, WithLogger(customLogger))
assert.NotNil(t, api)
})
// Test with multiple options
t.Run("multiple options", func(t *testing.T) {
auth := &mockAuth{token: "test_token"}
customURL := "https://custom.api.coze.com"
customClient := &http.Client{
Timeout: 30,
}
customLogger := &mockLogger{}
api := NewCozeAPI(auth,
WithBaseURL(customURL),
WithHttpClient(customClient),
WithLogLevel(LogLevelDebug),
WithLogger(customLogger),
)
assert.Equal(t, customURL, api.baseURL)
assert.NotNil(t, api)
})
}
func TestAuthTransport(t *testing.T) {
// Test successful authentication
t.Run("successful authentication", func(t *testing.T) {
auth := &mockAuth{token: "test_token"}
transport := &authTransport{
auth: auth,
next: &mockTransport{
roundTripFunc: func(req *http.Request) (*http.Response, error) {
// Verify authorization header
assert.Equal(t, "Bearer test_token", req.Header.Get("Authorization"))
return &http.Response{StatusCode: http.StatusOK}, nil
},
},
}
req, _ := http.NewRequest(http.MethodGet, ComBaseURL, nil)
resp, err := transport.RoundTrip(req)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
})
// Test authentication error
t.Run("authentication error", func(t *testing.T) {
auth := &mockAuth{
token: "",
err: assert.AnError,
}
transport := &authTransport{
auth: auth,
next: http.DefaultTransport,
}
req, _ := http.NewRequest(http.MethodGet, ComBaseURL, nil)
resp, err := transport.RoundTrip(req)
require.Error(t, err)
assert.Nil(t, resp)
})
}
// mockLogger implements log.Logger interface for testing
type mockLogger struct{}
func (m *mockLogger) Log(ctx context.Context, level LogLevel, message string, args ...interface{}) {
}
func (m *mockLogger) Errorf(format string, args ...interface{}) {}