-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
266 lines (227 loc) · 6.57 KB
/
client_test.go
File metadata and controls
266 lines (227 loc) · 6.57 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
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
package disk
import (
"context"
"io"
"net"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
)
type testTransport struct {
server *httptest.Server
}
func (t *testTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// Create a new request to the test server
testURL := "http://" + t.server.Listener.Addr().String() + req.URL.Path
if req.URL.RawQuery != "" {
testURL += "?" + req.URL.RawQuery
}
testReq, err := http.NewRequest(req.Method, testURL, req.Body)
if err != nil {
return nil, err
}
// Copy headers
testReq.Header = req.Header.Clone()
return http.DefaultClient.Do(testReq)
}
func mockedHttpClient(h http.HandlerFunc) *Client {
s := httptest.NewServer(h)
client, _ := New("token")
client.HTTPClient = &http.Client{
Transport: &testTransport{server: s},
}
return client
}
func testingHTTPClient(handler http.Handler) (*http.Client, func()) {
s := httptest.NewServer(handler)
client := &http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, network, _ string) (net.Conn, error) {
return net.Dial(network, s.Listener.Addr().String())
},
},
}
return client, s.Close
}
func TestNew(t *testing.T) {
// Helper function to reset environment variable
resetEnv := func() {
os.Unsetenv("YANDEX_DISK_ACCESS_TOKEN")
}
t.Run("With provided token", func(t *testing.T) {
resetEnv()
client, err := New("test-token")
if err != nil {
t.Fatal("Expected no error, got:", err)
}
if client == nil {
t.Fatal("Expected non-nil client")
}
if client.AccessToken != "test-token" {
t.Errorf("Expected AccessToken to be 'test-token', got '%s'", client.AccessToken)
}
if client.HTTPClient == nil {
t.Fatal("Expected non-nil HTTPClient")
}
if client.HTTPClient.Timeout != 30*time.Second {
t.Errorf("Expected Timeout to be 30 seconds, got %v", client.HTTPClient.Timeout)
}
})
t.Run("With environment variable", func(t *testing.T) {
resetEnv()
os.Setenv("YANDEX_DISK_ACCESS_TOKEN", "env-token")
client, err := New()
if err != nil {
t.Fatal("Expected no error, got:", err)
}
if client == nil {
t.Fatal("Expected non-nil client")
}
if client.AccessToken != "env-token" {
t.Errorf("Expected AccessToken to be 'env-token', got '%s'", client.AccessToken)
}
})
t.Run("Without token and empty environment variable", func(t *testing.T) {
resetEnv()
client, err := New()
if err == nil {
t.Fatal("Expected error for missing token")
}
if client != nil {
t.Fatal("Expected nil client")
}
})
t.Run("With multiple tokens", func(t *testing.T) {
resetEnv()
client, err := New("token1", "token2")
if err != nil {
t.Fatal("Expected no error, got:", err)
}
if client == nil {
t.Fatal("Expected non-nil client")
}
if client.AccessToken != "token1" {
t.Errorf("Expected AccessToken to be 'token1', got '%s'", client.AccessToken)
}
})
t.Run("HTTPClient configuration", func(t *testing.T) {
resetEnv()
client, err := New("test-token")
if err != nil {
t.Fatal("Expected no error, got:", err)
}
if client == nil {
t.Fatal("Expected non-nil client")
}
if client.HTTPClient == nil {
t.Fatal("Expected non-nil HTTPClient")
}
if client.HTTPClient.Timeout != 30*time.Second {
t.Errorf("Expected Timeout to be 30 seconds, got %v", client.HTTPClient.Timeout)
}
})
}
func TestClientHelperMethods(t *testing.T) {
t.Run("WithDeadline", func(t *testing.T) {
deadline := time.Now().Add(5 * time.Second)
ctx, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
actualDeadline, ok := ctx.Deadline()
if !ok {
t.Error("Expected deadline to be set")
}
if !actualDeadline.Equal(deadline) {
t.Errorf("Expected deadline %v, got %v", deadline, actualDeadline)
}
})
t.Run("GetTimeout basic test", func(t *testing.T) {
client, _ := New("test-token")
timeout := client.GetTimeout()
if timeout != 30*time.Second {
t.Errorf("Expected timeout 30s, got %v", timeout)
}
})
t.Run("SetTimeout edge cases", func(t *testing.T) {
client, _ := New("test-token")
// Test normal case
client.SetTimeout(5 * time.Second)
if client.HTTPClient.Timeout != 5*time.Second {
t.Errorf("Expected HTTPClient timeout 5s, got %v", client.HTTPClient.Timeout)
}
})
t.Run("safeDecodeJSON with various inputs", func(t *testing.T) {
client, _ := New("test-token")
// Test with valid JSON
validJSON := `{"name": "test", "value": 123}`
resp := &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(validJSON)),
}
var result map[string]interface{}
err := client.safeDecodeJSON(resp, &result)
if err != nil {
t.Errorf("Expected no error for valid JSON, got: %v", err)
}
if result["name"] != "test" {
t.Errorf("Expected name 'test', got %v", result["name"])
}
// Test with invalid JSON
invalidJSON := `{invalid json}`
resp = &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(invalidJSON)),
}
var invalidResult map[string]interface{}
err = client.safeDecodeJSON(resp, &invalidResult)
if err == nil {
t.Error("Expected error for invalid JSON")
}
// Test with response with nil body
respWithNilBody := &http.Response{
StatusCode: 200,
Body: nil,
}
err = client.safeDecodeJSON(respWithNilBody, &result)
if err == nil {
t.Error("Expected error for nil response body")
}
})
t.Run("handleResponse with different status codes", func(t *testing.T) {
client, _ := New("test-token")
// Test with acceptable status code
resp := &http.Response{
StatusCode: 200,
Status: "200 OK",
Body: io.NopCloser(strings.NewReader(`{"result": "success"}`)),
}
result, err := client.handleResponse(resp, []int{200, 201})
if err != nil {
t.Errorf("Expected no error for acceptable status code, got: %v", err)
}
// Result can be nil for some responses, which is OK
_ = result
// Test with unacceptable status code - error response
resp = &http.Response{
StatusCode: 404,
Status: "404 Not Found",
Body: io.NopCloser(strings.NewReader(`{"error": "NotFoundError", "description": "Resource not found"}`)),
}
_, err = client.handleResponse(resp, []int{200})
if err == nil {
t.Error("Expected error for unacceptable status code")
}
// Test with unacceptable status code - invalid error JSON
resp = &http.Response{
StatusCode: 500,
Status: "500 Internal Server Error",
Body: io.NopCloser(strings.NewReader(`{invalid error json}`)),
}
_, err = client.handleResponse(resp, []int{200})
if err == nil {
t.Error("Expected error for invalid error JSON")
}
})
}