diff --git a/pkg/filesystem/remote/handler.go b/pkg/filesystem/remote/handler.go index a3320f1..9741d69 100644 --- a/pkg/filesystem/remote/handler.go +++ b/pkg/filesystem/remote/handler.go @@ -35,8 +35,6 @@ func (handler Handler) getAPIUrl(scope string, routes ...string) string { var controller *url.URL switch scope { - case "upload": - controller, _ = url.Parse("/api/v3/slave/upload") case "delete": controller, _ = url.Parse("/api/v3/slave/delete") case "thumb": @@ -53,7 +51,6 @@ func (handler Handler) getAPIUrl(scope string, routes ...string) string { } // Get 获取文件内容 -// TODO 测试 func (handler Handler) Get(ctx context.Context, path string) (response.RSCloser, error) { // 尝试获取速度限制 TODO 是否需要在这里限制? speedLimit := 0 @@ -109,7 +106,7 @@ func (handler Handler) Put(ctx context.Context, file io.ReadCloser, dst string, // 上传文件 resp, err := handler.Client.Request( "POST", - handler.getAPIUrl("upload"), + handler.Policy.GetUploadURL(), file, request.WithHeader(map[string][]string{ "Authorization": {credential.Token}, diff --git a/pkg/filesystem/remote/handler_test.go b/pkg/filesystem/remote/handler_test.go index a0ac249..21e98db 100644 --- a/pkg/filesystem/remote/handler_test.go +++ b/pkg/filesystem/remote/handler_test.go @@ -251,3 +251,104 @@ func TestHandler_Get(t *testing.T) { asserts.Error(err) } } + +func TestHandler_Put(t *testing.T) { + asserts := assert.New(t) + handler := Handler{ + Policy: &model.Policy{ + Type: "remote", + SecretKey: "test", + Server: "http://test.com", + }, + AuthInstance: auth.HMACAuth{}, + } + ctx := context.Background() + asserts.NoError(cache.Set("setting_upload_credential_timeout", "3600", 0)) + + // 成功 + { + ctx = context.WithValue(ctx, fsctx.UserCtx, model.User{}) + clientMock := ClientMock{} + clientMock.On( + "Request", + "POST", + "http://test.com/api/v3/slave/upload", + testMock.Anything, + testMock.Anything, + ).Return(&request.Response{ + Err: nil, + Response: &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(strings.NewReader(`{"code":0}`)), + }, + }) + handler.Client = clientMock + err := handler.Put(ctx, ioutil.NopCloser(strings.NewReader("test input file")), "/", 15) + clientMock.AssertExpectations(t) + asserts.NoError(err) + } + + // 请求失败 + { + ctx = context.WithValue(ctx, fsctx.UserCtx, model.User{}) + clientMock := ClientMock{} + clientMock.On( + "Request", + "POST", + "http://test.com/api/v3/slave/upload", + testMock.Anything, + testMock.Anything, + ).Return(&request.Response{ + Err: nil, + Response: &http.Response{ + StatusCode: 404, + Body: ioutil.NopCloser(strings.NewReader(`{"code":0}`)), + }, + }) + handler.Client = clientMock + err := handler.Put(ctx, ioutil.NopCloser(strings.NewReader("test input file")), "/", 15) + clientMock.AssertExpectations(t) + asserts.Error(err) + } + + // 返回错误 + { + ctx = context.WithValue(ctx, fsctx.UserCtx, model.User{}) + clientMock := ClientMock{} + clientMock.On( + "Request", + "POST", + "http://test.com/api/v3/slave/upload", + testMock.Anything, + testMock.Anything, + ).Return(&request.Response{ + Err: nil, + Response: &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(strings.NewReader(`{"code":1}`)), + }, + }) + handler.Client = clientMock + err := handler.Put(ctx, ioutil.NopCloser(strings.NewReader("test input file")), "/", 15) + clientMock.AssertExpectations(t) + asserts.Error(err) + } + +} + +func TestHandler_Thumb(t *testing.T) { + asserts := assert.New(t) + handler := Handler{ + Policy: &model.Policy{ + Type: "remote", + SecretKey: "test", + Server: "http://test.com", + }, + AuthInstance: auth.HMACAuth{}, + } + ctx := context.Background() + asserts.NoError(cache.Set("setting_slave_api_timeout", "60", 0)) + resp, err := handler.Thumb(ctx, "/1.txt") + asserts.NoError(err) + asserts.True(resp.Redirect) +} diff --git a/pkg/request/request.go b/pkg/request/request.go index f900a96..d8bb438 100644 --- a/pkg/request/request.go +++ b/pkg/request/request.go @@ -90,7 +90,6 @@ func WithHeader(header http.Header) Option { } // WithContentLength 设置请求大小 -// TODO 测试 func WithContentLength(s int64) Option { return optionFunc(func(o *options) { o.contentLength = s @@ -170,7 +169,6 @@ func (resp *Response) CheckHTTPResponse(status int) *Response { } // DecodeResponse 尝试解析为serializer.Response,并对状态码进行检查 -// TODO 测试 func (resp *Response) DecodeResponse() (*serializer.Response, error) { if resp.Err != nil { return nil, resp.Err diff --git a/pkg/request/request_test.go b/pkg/request/request_test.go index 212bd44..fa4971f 100644 --- a/pkg/request/request_test.go +++ b/pkg/request/request_test.go @@ -37,12 +37,11 @@ func TestWithHeader(t *testing.T) { asserts.Equal(http.Header{"Origin": []string{"123"}}, options.header) } -func TestWithCredential(t *testing.T) { +func TestWithContentLength(t *testing.T) { asserts := assert.New(t) options := newDefaultOption() - WithCredential(auth.HMACAuth{SecretKey: []byte("123")}, 10).apply(options) - asserts.Equal(auth.HMACAuth{SecretKey: []byte("123")}, options.sign) - asserts.EqualValues(10, options.signTTL) + WithContentLength(10).apply(options) + asserts.EqualValues(10, options.contentLength) } func TestWithContext(t *testing.T) { @@ -175,3 +174,40 @@ func TestResponse_GetRSCloser(t *testing.T) { } } + +func TestResponse_DecodeResponse(t *testing.T) { + asserts := assert.New(t) + + // 直接返回错误 + { + resp := Response{Err: errors.New("error")} + response, err := resp.DecodeResponse() + asserts.Error(err) + asserts.Nil(response) + } + + // 无法解析响应 + { + resp := Response{ + Response: &http.Response{ + Body: ioutil.NopCloser(strings.NewReader("test")), + }, + } + response, err := resp.DecodeResponse() + asserts.Error(err) + asserts.Nil(response) + } + + // 成功 + { + resp := Response{ + Response: &http.Response{ + Body: ioutil.NopCloser(strings.NewReader("{\"code\":0}")), + }, + } + response, err := resp.DecodeResponse() + asserts.NoError(err) + asserts.NotNil(response) + asserts.Equal(0, response.Code) + } +}