github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_unit_param_json_test.go (about) 1 // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf. 6 7 package ghttp_test 8 9 import ( 10 "encoding/json" 11 "fmt" 12 "github.com/zhongdalu/gf/g" 13 "github.com/zhongdalu/gf/g/net/ghttp" 14 "github.com/zhongdalu/gf/g/test/gtest" 15 "testing" 16 "time" 17 ) 18 19 func Test_Params_Json(t *testing.T) { 20 type User struct { 21 Uid int 22 Name string 23 SiteUrl string `gconv:"-"` 24 NickName string `gconv:"nickname, omitempty"` 25 Pass1 string `gconv:"password1"` 26 Pass2 string `gconv:"password2"` 27 } 28 29 p := ports.PopRand() 30 s := g.Server(p) 31 s.BindHandler("/json1", func(r *ghttp.Request) { 32 r.Response.WriteJson(User{ 33 Uid: 100, 34 Name: "john", 35 SiteUrl: "https://goframe.org", 36 Pass1: "123", 37 Pass2: "456", 38 }) 39 }) 40 s.BindHandler("/json2", func(r *ghttp.Request) { 41 r.Response.WriteJson(&User{ 42 Uid: 100, 43 Name: "john", 44 SiteUrl: "https://goframe.org", 45 Pass1: "123", 46 Pass2: "456", 47 }) 48 }) 49 s.BindHandler("/json3", func(r *ghttp.Request) { 50 type Message struct { 51 Code int `json:"code"` 52 Body string `json:"body,omitempty"` 53 Error string `json:"error,omitempty"` 54 } 55 type ResponseJson struct { 56 Success bool `json:"success"` 57 Data interface{} `json:"data,omitempty"` 58 ExtData interface{} `json:"ext_data,omitempty"` 59 Paginate interface{} `json:"paginate,omitempty"` 60 Message Message `json:"message,omitempty"` 61 } 62 responseJson := &ResponseJson{ 63 Success: true, 64 Data: nil, 65 ExtData: nil, 66 Message: Message{3, "测试", "error"}, 67 } 68 r.Response.WriteJson(responseJson) 69 }) 70 s.BindHandler("/json4", func(r *ghttp.Request) { 71 type Message struct { 72 Code int `json:"code"` 73 Body string `json:"body,omitempty"` 74 Error string `json:"error,omitempty"` 75 } 76 type ResponseJson struct { 77 Success bool `json:"success"` 78 Data interface{} `json:"data,omitempty"` 79 ExtData interface{} `json:"ext_data,omitempty"` 80 Paginate interface{} `json:"paginate,omitempty"` 81 Message *Message `json:"message,omitempty"` 82 } 83 responseJson := ResponseJson{ 84 Success: true, 85 Data: nil, 86 ExtData: nil, 87 Message: &Message{3, "测试", "error"}, 88 } 89 r.Response.WriteJson(responseJson) 90 }) 91 s.SetPort(p) 92 s.SetDumpRouteMap(false) 93 s.Start() 94 defer s.Shutdown() 95 96 // 等待启动完成 97 time.Sleep(time.Second) 98 gtest.Case(t, func() { 99 client := ghttp.NewClient() 100 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 101 102 map1 := make(map[string]interface{}) 103 err1 := json.Unmarshal([]byte(client.GetContent("/json1")), &map1) 104 gtest.Assert(err1, nil) 105 gtest.Assert(len(map1), 4) 106 gtest.Assert(map1["Name"], "john") 107 gtest.Assert(map1["Uid"], 100) 108 gtest.Assert(map1["password1"], "123") 109 gtest.Assert(map1["password2"], "456") 110 111 map2 := make(map[string]interface{}) 112 err2 := json.Unmarshal([]byte(client.GetContent("/json2")), &map2) 113 gtest.Assert(err2, nil) 114 gtest.Assert(len(map2), 4) 115 gtest.Assert(map2["Name"], "john") 116 gtest.Assert(map2["Uid"], 100) 117 gtest.Assert(map2["password1"], "123") 118 gtest.Assert(map2["password2"], "456") 119 120 map3 := make(map[string]interface{}) 121 err3 := json.Unmarshal([]byte(client.GetContent("/json3")), &map3) 122 gtest.Assert(err3, nil) 123 gtest.Assert(len(map3), 2) 124 gtest.Assert(map3["success"], "true") 125 gtest.Assert(map3["message"], g.Map{"body": "测试", "code": 3, "error": "error"}) 126 127 map4 := make(map[string]interface{}) 128 err4 := json.Unmarshal([]byte(client.GetContent("/json4")), &map4) 129 gtest.Assert(err4, nil) 130 gtest.Assert(len(map4), 2) 131 gtest.Assert(map4["success"], "true") 132 gtest.Assert(map4["message"], g.Map{"body": "测试", "code": 3, "error": "error"}) 133 }) 134 }