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