github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_feature_request_json_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/wangyougui/gf. 6 7 package ghttp_test 8 9 import ( 10 "fmt" 11 "testing" 12 "time" 13 14 "github.com/wangyougui/gf/v2/frame/g" 15 "github.com/wangyougui/gf/v2/internal/json" 16 "github.com/wangyougui/gf/v2/net/ghttp" 17 "github.com/wangyougui/gf/v2/test/gtest" 18 "github.com/wangyougui/gf/v2/util/guid" 19 ) 20 21 func Test_Params_Json_Request(t *testing.T) { 22 type User struct { 23 Id int 24 Name string 25 Time *time.Time 26 Pass1 string `p:"password1"` 27 Pass2 string `p:"password2" v:"password2@required|length:2,20|password3|same:password1#||密码强度不足|两次密码不一致"` 28 } 29 s := g.Server(guid.S()) 30 s.BindHandler("/get", func(r *ghttp.Request) { 31 r.Response.WriteExit(r.Get("id"), r.Get("name")) 32 }) 33 s.BindHandler("/map", func(r *ghttp.Request) { 34 if m := r.GetMap(); len(m) > 0 { 35 r.Response.WriteExit(m["id"], m["name"], m["password1"], m["password2"]) 36 } 37 }) 38 s.BindHandler("/parse", func(r *ghttp.Request) { 39 if m := r.GetMap(); len(m) > 0 { 40 var user *User 41 if err := r.Parse(&user); err != nil { 42 r.Response.WriteExit(err) 43 } 44 r.Response.WriteExit(user.Id, user.Name, user.Pass1, user.Pass2) 45 } 46 }) 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", s.GetListenedPort())) 55 56 t.Assert(client.GetContent(ctx, "/get", `{"id":1,"name":"john","password1":"123Abc!@#","password2":"123Abc!@#"}`), ``) 57 t.Assert(client.GetContent(ctx, "/map", `{"id":1,"name":"john","password1":"123Abc!@#","password2":"123Abc!@#"}`), ``) 58 t.Assert(client.PostContent(ctx, "/parse", `{"id":1,"name":"john","password1":"123Abc!@#","password2":"123Abc!@#"}`), `1john123Abc!@#123Abc!@#`) 59 t.Assert(client.PostContent(ctx, "/parse", `{"id":1,"name":"john","password1":"123Abc!@#","password2":"123"}`), `密码强度不足`) 60 }) 61 } 62 63 func Test_Params_Json_Response(t *testing.T) { 64 type User struct { 65 Uid int 66 Name string 67 SiteUrl string `json:"-"` 68 NickName string `json:"nickname,omitempty"` 69 Pass1 string `json:"password1"` 70 Pass2 string `json:"password2"` 71 } 72 73 s := g.Server(guid.S()) 74 s.BindHandler("/json1", func(r *ghttp.Request) { 75 r.Response.WriteJson(User{ 76 Uid: 100, 77 Name: "john", 78 SiteUrl: "https://goframe.org", 79 Pass1: "123", 80 Pass2: "456", 81 }) 82 }) 83 s.BindHandler("/json2", func(r *ghttp.Request) { 84 r.Response.WriteJson(&User{ 85 Uid: 100, 86 Name: "john", 87 SiteUrl: "https://goframe.org", 88 Pass1: "123", 89 Pass2: "456", 90 }) 91 }) 92 s.BindHandler("/json3", func(r *ghttp.Request) { 93 type Message struct { 94 Code int `json:"code"` 95 Body string `json:"body,omitempty"` 96 Error string `json:"error,omitempty"` 97 } 98 type ResponseJson struct { 99 Success bool `json:"success"` 100 Data interface{} `json:"data,omitempty"` 101 ExtData interface{} `json:"ext_data,omitempty"` 102 Paginate interface{} `json:"paginate,omitempty"` 103 Message Message `json:"message,omitempty"` 104 } 105 responseJson := &ResponseJson{ 106 Success: true, 107 Data: nil, 108 ExtData: nil, 109 Message: Message{3, "测试", "error"}, 110 } 111 r.Response.WriteJson(responseJson) 112 }) 113 s.BindHandler("/json4", func(r *ghttp.Request) { 114 type Message struct { 115 Code int `json:"code"` 116 Body string `json:"body,omitempty"` 117 Error string `json:"error,omitempty"` 118 } 119 type ResponseJson struct { 120 Success bool `json:"success"` 121 Data interface{} `json:"data,omitempty"` 122 ExtData interface{} `json:"ext_data,omitempty"` 123 Paginate interface{} `json:"paginate,omitempty"` 124 Message *Message `json:"message,omitempty"` 125 } 126 responseJson := ResponseJson{ 127 Success: true, 128 Data: nil, 129 ExtData: nil, 130 Message: &Message{3, "测试", "error"}, 131 } 132 r.Response.WriteJson(responseJson) 133 }) 134 s.SetDumpRouterMap(false) 135 s.Start() 136 defer s.Shutdown() 137 138 time.Sleep(100 * time.Millisecond) 139 gtest.C(t, func(t *gtest.T) { 140 client := g.Client() 141 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 142 143 map1 := make(map[string]interface{}) 144 err1 := json.UnmarshalUseNumber([]byte(client.GetContent(ctx, "/json1")), &map1) 145 t.Assert(err1, nil) 146 t.Assert(len(map1), 4) 147 t.Assert(map1["Name"], "john") 148 t.Assert(map1["Uid"], 100) 149 t.Assert(map1["password1"], "123") 150 t.Assert(map1["password2"], "456") 151 152 map2 := make(map[string]interface{}) 153 err2 := json.UnmarshalUseNumber([]byte(client.GetContent(ctx, "/json2")), &map2) 154 t.Assert(err2, nil) 155 t.Assert(len(map2), 4) 156 t.Assert(map2["Name"], "john") 157 t.Assert(map2["Uid"], 100) 158 t.Assert(map2["password1"], "123") 159 t.Assert(map2["password2"], "456") 160 161 map3 := make(map[string]interface{}) 162 err3 := json.UnmarshalUseNumber([]byte(client.GetContent(ctx, "/json3")), &map3) 163 t.Assert(err3, nil) 164 t.Assert(len(map3), 2) 165 t.Assert(map3["success"], "true") 166 t.Assert(map3["message"], g.Map{"body": "测试", "code": 3, "error": "error"}) 167 168 map4 := make(map[string]interface{}) 169 err4 := json.UnmarshalUseNumber([]byte(client.GetContent(ctx, "/json4")), &map4) 170 t.Assert(err4, nil) 171 t.Assert(len(map4), 2) 172 t.Assert(map4["success"], "true") 173 t.Assert(map4["message"], g.Map{"body": "测试", "code": 3, "error": "error"}) 174 }) 175 }