Test: download file in filesystem

This commit is contained in:
HFO4
2019-11-27 13:54:25 +08:00
parent 9bb432c220
commit 16067c3ac8
6 changed files with 138 additions and 5 deletions

View File

@@ -2,7 +2,6 @@ package local
import (
"context"
"fmt"
"github.com/HFO4/cloudreve/pkg/util"
"io"
"os"
@@ -13,7 +12,6 @@ import (
type Handler struct{}
// Get 获取文件内容
// TODO:测试
func (handler Handler) Get(ctx context.Context, path string) (io.ReadSeeker, error) {
// 打开文件
file, err := os.Open(path)
@@ -32,8 +30,8 @@ func (handler Handler) Get(ctx context.Context, path string) (io.ReadSeeker, err
func closeReader(ctx context.Context, closer io.Closer) {
select {
case <-ctx.Done():
err := closer.Close()
fmt.Println("关闭reader", err)
_ = closer.Close()
}
}

View File

@@ -67,3 +67,25 @@ func TestHandler_Delete(t *testing.T) {
asserts.Equal([]string{}, list)
asserts.Error(err)
}
func TestHandler_Get(t *testing.T) {
asserts := assert.New(t)
handler := Handler{}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// 成功
file, err := os.Create("TestHandler_Get.txt")
asserts.NoError(err)
_ = file.Close()
rs, err := handler.Get(ctx, "TestHandler_Get.txt")
asserts.NoError(err)
asserts.NotNil(rs)
// 文件不存在
rs, err = handler.Get(ctx, "TestHandler_Get_notExist.txt")
asserts.Error(err)
asserts.Nil(rs)
}