fusenapi/server/upload/test/uploadfilefrontendhandler_test.go

69 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package test
import (
"fmt"
"fusenapi/server/upload/internal/types"
fstests "fusenapi/utils/fstests"
"io/ioutil"
"log"
"testing"
"github.com/474420502/requests"
"github.com/tidwall/gjson"
)
func TestCaseUploadfileFrontend(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)
tp := ses.Post(fmt.Sprintf("http://%s:%d/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: "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)
}
if resp.GetStatusCode() != 200 {
t.Error("put failed")
}
}