github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/znet/valid_test.go (about) 1 package znet 2 3 import ( 4 "testing" 5 6 "github.com/sohaha/zlsgo" 7 "github.com/sohaha/zlsgo/zvalid" 8 ) 9 10 func TestBindValid(t *testing.T) { 11 tt := zlsgo.NewTest(t) 12 r := newServer() 13 _ = newRequest(r, "POST", []string{"/TestBindValid", `{"id":666,"Pid":100,"name":"HelloWorld","g":{"Info":"基础"},"ids":[{"id":1,"Name":"用户1","g":{"Info":"详情","p":[{"id":1},{"id":2}]}}]}`, mimeJSON}, "/TestBindValid", func(c *Context) { 14 var s SS 15 r := c.ValidRule().Required() 16 err := c.BindValid(&s, map[string]zvalid.Engine{ 17 "id": r.IsNumber().Customize(func(rawValue string, err error) (newValue string, newErr error) { 18 newValue = "1999" 19 return 20 }), 21 "name": r.CamelCaseToSnakeCase(), 22 }) 23 t.Logf("%+v", s) 24 t.Log(err) 25 tt.EqualNil(err) 26 tt.Equal("hello_world", s.Name) 27 tt.Equal(1, s.IDs[0].Gg.P[0].ID) 28 }) 29 30 } 31 32 func TestContextValid(t *testing.T) { 33 tt := zlsgo.NewTest(t) 34 r := newServer() 35 w := newRequest(r, "POST", []string{ 36 "/TestContext_Valid?body2=b2", 37 "body=1 &b3=999", "application/x-www-form-urlencoded", 38 }, "/TestContext_Valid", func(c *Context) { 39 var rbool bool 40 rule := c.ValidRule().HasNumber() 41 42 v, err := c.Valid(rule, "body", "内容").MinLength(5).IsNumber().String() 43 44 tt.Equal(true, err != nil) 45 t.Log(v, err) 46 47 v, err = c.Valid(rule, "body2", "内容2").Required().MinLength(5).String() 48 tt.Equal(true, err != nil) 49 t.Log(v, err) 50 51 _, _ = c.ValidParam(rule, "body2").Required().String() 52 53 err = c.ValidQuery(rule, "body2", "内容2-2").Required().Error() 54 tt.Equal(true, err == nil) 55 t.Log(err) 56 57 rbool, err = c.ValidForm(rule, "body", "内容1-2").Required().Trim().Bool() 58 tt.Equal(true, err == nil) 59 tt.Equal(true, rbool) 60 t.Log(rbool, err) 61 62 c.String(200, expected) 63 }) 64 tt.Equal(200, w.Code) 65 tt.Equal(expected, w.Body.String()) 66 } 67 68 func TestContextBatchValid(tt *testing.T) { 69 t := zlsgo.NewTest(tt) 70 r := newServer() 71 w := newRequest(r, "POST", []string{ 72 "/TestContext_BatchValid?is=json", 73 `{"title":"这是json"}`, "application/json", 74 }, "/TestContext_BatchValid", func(c *Context) { 75 c.WithValue("varName", "666") 76 tt.Log("==前置") 77 c.Next() 78 tt.Log("==后置") 79 // var log bytes.Buffer 80 // rsp := io.MultiWriter(c.Writer, &log) 81 // tt.Log(rsp) 82 // tt.Log(log.String()) 83 // tt.Log(fmt.Sprintf("%#v", c.Request.Response)) 84 // tt.Log(fmt.Sprintf("%#v", c.Writer)) 85 // tt.Log(c.Request) 86 // tt.Log(c.Code) 87 }, func(c *Context) { 88 tt.Log("--前置2") 89 c.Abort() 90 c.Next() 91 }, func(c *Context) { 92 // rule := zvalid.New().HasNumber() 93 raw, _ := c.GetDataRaw() 94 tt.Log(raw) 95 tt.Log(c.Value("varName")) 96 tt.Log(c.Value("varName2")) 97 tt.Log(c.Value("varName3", []string{"a", "varName3"})) 98 99 json, err := c.GetJSONs() 100 tt.Log(json, err) 101 tt.Log(json.String()) 102 t.Equal(c.GetJSON("title").String(), json.Get("title").String()) 103 104 tt.Log(c.GetJSON("title")) 105 106 v, err := c.ValidJSON(c.ValidRule(), "title", "内容2-2").Required().String() 107 t.Equal(true, err == nil) 108 tt.Log(v, err) 109 110 c.String(200, "") 111 }) 112 t.Equal(200, w.Code) 113 }