-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_test.go
More file actions
72 lines (61 loc) · 1.63 KB
/
context_test.go
File metadata and controls
72 lines (61 loc) · 1.63 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
package GFileMux
import (
"net/http/httptest"
"testing"
"net/http"
)
func TestGetUploadedFilesFromContext_Empty(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/", nil)
_, err := GetUploadedFilesFromContext(req)
if err == nil {
t.Fatal("expected error when no files in context")
}
}
func TestGetFilesByFieldFromContext_Empty(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/", nil)
_, err := GetFilesByFieldFromContext(req, "field")
if err == nil {
t.Fatal("expected error when no files in context")
}
}
func TestFiles_All(t *testing.T) {
f := Files{
"images": {
{OriginalName: "a.jpg"},
{OriginalName: "b.jpg"},
},
"docs": {
{OriginalName: "c.pdf"},
},
}
all := f.All()
if len(all) != 3 {
t.Fatalf("expected 3 files from All(), got %d", len(all))
}
}
func TestFiles_Count(t *testing.T) {
f := Files{
"images": {{OriginalName: "a.jpg"}, {OriginalName: "b.jpg"}},
"docs": {{OriginalName: "c.pdf"}},
}
if got := f.Count(); got != 3 {
t.Fatalf("expected Count()=3, got %d", got)
}
}
func TestFiles_CountEmpty(t *testing.T) {
f := Files{}
if got := f.Count(); got != 0 {
t.Fatalf("expected Count()=0 for empty Files, got %d", got)
}
}
func TestAddFilesToContext_Accumulates(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/", nil)
first := Files{"a": {{OriginalName: "one.txt"}}}
ctx := addFilesToContext(req.Context(), first)
second := Files{"b": {{OriginalName: "two.txt"}}}
ctx = addFilesToContext(ctx, second)
all := getFilesFromContext(ctx)
if all.Count() != 2 {
t.Fatalf("expected 2 files after two addFilesToContext calls, got %d", all.Count())
}
}