package handler import ( "bytes" "encoding/json" "net/http" "net/http/httptest" "testing" "github.com/gin-gonic/gin" ) func TestCreateRuleReqBindsName(t *testing.T) { var req createRuleReq err := json.Unmarshal([]byte(`{ "name":"高低分短线", "source_name":"crypto-strategy", "event":"HLSS.*", "template_name":"高低分短线" }`), &req) if err != nil { t.Fatal(err) } if req.Name != "高低分短线" { t.Fatalf("name=%q", req.Name) } } func TestCreateRuleRequiresName(t *testing.T) { gin.SetMode(gin.TestMode) h := NewRuleHandler(nil, nil) r := gin.New() r.POST("/api/v1/rules", h.Create) req := httptest.NewRequest(http.MethodPost, "/api/v1/rules", bytes.NewReader([]byte( `{"source_name":"s","event":"trade.open","template_name":"t"}`, ))) req.Header.Set("Content-Type", "application/json") w := httptest.NewRecorder() r.ServeHTTP(w, req) if w.Code != http.StatusBadRequest { t.Fatalf("code=%d body=%s", w.Code, w.Body.String()) } }