Test: remote handler get & request chan operations / Modify: GET request to remote server should return error http status code

This commit is contained in:
HFO4
2020-01-04 15:17:27 +08:00
parent aeca161186
commit 93dc25aabb
10 changed files with 249 additions and 44 deletions

View File

@@ -3,7 +3,6 @@ package filesystem
import (
"archive/zip"
"context"
"errors"
"fmt"
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
@@ -27,15 +26,18 @@ func (fs *FileSystem) Compress(ctx context.Context, folderIDs, fileIDs []uint) (
if err != nil && len(folders) != 0 {
return "", ErrDBListObjects
}
// 查找待压缩文件
files, err := model.GetFilesByIDs(fileIDs, fs.User.ID)
if err != nil && len(files) != 0 {
return "", ErrDBListObjects
}
// 尝试获取请求上下文,以便于后续检查用户取消任务
reqContext := ctx
ginCtx, ok := ctx.Value(fsctx.GinCtx).(*gin.Context)
if !ok {
return "", errors.New("无法获取请求上下文")
if ok {
reqContext = ginCtx.Request.Context()
}
// 将顶级待处理对象的路径设为根路径
@@ -62,12 +64,12 @@ func (fs *FileSystem) Compress(ctx context.Context, folderIDs, fileIDs []uint) (
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
ctx = context.WithValue(ginCtx.Request.Context(), fsctx.UserCtx, *fs.User)
ctx = reqContext
// 压缩各个目录及文件
for i := 0; i < len(folders); i++ {
select {
case <-ginCtx.Request.Context().Done():
case <-reqContext.Done():
// 取消压缩请求
fs.cancelCompress(ctx, zipWriter, zipFile, zipFilePath)
return "", ErrClientCanceled
@@ -78,7 +80,7 @@ func (fs *FileSystem) Compress(ctx context.Context, folderIDs, fileIDs []uint) (
}
for i := 0; i < len(files); i++ {
select {
case <-ginCtx.Request.Context().Done():
case <-reqContext.Done():
// 取消压缩请求
fs.cancelCompress(ctx, zipWriter, zipFile, zipFilePath)
return "", ErrClientCanceled

View File

@@ -56,4 +56,27 @@ func TestFileSystem_Compress(t *testing.T) {
asserts.Contains(zipFile, "archive_")
asserts.Contains(zipFile, "tests")
}
// 上下文取消
{
ctx, cancel := context.WithCancel(context.Background())
cancel()
// 查找压缩父目录
mock.ExpectQuery("SELECT(.+)folders(.+)").
WithArgs(1, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(1, "parent"))
// 查找顶级待压缩文件
mock.ExpectQuery("SELECT(.+)files(.+)").
WithArgs(1, 1).
WillReturnRows(
sqlmock.NewRows(
[]string{"id", "name", "source_name", "policy_id"}).
AddRow(1, "1.txt", "tests/file1.txt", 1),
)
asserts.NoError(cache.Set("setting_temp_path", "tests", -1))
zipFile, err := fs.Compress(ctx, []uint{1}, []uint{1})
asserts.Error(err)
asserts.Empty(zipFile)
}
}

View File

@@ -550,9 +550,9 @@ type ClientMock struct {
testMock.Mock
}
func (m ClientMock) Request(method, target string, body io.Reader, opts ...request.Option) request.Response {
func (m ClientMock) Request(method, target string, body io.Reader, opts ...request.Option) *request.Response {
args := m.Called(method, target, body, opts)
return args.Get(0).(request.Response)
return args.Get(0).(*request.Response)
}
func TestSlaveAfterUpload(t *testing.T) {
@@ -571,7 +571,7 @@ func TestSlaveAfterUpload(t *testing.T) {
"http://test/callbakc",
testMock.Anything,
testMock.Anything,
).Return(request.Response{
).Return(&request.Response{
Err: nil,
Response: &http.Response{
StatusCode: 200,

View File

@@ -26,8 +26,8 @@ type Handler struct {
AuthInstance auth.Auth
}
// getAPI 获取接口请求地址
func (handler Handler) getAPI(scope string) string {
// getAPIUrl 获取接口请求地址
func (handler Handler) getAPIUrl(scope string) string {
serverURL, err := url.Parse(handler.Policy.Server)
if err != nil {
return ""
@@ -45,7 +45,6 @@ func (handler Handler) getAPI(scope string) string {
}
// Get 获取文件内容
// TODO 测试
func (handler Handler) Get(ctx context.Context, path string) (response.RSCloser, error) {
// 尝试获取速度限制 TODO 是否需要在这里限制?
speedLimit := 0
@@ -65,7 +64,7 @@ func (handler Handler) Get(ctx context.Context, path string) (response.RSCloser,
downloadURL,
nil,
request.WithContext(ctx),
).GetRSCloser()
).CheckHTTPResponse(200).GetRSCloser()
if err != nil {
return nil, err
@@ -96,10 +95,10 @@ func (handler Handler) Delete(ctx context.Context, files []string) ([]string, er
signTTL := model.GetIntSetting("slave_api_timeout", 60)
resp, err := handler.Client.Request(
"POST",
handler.getAPI("delete"),
handler.getAPIUrl("delete"),
bodyReader,
request.WithCredential(handler.AuthInstance, int64(signTTL)),
).GetResponse(200)
).CheckHTTPResponse(200).GetResponse()
if err != nil {
return files, err
}
@@ -127,7 +126,7 @@ func (handler Handler) Delete(ctx context.Context, files []string) ([]string, er
// Thumb 获取文件缩略图
func (handler Handler) Thumb(ctx context.Context, path string) (*response.ContentResponse, error) {
sourcePath := base64.RawURLEncoding.EncodeToString([]byte(path))
thumbURL := handler.getAPI("thumb") + "/" + sourcePath
thumbURL := handler.getAPIUrl("thumb") + "/" + sourcePath
ttl := model.GetIntSetting("slave_api_timeout", 60)
signedThumbURL, err := auth.SignURI(handler.AuthInstance, thumbURL, int64(ttl))
if err != nil {

View File

@@ -102,9 +102,9 @@ type ClientMock struct {
testMock.Mock
}
func (m ClientMock) Request(method, target string, body io.Reader, opts ...request.Option) request.Response {
func (m ClientMock) Request(method, target string, body io.Reader, opts ...request.Option) *request.Response {
args := m.Called(method, target, body, opts)
return args.Get(0).(request.Response)
return args.Get(0).(*request.Response)
}
func TestHandler_Delete(t *testing.T) {
@@ -128,7 +128,7 @@ func TestHandler_Delete(t *testing.T) {
"http://test.com/api/v3/slave/delete",
testMock.Anything,
testMock.Anything,
).Return(request.Response{
).Return(&request.Response{
Err: nil,
Response: &http.Response{
StatusCode: 200,
@@ -152,7 +152,7 @@ func TestHandler_Delete(t *testing.T) {
"http://test.com/api/v3/slave/delete",
testMock.Anything,
testMock.Anything,
).Return(request.Response{
).Return(&request.Response{
Err: nil,
Response: &http.Response{
StatusCode: 200,
@@ -175,7 +175,7 @@ func TestHandler_Delete(t *testing.T) {
"http://test.com/api/v3/slave/delete",
testMock.Anything,
testMock.Anything,
).Return(request.Response{
).Return(&request.Response{
Err: nil,
Response: &http.Response{
StatusCode: 200,
@@ -189,3 +189,63 @@ func TestHandler_Delete(t *testing.T) {
asserts.Len(failed, 1)
}
}
func TestHandler_Get(t *testing.T) {
asserts := assert.New(t)
handler := Handler{
Policy: &model.Policy{
SecretKey: "test",
Server: "http://test.com",
},
AuthInstance: auth.HMACAuth{},
}
ctx := context.Background()
// 成功
{
ctx = context.WithValue(ctx, fsctx.UserCtx, model.User{})
clientMock := ClientMock{}
clientMock.On(
"Request",
"GET",
testMock.Anything,
nil,
testMock.Anything,
).Return(&request.Response{
Err: nil,
Response: &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader(`{"code":0}`)),
},
})
handler.Client = clientMock
resp, err := handler.Get(ctx, "/test.txt")
clientMock.AssertExpectations(t)
asserts.NotNil(resp)
asserts.NoError(err)
}
// 请求失败
{
ctx = context.WithValue(ctx, fsctx.UserCtx, model.User{})
clientMock := ClientMock{}
clientMock.On(
"Request",
"GET",
testMock.Anything,
nil,
testMock.Anything,
).Return(&request.Response{
Err: nil,
Response: &http.Response{
StatusCode: 404,
Body: ioutil.NopCloser(strings.NewReader(`{"code":0}`)),
},
})
handler.Client = clientMock
resp, err := handler.Get(ctx, "/test.txt")
clientMock.AssertExpectations(t)
asserts.Nil(resp)
asserts.Error(err)
}
}