github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/znet/bind_value_test.go (about) 1 package znet 2 3 import ( 4 "testing" 5 6 "github.com/sohaha/zlsgo" 7 ) 8 9 func TestContext_Bind(t *testing.T) { 10 tt := zlsgo.NewTest(t) 11 r := newServer() 12 _ = newRequest(r, "POST", []string{"/TestContext_Bind", `{"id":666,"Pid":100,"name":"名字","g":{"Info":"基础"},"ids":[{"id":1,"Name":"用户1","g":{"Info":"详情","p":[{"id":1},{"id":2}]}}]}`, mimeJSON}, "/TestContext_Bind", func(c *Context) { 13 var s SS 14 err := c.Bind(&s) 15 t.Logf("%+v", s) 16 tt.EqualNil(err) 17 tt.Equal("名字", s.Name) 18 tt.Equal(1, s.IDs[0].Gg.P[0].ID) 19 }) 20 } 21 22 func TestContext_BindJSON(t *testing.T) { 23 tt := zlsgo.NewTest(t) 24 r := newServer() 25 _ = newRequest(r, "POST", []string{"/TestContext_BindJSON", `{"id":666,"Pid":100,"name":"名字","g":{"Info":"基础"},"ids":[{"id":1,"Name":"用户1","g":{"Info":"详情"}}]}`, mimeJSON}, "/TestContext_BindJSON", func(c *Context) { 26 var s SS 27 err := c.BindJSON(&s) 28 tt.Log(s) 29 tt.EqualNil(err) 30 tt.Equal("名字", s.Name) 31 }) 32 } 33 34 func TestContext_BindQuery(t *testing.T) { 35 tt := zlsgo.NewTest(t) 36 r := newServer() 37 _ = newRequest(r, "Get", "/TestContext_BindQuery?id=666&&t=1&t=2&t2=1&t2=2&g[Info]=基础&name=_name&ids[1][id]=123&ids[0][Name]=ids_0_name&p[n]=is pn&p[Key]=1.234", "/TestContext_BindQuery", func(c *Context) { 38 var s SS 39 err := c.BindQuery(&s) 40 tt.EqualNil(err) 41 tt.Equal("_name", s.Name) 42 tt.Equal("is pn", s.Property.Name) 43 tt.Equal(1, s.To2) 44 tt.Equal([]string{"1", "2"}, s.To) 45 t.Logf("%+v\n", s) 46 }) 47 } 48 49 func TestContext_BindForm(t *testing.T) { 50 tt := zlsgo.NewTest(t) 51 r := newServer() 52 _ = newRequest(r, "POST", []string{"/TestContext_BindForm", `id=666&&t=1&t=2&t2=1&t2=2&g[Info]=基础&name=_name&ids[1][id]=123&ids[0][Name]=ids_0_name&p[n]=is pn&p[Key]=1.234`, mimePOSTForm}, "/TestContext_BindForm", func(c *Context) { 53 var s SS 54 err := c.BindForm(&s) 55 tt.EqualNil(err) 56 tt.Equal("_name", s.Name) 57 tt.Equal("is pn", s.Property.Name) 58 tt.Equal(1, s.To2) 59 tt.Equal([]string{"1", "2"}, s.To) 60 t.Logf("%+v\n", s) 61 }) 62 }