Files
bj_power/bj_power_mes/internal/upload/handler_test.go
T

133 lines
3.4 KiB
Go

package upload
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"testing"
"bj_power_mes/ent"
"bj_power_mes/internal/config"
"bj_power_mes/internal/svc"
entsql "entgo.io/ent/dialect/sql"
_ "github.com/jackc/pgx/v5/stdlib"
"resty.dev/v3"
)
func TestHandleInspectionUpload(t *testing.T) {
// 创建临时上传目录
//tmpDir := t.TempDir()
svcCtx := newTestServiceContext(t, "F:\\Workspace\\Hardman\\back_cover\\uploads")
t.Run("上传成功", func(t *testing.T) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
_ = writer.WriteField("deviceId", "test")
part, _ := writer.CreateFormFile("file", "test.jpg")
data, _ := os.ReadFile("F:\\Workspace\\Hardman\\back_cover\\internal\\upload\\rog.png")
part.Write(data)
writer.Close()
req := httptest.NewRequest(http.MethodPost, "/document/upload", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
w := httptest.NewRecorder()
HandleInspectionUpload(svcCtx, w, req)
if w.Code != http.StatusOK {
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
}
var resp map[string]any
json.NewDecoder(w.Body).Decode(&resp)
if resp["code"] != 200 {
t.Errorf("expected code 200, got %v", resp["code"])
}
})
t.Run("缺少文件返回400", func(t *testing.T) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
_ = writer.WriteField("deviceId", "6")
writer.Close()
req := httptest.NewRequest(http.MethodPost, "/document/upload", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
w := httptest.NewRecorder()
HandleInspectionUpload(svcCtx, w, req)
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", w.Code)
}
})
t.Run("超大文件被拒绝", func(t *testing.T) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, _ := writer.CreateFormFile("file", "big.jpg")
// 写入超过 5MB 的数据
chunk := make([]byte, 1024)
for i := 0; i < 6*1024; i++ {
part.Write(chunk)
}
writer.Close()
req := httptest.NewRequest(http.MethodPost, "/document/upload", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
w := httptest.NewRecorder()
HandleInspectionUpload(svcCtx, w, req)
if w.Code == http.StatusOK {
t.Error("超大文件应被拒绝")
}
})
t.Run("内窥镜URL测试", func(t *testing.T) {
client := resty.New()
res, err := client.R().
SetMultipartFormData(map[string]string{
"userId": "0",
"deviceId": "test",
}).
SetFile("file", "F:\\Workspace\\Hardman\\back_cover\\internal\\upload\\rog.png").
//Post("http://47.115.54.30:8097/document/upload")
Post("http://localhost:8888/document/upload")
fmt.Println(err, res)
})
}
// newTestServiceContext 创建测试用的 ServiceContext,指向临时目录
func newTestServiceContext(t *testing.T, uploadDir string) *svc.ServiceContext {
t.Helper()
db, err := sql.Open("pgx", "postgresql://postgres:postgres@127.0.0.1:5432/back_cover?sslmode=disable")
if err != nil {
t.Skipf("跳过:无法连接数据库 (%v)", err)
}
drv := entsql.OpenDB("postgres", db)
entClient := ent.NewClient(ent.Driver(drv))
return &svc.ServiceContext{
Config: config.Config{
Upload: struct {
Dir string `json:",default=./uploads"`
RetentionDays int `json:",default=30"`
}{
Dir: uploadDir,
RetentionDays: -1,
},
},
EntClient: entClient,
}
}