79 lines
1.5 KiB
Go
79 lines
1.5 KiB
Go
//
|
|
// storage_local_test.go
|
|
// Copyright (C) 2022 tiglog <me@tiglog.com>
|
|
//
|
|
// Distributed under terms of the MIT license.
|
|
//
|
|
|
|
package storage_test
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"mime/multipart"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.hexq.cn/tiglog/golib/gtest"
|
|
"git.hexq.cn/tiglog/golib/storage"
|
|
)
|
|
|
|
func getLocalStorage() *storage.Engine {
|
|
ls := storage.NewLocalStorage("/tmp")
|
|
return storage.NewStorage(ls)
|
|
}
|
|
|
|
func TestNewStorage(t *testing.T) {
|
|
s := getLocalStorage()
|
|
name := s.GetName()
|
|
gtest.Equal(t, "local", name)
|
|
}
|
|
|
|
func getFileheader() (*multipart.FileHeader, error) {
|
|
path := "testdata/hello.txt"
|
|
file, err := os.Open(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer file.Close()
|
|
body := &bytes.Buffer{}
|
|
writer := multipart.NewWriter(body)
|
|
part, err := writer.CreateFormFile("my_file", filepath.Base(path))
|
|
if err != nil {
|
|
writer.Close()
|
|
return nil, err
|
|
}
|
|
io.Copy(part, file)
|
|
writer.Close()
|
|
|
|
req := httptest.NewRequest("POST", "/upload", body)
|
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
|
_, h, err := req.FormFile("my_file")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return h, nil
|
|
}
|
|
|
|
func TestUpload(t *testing.T) {
|
|
s := getLocalStorage()
|
|
upfile, err := getFileheader()
|
|
gtest.Nil(t, err)
|
|
|
|
path, err2 := s.Upload(upfile)
|
|
gtest.Nil(t, err2)
|
|
fmt.Println("path is ", path)
|
|
gtest.NotEqual(t, "", path)
|
|
tmp := strings.Split(path, "/")
|
|
fmt.Println(tmp)
|
|
if len(tmp) > 1 {
|
|
dir := filepath.Join(s.GetBaseDir(), tmp[0])
|
|
os.RemoveAll(dir)
|
|
}
|
|
}
|