-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_test.go
More file actions
413 lines (350 loc) · 12 KB
/
upload_test.go
File metadata and controls
413 lines (350 loc) · 12 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package disk
import (
"context"
"io"
"net/http"
"os"
"strings"
"testing"
)
func TestUploadFileFromPath(t *testing.T) {
t.Run("UploadFileFromPath function exists and validates input", func(t *testing.T) {
client, _ := New("test-token")
// Test that the method exists and can be called
// We expect it to fail since we don't have a real token, but we're just testing the method exists
_, err := client.UploadFileFromPath(context.Background(), "", "/test/upload.txt", nil)
if err == nil {
t.Error("Expected error for empty local path")
}
if !strings.Contains(err.Error(), "local path cannot be empty") {
t.Errorf("Expected 'local path cannot be empty' error, got: %s", err.Error())
}
})
t.Run("UploadFileFromPath with empty local path", func(t *testing.T) {
client, _ := New("test-token")
_, err := client.UploadFileFromPath(context.Background(), "", "/test/upload.txt", nil)
if err == nil {
t.Error("Expected error for empty local path")
}
if !strings.Contains(err.Error(), "local path cannot be empty") {
t.Errorf("Expected 'local path cannot be empty' error, got: %s", err.Error())
}
})
t.Run("UploadFileFromPath with empty remote path", func(t *testing.T) {
client, _ := New("test-token")
tmpFile, err := os.CreateTemp("", "upload_test_*.txt")
if err != nil {
t.Fatal("Failed to create temp file:", err)
}
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()
_, err = client.UploadFileFromPath(context.Background(), tmpFile.Name(), "", nil)
if err == nil {
t.Error("Expected error for empty remote path")
}
if !strings.Contains(err.Error(), "remote path cannot be empty") {
t.Errorf("Expected 'remote path cannot be empty' error, got: %s", err.Error())
}
})
t.Run("UploadFileFromPath with non-existent file", func(t *testing.T) {
client, _ := New("test-token")
_, err := client.UploadFileFromPath(context.Background(), "/non/existent/file.txt", "/test/upload.txt", nil)
if err == nil {
t.Error("Expected error for non-existent file")
}
if !strings.Contains(err.Error(), "file does not exist") {
t.Errorf("Expected 'file does not exist' error, got: %s", err.Error())
}
})
t.Run("UploadFileFromPath with directory instead of file", func(t *testing.T) {
client, _ := New("test-token")
tmpDir, err := os.MkdirTemp("", "upload_test_dir_*")
if err != nil {
t.Fatal("Failed to create temp dir:", err)
}
defer os.RemoveAll(tmpDir)
_, err = client.UploadFileFromPath(context.Background(), tmpDir, "/test/upload.txt", nil)
if err == nil {
t.Error("Expected error for directory path")
}
if !strings.Contains(err.Error(), "path is a directory") {
t.Errorf("Expected 'path is a directory' error, got: %s", err.Error())
}
})
t.Run("UploadFileFromPath progress callback validation", func(t *testing.T) {
client, _ := New("test-token")
// Test that progress callback is properly accepted and validated
var progressUpdates []UploadProgress
progressCallback := func(progress UploadProgress) {
progressUpdates = append(progressUpdates, progress)
}
options := &UploadOptions{
Progress: progressCallback,
}
// This will fail at API call level, but we're testing that the option is accepted
_, err := client.UploadFileFromPath(context.Background(), "/non/existent/file.txt", "/test/upload.txt", options)
if err == nil {
t.Error("Expected error for non-existent file")
}
// Verify the error is about file validation, not about progress callback
if !strings.Contains(err.Error(), "file does not exist") {
t.Errorf("Expected file validation error, got: %s", err.Error())
}
})
}
func TestValidateLocalFile(t *testing.T) {
t.Run("ValidateLocalFile with valid file", func(t *testing.T) {
client, _ := New("test-token")
tmpFile, err := os.CreateTemp("", "validate_test_*.txt")
if err != nil {
t.Fatal("Failed to create temp file:", err)
}
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()
tmpFile.WriteString("test content")
tmpFile.Sync()
fileInfo, err := client.validateLocalFile(tmpFile.Name())
if err != nil {
t.Fatal("Expected no error, got:", err)
}
if fileInfo.Size() <= 0 {
t.Error("Expected file size > 0")
}
})
t.Run("ValidateLocalFile with empty file", func(t *testing.T) {
client, _ := New("test-token")
tmpFile, err := os.CreateTemp("", "validate_empty_test_*.txt")
if err != nil {
t.Fatal("Failed to create temp file:", err)
}
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()
_, err = client.validateLocalFile(tmpFile.Name())
if err == nil {
t.Error("Expected error for empty file")
}
if !strings.Contains(err.Error(), "file is empty") {
t.Errorf("Expected 'file is empty' error, got: %s", err.Error())
}
})
}
func TestDetectMimeType(t *testing.T) {
t.Run("DetectMimeType by extension", func(t *testing.T) {
client, _ := New("test-token")
tmpFile, err := os.CreateTemp("", "mime_test_*.txt")
if err != nil {
t.Fatal("Failed to create temp file:", err)
}
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()
tmpFile.WriteString("test content")
tmpFile.Sync()
mimeType, err := client.DetectMimeType(tmpFile.Name())
if err != nil {
t.Fatal("Expected no error, got:", err)
}
expectedType := "text/plain; charset=utf-8"
if mimeType != expectedType {
t.Errorf("Expected MIME type '%s', got '%s'", expectedType, mimeType)
}
})
t.Run("DetectMimeType for non-existent file", func(t *testing.T) {
client, _ := New("test-token")
// Use a file without a recognized extension to force content reading
_, err := client.DetectMimeType("/non/existent/file.unknown")
if err == nil {
t.Error("Expected error for non-existent file")
}
if !strings.Contains(err.Error(), "cannot open file") {
t.Errorf("Expected 'cannot open file' error, got: %s", err.Error())
}
})
}
func TestValidateFilePath(t *testing.T) {
testCases := []struct {
path string
shouldError bool
description string
}{
{"/valid/path/file.txt", false, "valid path"},
{"", true, "empty path"},
{"invalid/path", true, "path not starting with /"},
{"/path/with<invalid>chars", true, "path with invalid characters"},
{"/path/with:colon", true, "path with colon"},
{"/path/with\"quote", true, "path with quote"},
{"/path/with|pipe", true, "path with pipe"},
{"/path/with?question", true, "path with question mark"},
{"/path/with*asterisk", true, "path with asterisk"},
{"/valid/long/path/that/is/acceptable.txt", false, "normal long path"},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
err := ValidateFilePath(tc.path)
if tc.shouldError && err == nil {
t.Errorf("Expected error for %s, got none", tc.description)
}
if !tc.shouldError && err != nil {
t.Errorf("Expected no error for %s, got: %s", tc.description, err.Error())
}
})
}
}
// uploadMockTransport simulates the file upload to Yandex servers
type uploadMockTransport struct {
t *testing.T
expectedContent string
}
func (u *uploadMockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// Read the request body to verify content
if req.Body != nil {
body, err := io.ReadAll(req.Body)
if err != nil {
u.t.Errorf("Failed to read request body: %v", err)
}
// Verify content matches expected
if string(body) != u.expectedContent {
u.t.Errorf("Upload content mismatch. Expected: %s, Got: %s", u.expectedContent, string(body))
}
}
// Return successful response
return &http.Response{
StatusCode: 201,
Status: "201 Created",
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader("")),
Request: req,
}, nil
}
func TestUploadOptions(t *testing.T) {
t.Run("UploadOptions validation", func(t *testing.T) {
client, _ := New("test-token")
// Test that all upload options are properly accepted
var progressUpdates []UploadProgress
progressCallback := func(progress UploadProgress) {
progressUpdates = append(progressUpdates, progress)
}
options := &UploadOptions{
Overwrite: true,
Progress: progressCallback,
ChunkSize: 5 * 1024 * 1024, // 5MB
ValidateChecksum: false,
}
// This will fail at file validation level, but we're testing that options are accepted
_, err := client.UploadFileFromPath(context.Background(), "/non/existent/file.txt", "/test/upload.txt", options)
if err == nil {
t.Error("Expected error for non-existent file")
}
// Verify the error is about file validation, not about options
if !strings.Contains(err.Error(), "file does not exist") {
t.Errorf("Expected file validation error, got: %s", err.Error())
}
})
}
// overwriteMockTransport checks for overwrite header
type overwriteMockTransport struct {
t *testing.T
}
func (o *overwriteMockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// Check for overwrite header
overwriteHeader := req.Header.Get("X-Overwrite")
if overwriteHeader != "true" {
o.t.Error("Expected X-Overwrite header to be 'true'")
}
return &http.Response{
StatusCode: 201,
Status: "201 Created",
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader("")),
Request: req,
}, nil
}
func TestUtilityFunctions(t *testing.T) {
t.Run("GetFileSize works correctly", func(t *testing.T) {
tmpFile, err := os.CreateTemp("", "size_test_*.txt")
if err != nil {
t.Fatal("Failed to create temp file:", err)
}
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()
testContent := "This is test content for size checking"
tmpFile.WriteString(testContent)
tmpFile.Sync()
size, err := GetFileSize(tmpFile.Name())
if err != nil {
t.Fatal("Expected no error, got:", err)
}
expectedSize := int64(len(testContent))
if size != expectedSize {
t.Errorf("Expected size %d, got %d", expectedSize, size)
}
})
t.Run("GetFileSize fails for non-existent file", func(t *testing.T) {
_, err := GetFileSize("/non/existent/file.txt")
if err == nil {
t.Error("Expected error for non-existent file")
}
})
t.Run("FormatFileSize formats correctly", func(t *testing.T) {
testCases := []struct {
bytes int64
expected string
}{
{512, "512 B"},
{1024, "1.0 KB"},
{1536, "1.5 KB"},
{1024 * 1024, "1.0 MB"},
{1024 * 1024 * 1024, "1.0 GB"},
{1536 * 1024 * 1024, "1.5 GB"},
}
for _, tc := range testCases {
result := FormatFileSize(tc.bytes)
if result != tc.expected {
t.Errorf("FormatFileSize(%d) = %s, expected %s", tc.bytes, result, tc.expected)
}
}
})
}
func TestConvenienceMethods(t *testing.T) {
t.Run("UploadFileFromPathWithProgress validates input", func(t *testing.T) {
client, _ := New("test-token")
_, err := client.UploadFileFromPathWithProgress(context.Background(), "", "/test/file.txt", false, nil)
if err == nil {
t.Error("Expected error for empty local path")
}
})
t.Run("UploadLargeFileFromPath validates input", func(t *testing.T) {
client, _ := New("test-token")
_, err := client.UploadLargeFileFromPath(context.Background(), "", "/test/file.txt", 10, nil)
if err == nil {
t.Error("Expected error for empty local path")
}
})
}
func TestUploadInternalFunctions(t *testing.T) {
t.Run("UploadFile function validation", func(t *testing.T) {
client, _ := New("test-token")
// Test the public UploadFile function with correct signature
_, err := client.UploadFile(context.Background(), "/test/file.txt", "https://mock-upload-url.com")
if err != nil {
// Expected to fail due to mock setup, but function should exist and validate
t.Log("UploadFile function exists and validates input")
}
})
t.Run("DetectMimeType covers additional cases", func(t *testing.T) {
client, _ := New("test-token")
// Test DetectMimeType with various extensions
testCases := []struct {
filename string
contains string // Use contains instead of exact match
}{
{"test.txt", "text/plain"},
{"test.json", "application/json"},
}
for _, tc := range testCases {
mimeType, _ := client.DetectMimeType(tc.filename)
if !strings.Contains(mimeType, tc.contains) {
t.Errorf("Expected mime type to contain %s for %s, got %s", tc.contains, tc.filename, mimeType)
}
}
})
}