-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupload_test.go
90 lines (70 loc) · 2.03 KB
/
upload_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
// SPDX-FileCopyrightText: 2015-2024 caixw
//
// SPDX-License-Identifier: MIT
package upload
import (
"bytes"
"io"
"io/fs"
"mime/multipart"
"net/http"
"os"
"path"
"testing"
"time"
"github.com/issue9/assert/v4"
)
var _ fs.FS = &Upload{}
func TestNew(t *testing.T) {
a := assert.New(t, false)
s, err := NewLocalSaver("./testdir", "", Day, Filename)
a.NotError(err).NotNil(s)
u := New(s, 10*1024, "gif", ".png", ".GIF")
a.NotNil(u)
// 自动转换成小写,且加上最前面的.符号
a.Equal(u.exts, []string{".gif", ".png", ".gif"})
a.Equal(s.(*localSaver).dir, "./testdir"+string(os.PathSeparator))
// dir 为一个文件
s, err = NewLocalSaver("./testdir/file", "", Day, Filename)
a.Error(err).Nil(s)
}
func TestUpload_isAllowExt(t *testing.T) {
a := assert.New(t, false)
s, err := NewLocalSaver("./testdir", "", Day, Filename)
a.NotError(err).NotNil(s)
u := New(s, 10*1024, "gif", ".png", ".GIF")
a.NotError(err)
a.True(u.isAllowExt(".gif"))
a.True(u.isAllowExt(".png"))
a.False(u.isAllowExt(".TXT"))
a.False(u.isAllowExt(""))
a.False(u.isAllowExt("png"))
a.False(u.isAllowExt(".exe"))
}
func TestUpload_Do(t *testing.T) {
a := assert.New(t, false)
s, err := NewLocalSaver("./testdir", "https://example.com", Day, Filename)
a.NotError(err).NotNil(s)
u := New(s, 10*1024, "xml")
a.NotError(err)
filename := "./testdir/file.xml"
f, err := os.Open(filename)
a.NotError(err).NotNil(f)
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
fw, err := writer.CreateFormFile("file", filename)
a.NotError(err).NotNil(fw)
_, err = io.Copy(fw, f)
a.NotError(err)
err = writer.WriteField("filename", filename)
a.NotError(err)
err = writer.Close() // close writer before POST request
a.NotError(err)
r, err := http.NewRequest(http.MethodPost, "/upload", body)
r.Header.Add("content-type", writer.FormDataContentType())
a.NotError(err).NotNil(r)
paths, err := u.Do("file", r)
a.NotError(err).
Length(paths, 1).
Equal(paths[0], "https://example.com/"+path.Join(time.Now().Format(Day), "file.xml"))
}