-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
90 lines (76 loc) · 2.51 KB
/
handler_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
package GFileMux
import (
"bytes"
"context"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"
)
// MockStorage is a mock implementation of the Storage interface for testing.
type MockStorage struct {
uploadedFiles map[string]*UploadedFileMetadata
}
func (ms *MockStorage) Upload(ctx context.Context, reader io.Reader, options *UploadFileOptions) (*UploadedFileMetadata, error) {
if ms.uploadedFiles == nil {
ms.uploadedFiles = make(map[string]*UploadedFileMetadata)
}
ms.uploadedFiles[options.FileName] = &UploadedFileMetadata{
FolderDestination: options.Bucket,
Size: 12345,
Key: options.FileName,
}
return ms.uploadedFiles[options.FileName], nil
}
func (ms *MockStorage) Path(ctx context.Context, options PathOptions) (string, error) {
return "mock/path/" + options.Key, nil
}
func (ms *MockStorage) Close() error {
return nil
}
func TestUpload(t *testing.T) {
mockStorage := &MockStorage{}
handler, err := New(
WithStorage(mockStorage),
WithMaxFileSize(10<<20),
WithFileValidatorFunc(DefaultFileValidator),
WithFileNameGeneratorFunc(DefaultFileNameGeneratorFunc),
WithUploadErrorHandlerFunc(DefaultUploadErrorHandlerFunc),
)
if err != nil {
t.Fatalf("Failed to create handler: %v", err)
}
// Create a new HTTP request with a multipart form
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file1", "testfile.txt")
if err != nil {
t.Fatalf("Failed to create form file: %v", err)
}
part.Write([]byte("This is a test file"))
writer.Close()
req := httptest.NewRequest("POST", "/", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
// Create a ResponseRecorder to capture the response
rr := httptest.NewRecorder()
// Create a test handler to verify the upload
testHandler := handler.Upload("test_bucket", "file1")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
files, err := GetUploadedFilesFromContext(r)
if err != nil {
t.Fatalf("Failed to get uploaded files from context: %v", err)
}
if len(files["file1"]) != 1 {
t.Fatalf("Expected 1 file, got %d", len(files["file1"]))
}
if files["file1"][0].OriginalName != "testfile.txt" {
t.Fatalf("Expected file name 'testfile.txt', got '%s'", files["file1"][0].OriginalName)
}
}))
// Serve the HTTP request
testHandler.ServeHTTP(rr, req)
// Check the response status code
if status := rr.Code; status != http.StatusOK {
t.Fatalf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
}