-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathform_test.go
145 lines (120 loc) · 4.72 KB
/
form_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
package requests
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// startFileUploadServer starts a mock server to test file uploads.
func startFileUploadServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Parse the multipart form
err := r.ParseMultipartForm(10 << 20) // Limit: 10MB
if err != nil {
http.Error(w, "Failed to parse multipart form", http.StatusBadRequest)
return
}
// Collect file upload details
uploads := make(map[string][]string)
for key, files := range r.MultipartForm.File {
for _, fileHeader := range files {
file, err := fileHeader.Open()
if err != nil {
http.Error(w, "Failed to open file", http.StatusInternalServerError)
return
}
defer file.Close() //nolint: errcheck
// Read file content (for demonstration; in real tests, might hash or skip)
content, err := io.ReadAll(file)
if err != nil {
http.Error(w, "Failed to read file content", http.StatusInternalServerError)
return
}
// Store file details (e.g., filename and a snippet of content for verification)
contentSnippet := string(content)
if len(contentSnippet) > 10 {
contentSnippet = contentSnippet[:10] + "..."
}
uploads[key] = append(uploads[key], fmt.Sprintf("%s: %s", fileHeader.Filename, contentSnippet))
}
}
// Respond with details of the uploaded files in JSON format
w.Header().Set("Content-Type", "application/json")
if encoder := json.NewEncoder(w); encoder != nil {
if err = encoder.Encode(uploads); err != nil {
http.Error(w, "Failed to encode response", http.StatusInternalServerError)
}
} else {
http.Error(w, "Failed to create JSON encoder", http.StatusInternalServerError)
}
}))
}
func TestFiles(t *testing.T) {
server := startFileUploadServer()
defer server.Close()
client := Create(&Config{BaseURL: server.URL})
fileContent1 := strings.NewReader("File content 1")
fileContent2 := strings.NewReader("File content 2")
resp, err := client.Post("/").
Files(
&File{Name: "file1", FileName: "test1.txt", Content: io.NopCloser(fileContent1)},
&File{Name: "file2", FileName: "test2.txt", Content: io.NopCloser(fileContent2)},
).
Send(context.Background())
assert.NoError(t, err, "No error expected on sending request")
var uploads map[string][]string
err = resp.ScanJSON(&uploads)
assert.NoError(t, err, "Expect no error on parsing response")
// Validate the file uploads
assert.Contains(t, uploads, "file1", "file1 should be present in the uploads")
assert.Contains(t, uploads, "file2", "file2 should be present in the uploads")
// Optionally check for specific file content snippets
}
func TestFile(t *testing.T) {
server := startFileUploadServer() // Start the mock file upload server
defer server.Close()
client := Create(&Config{BaseURL: server.URL})
// Simulate a file's content
fileContent := strings.NewReader("This is the file content")
// Send a request with a single file
resp, err := client.Post("/").
File("file", "single.txt", io.NopCloser(fileContent)).
Send(context.Background())
assert.NoError(t, err, "No error expected on sending request")
// Parse the server's JSON response
var uploads map[string][]string
err = resp.ScanJSON(&uploads)
assert.NoError(t, err, "Expect no error on parsing response")
// Check if the server received the file correctly
assert.Contains(t, uploads, "file", "The file should be present in the uploads")
assert.Contains(t, uploads["file"][0], "single.txt", "The file name should be correctly received")
}
func TestDelFile(t *testing.T) {
server := startFileUploadServer() // Start the mock file upload server
defer server.Close()
client := Create(&Config{BaseURL: server.URL})
// Simulate file contents
fileContent1 := strings.NewReader("File content 1")
fileContent2 := strings.NewReader("File content 2")
// Prepare the request with two files, then delete one before sending
resp, err := client.Post("/").
Files(
&File{Name: "file1", FileName: "file1.txt", Content: io.NopCloser(fileContent1)},
&File{Name: "file2", FileName: "file2.txt", Content: io.NopCloser(fileContent2)},
).
DelFile("file1"). // Remove the first file
Send(context.Background())
assert.NoError(t, err, "No error expected on sending request")
// Parse the server's JSON response
var uploads map[string][]string
err = resp.ScanJSON(&uploads)
assert.NoError(t, err, "Expect no error on parsing response")
// Validate that only the second file was uploaded
assert.NotContains(t, uploads, "file1", "file1 should have been removed from the uploads")
assert.Contains(t, uploads, "file2", "file2 should be present in the uploads")
}