-
Notifications
You must be signed in to change notification settings - Fork 0
/
zzfile_test.go
95 lines (74 loc) · 2.34 KB
/
zzfile_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
package router
import (
"strings"
"testing"
"net/http/httptest"
"github.com/starmanmartin/simple-fs"
"github.com/starmanmartin/simple-router/rtest"
)
func TestUploadAndPublic(t *testing.T) {
router := NewRouter()
handler := router.UploadPath("/test", true)
params := map[string]string{
"name": "Maritn",
"surname": "bar",
}
r, err := rtest.NewFileUploadRequest("http://localhost:80", params, "image", "test/image.png")
if err != nil {
t.Error("Error", err)
t.SkipNow()
}
if isNext, err := handler(nil, r); !isNext {
t.Error("No next on upload")
} else if err != nil {
t.Error("Error", err)
}
imageName := r.Files["image"].Name
imagePath := r.Files["image"].Path
if ex, err := fs.Exists(imagePath + "/" + imageName); !ex {
t.Error("File not exists")
} else if err != nil {
t.Error("Error", err)
}
r, err = rtest.NewFileUploadRequest("http://localhost:80", params, "image", "test/image.png")
if err != nil {
t.Error("Error", err)
t.SkipNow()
}
r.Header.Set("Content-type", "multipart/form-data;")
if isNext, err := handler(nil, r); !isNext {
t.Error("No next on upload")
} else if err == nil {
t.Error("No error")
}
r, err = rtest.NewPostReqeust("http://localhost:80", nil)
if err != nil {
t.Error("Error", err)
t.SkipNow()
}
r.Header.Set("Content-type", "multipart/form-data;boundary=foo;")
if isNext, err := handler(nil, r); !isNext {
t.Error("No next on upload")
} else if err == nil {
t.Error("No error")
}
router.Public("test")
if finalHandler, ok := findListOfHandler(routerList[mGet], strings.Split("test/" + imageName, "/"), false); ok {
for _, finalHanderTemp := range finalHandler {
for _, handerTemp := range finalHanderTemp.hanlder {
w := httptest.NewRecorder()
req, _ := rtest.NewGetRequest("http://localhost.com/test/" + imageName, nil)
handerTemp(w, req)
if w.Header().Get("Content-Type") != "image/png" {
t.Error("Public download wrong Content-Type")
}
w = httptest.NewRecorder()
req, _ = rtest.NewGetRequest("http://localhost.com/test/not.png", nil)
handerTemp(w, req)
if w.Code != 404 {
t.Error("Not not found", w.Code)
}
}
}
}
}