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

@@ -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)
}
}