Test: SlaveAfterUpload hook
This commit is contained in:
parent
64342fa88d
commit
ca9f44c06c
4 changed files with 60 additions and 7 deletions
|
@ -9,11 +9,16 @@ import (
|
||||||
"github.com/HFO4/cloudreve/pkg/conf"
|
"github.com/HFO4/cloudreve/pkg/conf"
|
||||||
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
|
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
|
||||||
"github.com/HFO4/cloudreve/pkg/filesystem/local"
|
"github.com/HFO4/cloudreve/pkg/filesystem/local"
|
||||||
|
"github.com/HFO4/cloudreve/pkg/request"
|
||||||
"github.com/HFO4/cloudreve/pkg/serializer"
|
"github.com/HFO4/cloudreve/pkg/serializer"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
testMock "github.com/stretchr/testify/mock"
|
testMock "github.com/stretchr/testify/mock"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -540,3 +545,51 @@ func TestHookSlaveUploadValidate(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ClientMock struct {
|
||||||
|
testMock.Mock
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSlaveAfterUpload(t *testing.T) {
|
||||||
|
asserts := assert.New(t)
|
||||||
|
conf.SystemConfig.Mode = "slave"
|
||||||
|
fs, err := NewAnonymousFileSystem()
|
||||||
|
conf.SystemConfig.Mode = "master"
|
||||||
|
asserts.NoError(err)
|
||||||
|
|
||||||
|
// 成功
|
||||||
|
{
|
||||||
|
clientMock := ClientMock{}
|
||||||
|
clientMock.On(
|
||||||
|
"Request",
|
||||||
|
"POST",
|
||||||
|
"http://test/callbakc",
|
||||||
|
testMock.Anything,
|
||||||
|
testMock.Anything,
|
||||||
|
).Return(request.Response{
|
||||||
|
Err: nil,
|
||||||
|
Response: &http.Response{
|
||||||
|
StatusCode: 200,
|
||||||
|
Body: ioutil.NopCloser(strings.NewReader(`{"code":0}`)),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
request.GeneralClient = clientMock
|
||||||
|
ctx := context.WithValue(context.Background(), fsctx.FileHeaderCtx, local.FileStream{
|
||||||
|
Size: 10,
|
||||||
|
VirtualPath: "/my",
|
||||||
|
Name: "test.txt",
|
||||||
|
})
|
||||||
|
ctx = context.WithValue(ctx, fsctx.UploadPolicyCtx, serializer.UploadPolicy{
|
||||||
|
CallbackURL: "http://test/callbakc",
|
||||||
|
})
|
||||||
|
ctx = context.WithValue(ctx, fsctx.SavePathCtx, "/not_exist")
|
||||||
|
err := SlaveAfterUpload(ctx, fs)
|
||||||
|
clientMock.AssertExpectations(t)
|
||||||
|
asserts.NoError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ func RemoteCallback(url string, body serializer.UploadCallback) error {
|
||||||
return serializer.NewError(serializer.CodeCallbackError, "无法编码回调正文", err)
|
return serializer.NewError(serializer.CodeCallbackError, "无法编码回调正文", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp := generalClient.Request(
|
resp := GeneralClient.Request(
|
||||||
"POST",
|
"POST",
|
||||||
url,
|
url,
|
||||||
bytes.NewReader(callbackBody),
|
bytes.NewReader(callbackBody),
|
||||||
|
|
|
@ -33,7 +33,7 @@ func TestRemoteCallback(t *testing.T) {
|
||||||
Body: ioutil.NopCloser(bytes.NewReader(mockResp)),
|
Body: ioutil.NopCloser(bytes.NewReader(mockResp)),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
generalClient = clientMock
|
GeneralClient = clientMock
|
||||||
resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
||||||
SourceName: "source",
|
SourceName: "source",
|
||||||
})
|
})
|
||||||
|
@ -58,7 +58,7 @@ func TestRemoteCallback(t *testing.T) {
|
||||||
Body: ioutil.NopCloser(bytes.NewReader(mockResp)),
|
Body: ioutil.NopCloser(bytes.NewReader(mockResp)),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
generalClient = clientMock
|
GeneralClient = clientMock
|
||||||
resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
||||||
SourceName: "source",
|
SourceName: "source",
|
||||||
})
|
})
|
||||||
|
@ -82,7 +82,7 @@ func TestRemoteCallback(t *testing.T) {
|
||||||
Body: ioutil.NopCloser(strings.NewReader("mockResp")),
|
Body: ioutil.NopCloser(strings.NewReader("mockResp")),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
generalClient = clientMock
|
GeneralClient = clientMock
|
||||||
resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
||||||
SourceName: "source",
|
SourceName: "source",
|
||||||
})
|
})
|
||||||
|
@ -106,7 +106,7 @@ func TestRemoteCallback(t *testing.T) {
|
||||||
Body: ioutil.NopCloser(strings.NewReader("mockResp")),
|
Body: ioutil.NopCloser(strings.NewReader("mockResp")),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
generalClient = clientMock
|
GeneralClient = clientMock
|
||||||
resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
||||||
SourceName: "source",
|
SourceName: "source",
|
||||||
})
|
})
|
||||||
|
@ -126,7 +126,7 @@ func TestRemoteCallback(t *testing.T) {
|
||||||
).Return(Response{
|
).Return(Response{
|
||||||
Err: errors.New("error"),
|
Err: errors.New("error"),
|
||||||
})
|
})
|
||||||
generalClient = clientMock
|
GeneralClient = clientMock
|
||||||
resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
||||||
SourceName: "source",
|
SourceName: "source",
|
||||||
})
|
})
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var generalClient Client = HTTPClient{}
|
var GeneralClient Client = HTTPClient{}
|
||||||
|
|
||||||
// Response 请求的响应或错误信息
|
// Response 请求的响应或错误信息
|
||||||
type Response struct {
|
type Response struct {
|
||||||
|
|
Loading…
Add table
Reference in a new issue