github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_unit_param_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  	"fmt"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/zhongdalu/gf/g"
    15  	"github.com/zhongdalu/gf/g/net/ghttp"
    16  	"github.com/zhongdalu/gf/g/test/gtest"
    17  )
    18  
    19  func Test_Params_Basic(t *testing.T) {
    20  	type User struct {
    21  		Id    int
    22  		Name  string
    23  		Pass1 string `params:"password1"`
    24  		Pass2 string `params:"password2"`
    25  	}
    26  	p := ports.PopRand()
    27  	s := g.Server(p)
    28  	s.BindHandler("/get", func(r *ghttp.Request) {
    29  		if r.GetQuery("slice") != nil {
    30  			r.Response.Write(r.GetQuery("slice"))
    31  		}
    32  		if r.GetQuery("bool") != nil {
    33  			r.Response.Write(r.GetQueryBool("bool"))
    34  		}
    35  		if r.GetQuery("float32") != nil {
    36  			r.Response.Write(r.GetQueryFloat32("float32"))
    37  		}
    38  		if r.GetQuery("float64") != nil {
    39  			r.Response.Write(r.GetQueryFloat64("float64"))
    40  		}
    41  		if r.GetQuery("int") != nil {
    42  			r.Response.Write(r.GetQueryInt("int"))
    43  		}
    44  		if r.GetQuery("uint") != nil {
    45  			r.Response.Write(r.GetQueryUint("uint"))
    46  		}
    47  		if r.GetQuery("string") != nil {
    48  			r.Response.Write(r.GetQueryString("string"))
    49  		}
    50  	})
    51  	s.BindHandler("/post", func(r *ghttp.Request) {
    52  		if r.GetPost("slice") != nil {
    53  			r.Response.Write(r.GetPost("slice"))
    54  		}
    55  		if r.GetPost("bool") != nil {
    56  			r.Response.Write(r.GetPostBool("bool"))
    57  		}
    58  		if r.GetPost("float32") != nil {
    59  			r.Response.Write(r.GetPostFloat32("float32"))
    60  		}
    61  		if r.GetPost("float64") != nil {
    62  			r.Response.Write(r.GetPostFloat64("float64"))
    63  		}
    64  		if r.GetPost("int") != nil {
    65  			r.Response.Write(r.GetPostInt("int"))
    66  		}
    67  		if r.GetPost("uint") != nil {
    68  			r.Response.Write(r.GetPostUint("uint"))
    69  		}
    70  		if r.GetPost("string") != nil {
    71  			r.Response.Write(r.GetPostString("string"))
    72  		}
    73  	})
    74  	s.BindHandler("/map", func(r *ghttp.Request) {
    75  		if m := r.GetQueryMap(); len(m) > 0 {
    76  			r.Response.Write(m["name"])
    77  		}
    78  		if m := r.GetPostMap(); len(m) > 0 {
    79  			r.Response.Write(m["name"])
    80  		}
    81  	})
    82  	s.BindHandler("/raw", func(r *ghttp.Request) {
    83  		r.Response.Write(r.GetRaw())
    84  	})
    85  	s.BindHandler("/json", func(r *ghttp.Request) {
    86  		r.Response.Write(r.GetJson().Get("name"))
    87  	})
    88  	s.BindHandler("/struct", func(r *ghttp.Request) {
    89  		if m := r.GetQueryMap(); len(m) > 0 {
    90  			user := new(User)
    91  			r.GetQueryToStruct(user)
    92  			r.Response.Write(user.Id, user.Name, user.Pass1, user.Pass2)
    93  		}
    94  		if m := r.GetPostMap(); len(m) > 0 {
    95  			user := new(User)
    96  			r.GetPostToStruct(user)
    97  			r.Response.Write(user.Id, user.Name, user.Pass1, user.Pass2)
    98  		}
    99  	})
   100  	s.BindHandler("/struct-with-base", func(r *ghttp.Request) {
   101  		type Base struct {
   102  			Pass1 string `params:"password1"`
   103  			Pass2 string `params:"password2"`
   104  		}
   105  		type UserWithBase1 struct {
   106  			Id   int
   107  			Name string
   108  			Base
   109  		}
   110  		type UserWithBase2 struct {
   111  			Id   int
   112  			Name string
   113  			Pass Base
   114  		}
   115  		if m := r.GetPostMap(); len(m) > 0 {
   116  			user1 := new(UserWithBase1)
   117  			user2 := new(UserWithBase2)
   118  			r.GetToStruct(user1)
   119  			r.GetToStruct(user2)
   120  			r.Response.Write(user1.Id, user1.Name, user1.Pass1, user1.Pass2)
   121  			r.Response.Write(user2.Id, user2.Name, user2.Pass.Pass1, user2.Pass.Pass2)
   122  		}
   123  	})
   124  	s.SetPort(p)
   125  	s.SetDumpRouteMap(false)
   126  	s.Start()
   127  	defer s.Shutdown()
   128  
   129  	// 等待启动完成
   130  	time.Sleep(time.Second)
   131  	gtest.Case(t, func() {
   132  		client := ghttp.NewClient()
   133  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   134  		// GET
   135  		gtest.Assert(client.GetContent("/get", "slice=1&slice=2"), `["1","2"]`)
   136  		gtest.Assert(client.GetContent("/get", "bool=1"), `true`)
   137  		gtest.Assert(client.GetContent("/get", "bool=0"), `false`)
   138  		gtest.Assert(client.GetContent("/get", "float32=0.11"), `0.11`)
   139  		gtest.Assert(client.GetContent("/get", "float64=0.22"), `0.22`)
   140  		gtest.Assert(client.GetContent("/get", "int=-10000"), `-10000`)
   141  		gtest.Assert(client.GetContent("/get", "int=10000"), `10000`)
   142  		gtest.Assert(client.GetContent("/get", "uint=10000"), `10000`)
   143  		gtest.Assert(client.GetContent("/get", "uint=9"), `9`)
   144  		gtest.Assert(client.GetContent("/get", "string=key"), `key`)
   145  
   146  		// POST
   147  		gtest.Assert(client.PostContent("/post", "slice=1&slice=2"), `["1","2"]`)
   148  		gtest.Assert(client.PostContent("/post", "bool=1"), `true`)
   149  		gtest.Assert(client.PostContent("/post", "bool=0"), `false`)
   150  		gtest.Assert(client.PostContent("/post", "float32=0.11"), `0.11`)
   151  		gtest.Assert(client.PostContent("/post", "float64=0.22"), `0.22`)
   152  		gtest.Assert(client.PostContent("/post", "int=-10000"), `-10000`)
   153  		gtest.Assert(client.PostContent("/post", "int=10000"), `10000`)
   154  		gtest.Assert(client.PostContent("/post", "uint=10000"), `10000`)
   155  		gtest.Assert(client.PostContent("/post", "uint=9"), `9`)
   156  		gtest.Assert(client.PostContent("/post", "string=key"), `key`)
   157  
   158  		// Map
   159  		gtest.Assert(client.GetContent("/map", "id=1&name=john"), `john`)
   160  		gtest.Assert(client.PostContent("/map", "id=1&name=john"), `john`)
   161  
   162  		// Raw
   163  		gtest.Assert(client.PutContent("/raw", "id=1&name=john"), `id=1&name=john`)
   164  
   165  		// Json
   166  		gtest.Assert(client.PostContent("/json", `{"id":1, "name":"john"}`), `john`)
   167  
   168  		// Struct
   169  		gtest.Assert(client.GetContent("/struct", `id=1&name=john&password1=123&password2=456`), `1john123456`)
   170  		gtest.Assert(client.PostContent("/struct", `id=1&name=john&password1=123&password2=456`), `1john123456`)
   171  		gtest.Assert(client.PostContent("/struct-with-base", `id=1&name=john&password1=123&password2=456`), "1john1234561john123456")
   172  	})
   173  }