-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrequest_test.go
280 lines (243 loc) · 6.72 KB
/
request_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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package coze
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// TestResponse 用于测试的响应结构
type TestResponse struct {
Data struct {
Name string `json:"name"`
} `json:"data"`
baseResponse
}
type TestReq struct {
Test string `json:"test"`
Data string `json:"data"`
}
func TestClient_Request_Success(t *testing.T) {
// 准备测试数据
expectedResp := &TestResponse{
Data: struct {
Name string `json:"name"`
}{
Name: "test",
},
}
respBody, _ := json.Marshal(expectedResp)
// 创建 mock 响应
mockResp := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(respBody)),
Header: make(http.Header),
}
mockResp.Header.Set(logIDHeader, "test-log-id")
// 创建测试客户端
core := newCore(&mockHTTP{
Response: mockResp,
Error: nil,
}, "https://api.test.com")
// 执行请求
var actualResp TestResponse
actualReq := &TestReq{
Test: "test",
Data: "data",
}
err := core.Request(context.Background(), http.MethodGet, "/test", actualReq, &actualResp, withHTTPQuery("test", "data"))
// 验证结果
assert.NoError(t, err)
assert.Equal(t, expectedResp.Code, actualResp.Code)
assert.Equal(t, expectedResp.Data.Name, actualResp.Data.Name)
assert.Equal(t, "test-log-id", actualResp.HTTPResponse.LogID())
}
func TestClient_Request_Error(t *testing.T) {
// 测试 HTTP 错误
t.Run("HTTP Error", func(t *testing.T) {
core := newCore(&mockHTTP{
Response: nil,
Error: errors.New("network error"),
}, "https://api.test.com")
var resp TestResponse
err := core.Request(context.Background(), http.MethodGet, "/test", nil, &resp)
assert.Error(t, err)
assert.Contains(t, err.Error(), "network error")
})
// 测试业务错误
t.Run("Business Error", func(t *testing.T) {
errorResp := &TestResponse{}
errorResp.Code = 1001
errorResp.Msg = "business error"
respBody, _ := json.Marshal(errorResp)
mockResp := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(respBody)),
Header: make(http.Header),
}
mockResp.Header.Set(logIDHeader, "test-log-id")
core := newCore(&mockHTTP{
Response: mockResp,
Error: nil,
}, "https://api.test.com")
var resp TestResponse
err := core.Request(context.Background(), http.MethodGet, "/test", nil, &resp)
assert.Error(t, err)
cozeErr, ok := err.(*Error)
assert.True(t, ok)
assert.Equal(t, 1001, cozeErr.Code)
assert.Equal(t, "business error", cozeErr.Message)
})
// 测试认证错误
t.Run("Auth Error", func(t *testing.T) {
errorResp := &authErrorFormat{
ErrorCode: "invalid_token",
ErrorMessage: "Token is invalid",
}
respBody, _ := json.Marshal(errorResp)
mockResp := &http.Response{
StatusCode: http.StatusUnauthorized,
Body: io.NopCloser(bytes.NewReader(respBody)),
Header: make(http.Header),
}
mockResp.Header.Set(logIDHeader, "test-log-id")
core := newCore(&mockHTTP{
Response: mockResp,
Error: nil,
}, "https://api.test.com")
var resp TestResponse
err := core.Request(context.Background(), http.MethodGet, "/test", nil, &resp)
assert.Error(t, err)
authErr, ok := err.(*AuthError)
assert.True(t, ok)
assert.Equal(t, "invalid_token", authErr.Code.String())
assert.Equal(t, "Token is invalid", authErr.ErrorMessage)
})
}
func TestClient_UploadFile_Success(t *testing.T) {
// 准备测试数据
expectedResp := &TestResponse{
Data: struct {
Name string `json:"name"`
}{
Name: "uploaded.txt",
},
}
respBody, _ := json.Marshal(expectedResp)
mockResp := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(respBody)),
Header: make(http.Header),
}
mockResp.Header.Set(logIDHeader, "test-log-id")
core := newCore(&mockHTTP{
Response: mockResp,
Error: nil,
}, "https://api.test.com")
// 创建测试文件内容
fileContent := "test file content"
fields := map[string]string{
"field1": "value1",
"field2": "value2",
}
var actualResp TestResponse
err := core.UploadFile(
context.Background(),
"/upload",
strings.NewReader(fileContent),
"test.txt",
fields,
&actualResp,
withHTTPHeader("test", "header-value"),
)
assert.NoError(t, err)
assert.Equal(t, expectedResp.Code, actualResp.Code)
assert.Equal(t, expectedResp.Data.Name, actualResp.Data.Name)
assert.Equal(t, "test-log-id", actualResp.HTTPResponse.LogID())
}
func TestClient_UploadFile_Error(t *testing.T) {
// 测试上传错误
t.Run("Upload Error", func(t *testing.T) {
core := newCore(&mockHTTP{
Response: nil,
Error: errors.New("upload failed"),
}, "https://api.test.com")
var resp TestResponse
err := core.UploadFile(
context.Background(),
"/upload",
strings.NewReader("test"),
"test.txt",
nil,
&resp,
)
assert.Error(t, err)
assert.Contains(t, err.Error(), "upload failed")
})
// 测试业务错误
t.Run("Business Error", func(t *testing.T) {
errorResp := &TestResponse{}
errorResp.Code = 1002
errorResp.Msg = "upload business error"
respBody, _ := json.Marshal(errorResp)
mockResp := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(respBody)),
Header: make(http.Header),
}
mockResp.Header.Set(logIDHeader, "test-log-id")
core := newCore(&mockHTTP{
Response: mockResp,
Error: nil,
}, "https://api.test.com")
var resp TestResponse
err := core.UploadFile(
context.Background(),
"/upload",
strings.NewReader("test"),
"test.txt",
nil,
&resp,
)
assert.Error(t, err)
cozeErr, ok := err.(*Error)
assert.True(t, ok)
assert.Equal(t, 1002, cozeErr.Code)
assert.Equal(t, "upload business error", cozeErr.Message)
})
}
func TestRequestOptions(t *testing.T) {
// 测试请求选项
t.Run("withHTTPHeader", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "https://api.test.com", nil)
opt := withHTTPHeader("X-Test", "test-value")
err := opt(req)
assert.NoError(t, err)
assert.Equal(t, "test-value", req.Header.Get("X-Test"))
})
t.Run("withHTTPQuery", func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, "https://api.test.com", nil)
opt := withHTTPQuery("param", "value")
err := opt(req)
assert.NoError(t, err)
assert.Equal(t, "value", req.URL.Query().Get("param"))
})
}
func TestNewClient(t *testing.T) {
// 测试创建客户端
t.Run("With Custom Doer", func(t *testing.T) {
customDoer := &mockHTTP{}
core := newCore(customDoer, "https://api.test.com")
assert.Equal(t, customDoer, core.httpClient)
})
t.Run("With Nil Doer", func(t *testing.T) {
core := newCore(nil, "https://api.test.com")
assert.NotNil(t, core.httpClient)
_, ok := core.httpClient.(*http.Client)
assert.True(t, ok)
})
}