github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_unit_request_xml_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package ghttp_test 8 9 import ( 10 "fmt" 11 "testing" 12 "time" 13 14 "github.com/gogf/gf/frame/g" 15 "github.com/gogf/gf/net/ghttp" 16 "github.com/gogf/gf/test/gtest" 17 ) 18 19 func Test_Params_Xml_Request(t *testing.T) { 20 type User struct { 21 Id int 22 Name string 23 Time *time.Time 24 Pass1 string `p:"password1"` 25 Pass2 string `p:"password2" v:"password2@required|length:2,20|password3|same:password1#||密码强度不足|两次密码不一致"` 26 } 27 p, _ := ports.PopRand() 28 s := g.Server(p) 29 s.BindHandler("/get", func(r *ghttp.Request) { 30 r.Response.WriteExit(r.Get("id"), r.Get("name")) 31 }) 32 s.BindHandler("/map", func(r *ghttp.Request) { 33 if m := r.GetMap(); len(m) > 0 { 34 r.Response.WriteExit(m["id"], m["name"], m["password1"], m["password2"]) 35 } 36 }) 37 s.BindHandler("/parse", func(r *ghttp.Request) { 38 if m := r.GetMap(); len(m) > 0 { 39 var user *User 40 if err := r.Parse(&user); err != nil { 41 r.Response.WriteExit(err) 42 } 43 r.Response.WriteExit(user.Id, user.Name, user.Pass1, user.Pass2) 44 } 45 }) 46 s.SetPort(p) 47 s.SetDumpRouterMap(false) 48 s.Start() 49 defer s.Shutdown() 50 51 time.Sleep(100 * time.Millisecond) 52 gtest.C(t, func(t *gtest.T) { 53 client := g.Client() 54 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 55 56 content1 := `<doc><id>1</id><name>john</name><password1>123Abc!@#</password1><password2>123Abc!@#</password2></doc>` 57 content2 := `<doc><id>1</id><name>john</name><password1>123Abc!@#</password1><password2>123</password2></doc>` 58 t.Assert(client.GetContent("/get", content1), ``) 59 t.Assert(client.PostContent("/get", content1), `1john`) 60 t.Assert(client.GetContent("/map", content1), ``) 61 t.Assert(client.PostContent("/map", content1), `1john123Abc!@#123Abc!@#`) 62 t.Assert(client.PostContent("/parse", content1), `1john123Abc!@#123Abc!@#`) 63 t.Assert(client.PostContent("/parse", content2), `密码强度不足; 两次密码不一致`) 64 }) 65 }