Test: new changes in pkg request, serializer, task, xml, router

This commit is contained in:
HFO4
2022-03-31 20:17:01 +08:00
parent d117080991
commit ec776ac837
9 changed files with 26 additions and 285 deletions

View File

@@ -82,6 +82,8 @@ func TestHTTPClient_Request(t *testing.T) {
WithTimeout(time.Duration(1)*time.Microsecond),
WithCredential(auth.HMACAuth{SecretKey: []byte("123")}, 10),
WithContext(context.Background()),
WithoutHeader([]string{"s s", "s s"}),
WithMasterMeta(),
)
asserts.Error(resp.Err)
asserts.Nil(resp.Response)

View File

@@ -0,0 +1,15 @@
package serializer
import (
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/stretchr/testify/assert"
"testing"
)
func TestBuildObjectList(t *testing.T) {
a := assert.New(t)
res := BuildObjectList(1, []Object{{}, {}}, &model.Policy{})
a.NotEmpty(res.Parent)
a.NotNil(res.Policy)
a.Len(res.Objects, 2)
}

View File

@@ -1,9 +1,7 @@
package serializer
import (
"encoding/base64"
"encoding/gob"
"encoding/json"
model "github.com/cloudreve/Cloudreve/v3/models"
"time"
)
@@ -64,32 +62,3 @@ type GeneralUploadCallbackFailed struct {
func init() {
gob.Register(UploadSession{})
}
// DecodeUploadPolicy 反序列化Header中携带的上传策略
func DecodeUploadPolicy(raw string) (*UploadPolicy, error) {
var res UploadPolicy
rawJSON, err := base64.StdEncoding.DecodeString(raw)
if err != nil {
return nil, err
}
err = json.Unmarshal(rawJSON, &res)
if err != nil {
return nil, err
}
return &res, err
}
// EncodeUploadPolicy 序列化Header中携带的上传策略
func (policy *UploadPolicy) EncodeUploadPolicy() (string, error) {
jsonRes, err := json.Marshal(policy)
if err != nil {
return "", err
}
res := base64.StdEncoding.EncodeToString(jsonRes)
return res, nil
}

View File

@@ -1,63 +0,0 @@
package serializer
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestDecodeUploadPolicy(t *testing.T) {
asserts := assert.New(t)
testCases := []struct {
input string
expectError bool
expectNil bool
expectRes *UploadPolicy
}{
{
"错误的base64字符",
true,
true,
&UploadPolicy{},
},
{
"6ZSZ6K+v55qESlNPTuWtl+espg==",
true,
true,
&UploadPolicy{},
},
{
"e30=",
false,
false,
&UploadPolicy{},
},
{
"eyJjYWxsYmFja191cmwiOiJ0ZXN0In0=",
false,
false,
&UploadPolicy{CallbackURL: "test"},
},
}
for _, testCase := range testCases {
res, err := DecodeUploadPolicy(testCase.input)
if testCase.expectError {
asserts.Error(err)
}
if testCase.expectNil {
asserts.Nil(res)
}
if !testCase.expectNil {
asserts.Equal(testCase.expectRes, res)
}
}
}
func TestUploadPolicy_EncodeUploadPolicy(t *testing.T) {
asserts := assert.New(t)
testPolicy := UploadPolicy{}
res, err := testPolicy.EncodeUploadPolicy()
asserts.NoError(err)
asserts.NotEmpty(res)
}

View File

@@ -29,23 +29,19 @@ func TestMain(m *testing.M) {
func TestBuildUser(t *testing.T) {
asserts := assert.New(t)
user := model.User{
Policy: model.Policy{MaxSize: 1024 * 1024},
}
user := model.User{}
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id"}))
res := BuildUser(user)
asserts.NoError(mock.ExpectationsWereMet())
asserts.Equal("1.00mb", res.Policy.MaxSize)
asserts.NotNil(res)
}
func TestBuildUserResponse(t *testing.T) {
asserts := assert.New(t)
user := model.User{
Policy: model.Policy{MaxSize: 1024 * 1024},
}
user := model.User{}
res := BuildUserResponse(user)
asserts.Equal("1.00mb", res.Data.(User).Policy.MaxSize)
asserts.NotNil(res)
}
func TestBuildUserStorageResponse(t *testing.T) {

View File

@@ -182,6 +182,7 @@ func TestImportTask_Do(t *testing.T) {
// 插入文件记录
mock.ExpectBegin()
mock.ExpectExec("INSERT(.+)files(.+)").WillReturnResult(sqlmock.NewResult(2, 1))
mock.ExpectExec("UPDATE(.+)users(.+)storage(.+)").WillReturnResult(sqlmock.NewResult(2, 1))
mock.ExpectCommit()
task.Do()

View File

@@ -1040,7 +1040,7 @@ Input:
d.buf.WriteByte(';')
n, err := strconv.ParseUint(s, base, 64)
if err == nil && n <= unicode.MaxRune {
text = string(n)
text = string(rune(n))
haveText = true
}
}
@@ -1063,7 +1063,7 @@ Input:
if isName(name) {
s := string(name)
if r, ok := entity[s]; ok {
text = string(r)
text = string(rune(r))
haveText = true
} else if d.Entity != nil {
text, haveText = d.Entity[s]