-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
53 lines (42 loc) · 1.08 KB
/
main_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
package main
import(
"testing"
"os"
"bytes"
"mime/multipart"
"path/filepath"
"net/http/httptest"
"net/http"
"io"
"github.com/stretchr/testify/assert"
)
func TestUploadTest(t *testing.T){
assert := assert.New(t)
path := "./icon.png"
file, _ := os.Open(path)
defer file.Close()
os.RemoveAll("./uploads")
buf := &bytes.Buffer{}
writer := multipart.NewWriter(buf)
multi, err := writer.CreateFormFile("upload_file",filepath.Base(path))
assert.NoError(err)
io.Copy(multi,file)
writer.Close()
res := httptest.NewRecorder()
req := httptest.NewRequest("POST","/uploads",buf)
req.Header.Set("Content-type",writer.FormDataContentType())
uploadsHandler(res,req)
assert.Equal(http.StatusOK, res.Code)
uploadFilePath := "./uploads/"+ filepath.Base(path)
_, err1 := os.Stat(uploadFilePath)
assert.NoError(err1)
uploadFile, _ := os.Open(uploadFilePath)
originFile, _ := os.Open(path)
defer uploadFile.Close()
defer originFile.Close()
uploadData := []byte{}
originData := []byte{}
uploadFile.Read(uploadData)
originFile.Read(originData)
assert.Equal(originData,uploadData)
}