fusenapi/server/upload/test/uploadfilefrontendhandler_test.go

125 lines
2.7 KiB
Go
Raw Normal View History

2023-07-05 11:00:33 +00:00
package test
import (
"fmt"
"fusenapi/server/upload/internal/types"
"fusenapi/utils/format"
2023-07-05 11:00:33 +00:00
fstests "fusenapi/utils/fstests"
"io/ioutil"
"log"
"testing"
"github.com/474420502/requests"
"github.com/tidwall/gjson"
)
2023-07-07 06:49:02 +00:00
func TestCasePersonalization(t *testing.T) {
2023-07-05 11:00:33 +00:00
var err error
var resp *requests.Response
var result gjson.Result
// 获取 session并携带 JWT token
ses := fstests.GetSessionWithUserToken(t, userver, cnf.Host, cnf.Port)
2023-07-24 11:17:02 +00:00
tp := ses.Post(fmt.Sprintf("http://%s:%d/api/upload/upload-file-frontend", cnf.Host, cnf.Port))
2023-07-05 11:00:33 +00:00
data, err := ioutil.ReadFile("./fusen.webp")
if err != nil {
t.Error(err)
return
}
req := types.RequestUploadFileFrontend{
2023-07-05 11:00:33 +00:00
FileName: "fusen.webp", FileType: "image", FileSize: int64(len(data)),
Category: "personalization",
}
tp.SetBodyJson(req)
// 向服务器发送 GET 请求,获取用户基本信息
resp, err = tp.TestExecute(gserver)
if err != nil {
t.Error(err)
}
result = resp.Json()
if result.Get("code").Int() != 200 {
t.Error("code != 200")
}
if !result.Get("data").Exists() {
t.Error("data not exists")
}
if !result.Get("data.upload_url").Exists() {
t.Error("upload_url not exists")
}
log.Println(result)
uri := result.Get("data.upload_url").String()
tpUpload := requests.NewSession().Put(uri)
tpUpload.SetBodyStream(data)
resp, err = tpUpload.Execute()
if err != nil {
t.Error(err)
2023-07-05 11:00:33 +00:00
}
if resp.GetStatusCode() != 200 {
t.Error("put failed")
}
2023-07-05 11:00:33 +00:00
}
func TestCaseRenderMegreF(t *testing.T) {
var err error
var resp *requests.Response
var result gjson.Result
// 获取 session并携带 JWT token
ses := fstests.GetSessionWithUserToken(t, userver, cnf.Host, cnf.Port)
2023-07-24 11:17:02 +00:00
tp := ses.Post(fmt.Sprintf("http://%s:%d/api/upload/upload-file-frontend", cnf.Host, cnf.Port))
data, err := ioutil.ReadFile("./fusen.webp")
if err != nil {
t.Error(err)
return
}
req := types.RequestUploadFileFrontend{
FileName: "fusen.webp", FileType: "image", FileSize: int64(len(data)),
Category: string(format.TCategoryRenderMegre),
}
tp.SetBodyJson(req)
// 向服务器发送 GET 请求,获取用户基本信息
resp, err = tp.TestExecute(gserver)
if err != nil {
t.Error(err)
}
result = resp.Json()
if result.Get("code").Int() != 200 {
t.Error("code != 200")
}
if !result.Get("data").Exists() {
t.Error("data not exists")
}
if !result.Get("data.upload_url").Exists() {
t.Error("upload_url not exists")
}
log.Println(result)
uri := result.Get("data.upload_url").String()
tpUpload := requests.NewSession().Put(uri)
tpUpload.SetBodyStream(data)
resp, err = tpUpload.Execute()
if err != nil {
t.Error(err)
}
if resp.GetStatusCode() != 200 {
t.Error("put failed")
}
}