-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_test.go
65 lines (59 loc) · 1.44 KB
/
file_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
package kyuu
import (
"bytes"
"html/template"
"mime/multipart"
"path"
"testing"
)
func TestFileUploader_Handle(t *testing.T) {
s := NewHTTPServer()
s.Get("/upload_page", func(ctx *Context) {
tpl := template.New("upload")
tpl, err := tpl.Parse(`
<html>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="myfile" />
<button type="submit">上传</button>
</form>
</body>
<html>
`)
if err != nil {
t.Fatal(err)
}
page := &bytes.Buffer{}
err = tpl.Execute(page, nil)
if err != nil {
t.Fatal(err)
}
ctx.RespStatusCode = 200
ctx.RespData = page.Bytes()
})
s.Post("/upload", (&FileUploader{
// 这里的 myfile 就是 <input type="file" name="myfile" />
// 那个 name 的取值
FileField: "myfile",
DstPathFunc: func(fh *multipart.FileHeader) string {
return path.Join("testdata", "upload", fh.Filename)
},
}).Handle())
s.Start(":8081")
}
func TestFileDownloader_Handle(t *testing.T) {
s := NewHTTPServer()
s.Get("/download", (&FileDownloader{
// 下载的文件所在目录
Dir: "./testdata/download",
}).Handle())
// 在浏览器里面输入 localhost:8081/download?file=test.txt
s.Start(":8081")
}
func TestStaticResourceHandler_Handle(t *testing.T) {
s := NewHTTPServer()
handler := NewStaticResourceHandler("./testdata/img", "/img")
s.Get("/img/:file", handler.Handle)
// 在浏览器里面输入 localhost:8081/img/come_on_baby.jpg
s.Start(":8081")
}