github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_unit_request_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  	"bytes"
    11  	"fmt"
    12  	"io/ioutil"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/gogf/gf/encoding/gjson"
    17  
    18  	"github.com/gogf/gf/frame/g"
    19  	"github.com/gogf/gf/net/ghttp"
    20  	"github.com/gogf/gf/test/gtest"
    21  )
    22  
    23  func Test_Params_Basic(t *testing.T) {
    24  	type User struct {
    25  		Id    int
    26  		Name  string
    27  		Pass1 string `params:"password1"`
    28  		Pass2 string `params:"password2"`
    29  	}
    30  	p, _ := ports.PopRand()
    31  	s := g.Server(p)
    32  	// GET
    33  	s.BindHandler("/get", func(r *ghttp.Request) {
    34  		if r.GetQuery("array") != nil {
    35  			r.Response.Write(r.GetQuery("array"))
    36  		}
    37  		if r.GetQuery("slice") != nil {
    38  			r.Response.Write(r.GetQuery("slice"))
    39  		}
    40  		if r.GetQuery("bool") != nil {
    41  			r.Response.Write(r.GetQueryBool("bool"))
    42  		}
    43  		if r.GetQuery("float32") != nil {
    44  			r.Response.Write(r.GetQueryFloat32("float32"))
    45  		}
    46  		if r.GetQuery("float64") != nil {
    47  			r.Response.Write(r.GetQueryFloat64("float64"))
    48  		}
    49  		if r.GetQuery("int") != nil {
    50  			r.Response.Write(r.GetQueryInt("int"))
    51  		}
    52  		if r.GetQuery("uint") != nil {
    53  			r.Response.Write(r.GetQueryUint("uint"))
    54  		}
    55  		if r.GetQuery("string") != nil {
    56  			r.Response.Write(r.GetQueryString("string"))
    57  		}
    58  		if r.GetQuery("map") != nil {
    59  			r.Response.Write(r.GetQueryMap()["map"].(map[string]interface{})["b"])
    60  		}
    61  		if r.GetQuery("a") != nil {
    62  			r.Response.Write(r.GetQueryMapStrStr()["a"])
    63  		}
    64  	})
    65  	// PUT
    66  	s.BindHandler("/put", func(r *ghttp.Request) {
    67  		if r.Get("array") != nil {
    68  			r.Response.Write(r.Get("array"))
    69  		}
    70  		if r.Get("slice") != nil {
    71  			r.Response.Write(r.Get("slice"))
    72  		}
    73  		if r.Get("bool") != nil {
    74  			r.Response.Write(r.GetBool("bool"))
    75  		}
    76  		if r.Get("float32") != nil {
    77  			r.Response.Write(r.GetFloat32("float32"))
    78  		}
    79  		if r.Get("float64") != nil {
    80  			r.Response.Write(r.GetFloat64("float64"))
    81  		}
    82  		if r.Get("int") != nil {
    83  			r.Response.Write(r.GetInt("int"))
    84  		}
    85  		if r.Get("uint") != nil {
    86  			r.Response.Write(r.GetUint("uint"))
    87  		}
    88  		if r.Get("string") != nil {
    89  			r.Response.Write(r.GetString("string"))
    90  		}
    91  		if r.Get("map") != nil {
    92  			r.Response.Write(r.GetMap()["map"].(map[string]interface{})["b"])
    93  		}
    94  		if r.Get("a") != nil {
    95  			r.Response.Write(r.GetMapStrStr()["a"])
    96  		}
    97  	})
    98  	// POST
    99  	s.BindHandler("/post", func(r *ghttp.Request) {
   100  		if r.GetPost("array") != nil {
   101  			r.Response.Write(r.GetPost("array"))
   102  		}
   103  		if r.GetPost("slice") != nil {
   104  			r.Response.Write(r.GetPost("slice"))
   105  		}
   106  		if r.GetPost("bool") != nil {
   107  			r.Response.Write(r.GetPostBool("bool"))
   108  		}
   109  		if r.GetPost("float32") != nil {
   110  			r.Response.Write(r.GetPostFloat32("float32"))
   111  		}
   112  		if r.GetPost("float64") != nil {
   113  			r.Response.Write(r.GetPostFloat64("float64"))
   114  		}
   115  		if r.GetPost("int") != nil {
   116  			r.Response.Write(r.GetPostInt("int"))
   117  		}
   118  		if r.GetPost("uint") != nil {
   119  			r.Response.Write(r.GetPostUint("uint"))
   120  		}
   121  		if r.GetPost("string") != nil {
   122  			r.Response.Write(r.GetPostString("string"))
   123  		}
   124  		if r.GetPost("map") != nil {
   125  			r.Response.Write(r.GetPostMap()["map"].(map[string]interface{})["b"])
   126  		}
   127  		if r.GetPost("a") != nil {
   128  			r.Response.Write(r.GetPostMapStrStr()["a"])
   129  		}
   130  	})
   131  	// DELETE
   132  	s.BindHandler("/delete", func(r *ghttp.Request) {
   133  		if r.Get("array") != nil {
   134  			r.Response.Write(r.Get("array"))
   135  		}
   136  		if r.Get("slice") != nil {
   137  			r.Response.Write(r.Get("slice"))
   138  		}
   139  		if r.Get("bool") != nil {
   140  			r.Response.Write(r.GetBool("bool"))
   141  		}
   142  		if r.Get("float32") != nil {
   143  			r.Response.Write(r.GetFloat32("float32"))
   144  		}
   145  		if r.Get("float64") != nil {
   146  			r.Response.Write(r.GetFloat64("float64"))
   147  		}
   148  		if r.Get("int") != nil {
   149  			r.Response.Write(r.GetInt("int"))
   150  		}
   151  		if r.Get("uint") != nil {
   152  			r.Response.Write(r.GetUint("uint"))
   153  		}
   154  		if r.Get("string") != nil {
   155  			r.Response.Write(r.GetString("string"))
   156  		}
   157  		if r.Get("map") != nil {
   158  			r.Response.Write(r.GetMap()["map"].(map[string]interface{})["b"])
   159  		}
   160  		if r.Get("a") != nil {
   161  			r.Response.Write(r.GetMapStrStr()["a"])
   162  		}
   163  	})
   164  	// PATCH
   165  	s.BindHandler("/patch", func(r *ghttp.Request) {
   166  		if r.Get("array") != nil {
   167  			r.Response.Write(r.Get("array"))
   168  		}
   169  		if r.Get("slice") != nil {
   170  			r.Response.Write(r.Get("slice"))
   171  		}
   172  		if r.Get("bool") != nil {
   173  			r.Response.Write(r.GetBool("bool"))
   174  		}
   175  		if r.Get("float32") != nil {
   176  			r.Response.Write(r.GetFloat32("float32"))
   177  		}
   178  		if r.Get("float64") != nil {
   179  			r.Response.Write(r.GetFloat64("float64"))
   180  		}
   181  		if r.Get("int") != nil {
   182  			r.Response.Write(r.GetInt("int"))
   183  		}
   184  		if r.Get("uint") != nil {
   185  			r.Response.Write(r.GetUint("uint"))
   186  		}
   187  		if r.Get("string") != nil {
   188  			r.Response.Write(r.GetString("string"))
   189  		}
   190  		if r.Get("map") != nil {
   191  			r.Response.Write(r.GetMap()["map"].(map[string]interface{})["b"])
   192  		}
   193  		if r.Get("a") != nil {
   194  			r.Response.Write(r.GetMapStrStr()["a"])
   195  		}
   196  	})
   197  	// Form
   198  	s.BindHandler("/form", func(r *ghttp.Request) {
   199  		if r.Get("array") != nil {
   200  			r.Response.Write(r.GetForm("array"))
   201  		}
   202  		if r.Get("slice") != nil {
   203  			r.Response.Write(r.GetForm("slice"))
   204  		}
   205  		if r.Get("bool") != nil {
   206  			r.Response.Write(r.GetFormBool("bool"))
   207  		}
   208  		if r.Get("float32") != nil {
   209  			r.Response.Write(r.GetFormFloat32("float32"))
   210  		}
   211  		if r.Get("float64") != nil {
   212  			r.Response.Write(r.GetFormFloat64("float64"))
   213  		}
   214  		if r.Get("int") != nil {
   215  			r.Response.Write(r.GetFormInt("int"))
   216  		}
   217  		if r.Get("uint") != nil {
   218  			r.Response.Write(r.GetFormUint("uint"))
   219  		}
   220  		if r.Get("string") != nil {
   221  			r.Response.Write(r.GetFormString("string"))
   222  		}
   223  		if r.Get("map") != nil {
   224  			r.Response.Write(r.GetFormMap()["map"].(map[string]interface{})["b"])
   225  		}
   226  		if r.Get("a") != nil {
   227  			r.Response.Write(r.GetFormMapStrStr()["a"])
   228  		}
   229  	})
   230  	s.BindHandler("/map", func(r *ghttp.Request) {
   231  		if m := r.GetQueryMap(); len(m) > 0 {
   232  			r.Response.Write(m["name"])
   233  			return
   234  		}
   235  		if m := r.GetMap(); len(m) > 0 {
   236  			r.Response.Write(m["name"])
   237  			return
   238  		}
   239  	})
   240  	s.BindHandler("/raw", func(r *ghttp.Request) {
   241  		r.Response.Write(r.GetRaw())
   242  	})
   243  	s.BindHandler("/json", func(r *ghttp.Request) {
   244  		j, err := r.GetJson()
   245  		if err != nil {
   246  			r.Response.Write(err)
   247  			return
   248  		}
   249  		r.Response.Write(j.Get("name"))
   250  	})
   251  	s.BindHandler("/struct", func(r *ghttp.Request) {
   252  		if m := r.GetQueryMap(); len(m) > 0 {
   253  			user := new(User)
   254  			r.GetQueryStruct(user)
   255  			r.Response.Write(user.Id, user.Name, user.Pass1, user.Pass2)
   256  			return
   257  		}
   258  		if m := r.GetMap(); len(m) > 0 {
   259  			user := new(User)
   260  			r.GetStruct(user)
   261  			r.Response.Write(user.Id, user.Name, user.Pass1, user.Pass2)
   262  			return
   263  		}
   264  	})
   265  	s.BindHandler("/struct-with-nil", func(r *ghttp.Request) {
   266  		user := (*User)(nil)
   267  		err := r.GetStruct(&user)
   268  		r.Response.Write(err)
   269  	})
   270  	s.BindHandler("/struct-with-base", func(r *ghttp.Request) {
   271  		type Base struct {
   272  			Pass1 string `params:"password1"`
   273  			Pass2 string `params:"password2"`
   274  		}
   275  		type UserWithBase1 struct {
   276  			Id   int
   277  			Name string
   278  			Base
   279  		}
   280  		type UserWithBase2 struct {
   281  			Id   int
   282  			Name string
   283  			Pass Base
   284  		}
   285  		if m := r.GetMap(); len(m) > 0 {
   286  			user1 := new(UserWithBase1)
   287  			user2 := new(UserWithBase2)
   288  			r.GetStruct(user1)
   289  			r.GetStruct(user2)
   290  			r.Response.Write(user1.Id, user1.Name, user1.Pass1, user1.Pass2)
   291  			r.Response.Write(user2.Id, user2.Name, user2.Pass.Pass1, user2.Pass.Pass2)
   292  		}
   293  	})
   294  	s.SetPort(p)
   295  	s.SetDumpRouterMap(false)
   296  	s.Start()
   297  	defer s.Shutdown()
   298  
   299  	time.Sleep(100 * time.Millisecond)
   300  	gtest.C(t, func(t *gtest.T) {
   301  		client := g.Client()
   302  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   303  		// GET
   304  		t.Assert(client.GetContent("/get", "array[]=1&array[]=2"), `["1","2"]`)
   305  		t.Assert(client.GetContent("/get", "slice=1&slice=2"), `2`)
   306  		t.Assert(client.GetContent("/get", "bool=1"), `true`)
   307  		t.Assert(client.GetContent("/get", "bool=0"), `false`)
   308  		t.Assert(client.GetContent("/get", "float32=0.11"), `0.11`)
   309  		t.Assert(client.GetContent("/get", "float64=0.22"), `0.22`)
   310  		t.Assert(client.GetContent("/get", "int=-10000"), `-10000`)
   311  		t.Assert(client.GetContent("/get", "int=10000"), `10000`)
   312  		t.Assert(client.GetContent("/get", "uint=10000"), `10000`)
   313  		t.Assert(client.GetContent("/get", "uint=9"), `9`)
   314  		t.Assert(client.GetContent("/get", "string=key"), `key`)
   315  		t.Assert(client.GetContent("/get", "map[a]=1&map[b]=2"), `2`)
   316  		t.Assert(client.GetContent("/get", "a=1&b=2"), `1`)
   317  
   318  		// PUT
   319  		t.Assert(client.PutContent("/put", "array[]=1&array[]=2"), `["1","2"]`)
   320  		t.Assert(client.PutContent("/put", "slice=1&slice=2"), `2`)
   321  		t.Assert(client.PutContent("/put", "bool=1"), `true`)
   322  		t.Assert(client.PutContent("/put", "bool=0"), `false`)
   323  		t.Assert(client.PutContent("/put", "float32=0.11"), `0.11`)
   324  		t.Assert(client.PutContent("/put", "float64=0.22"), `0.22`)
   325  		t.Assert(client.PutContent("/put", "int=-10000"), `-10000`)
   326  		t.Assert(client.PutContent("/put", "int=10000"), `10000`)
   327  		t.Assert(client.PutContent("/put", "uint=10000"), `10000`)
   328  		t.Assert(client.PutContent("/put", "uint=9"), `9`)
   329  		t.Assert(client.PutContent("/put", "string=key"), `key`)
   330  		t.Assert(client.PutContent("/put", "map[a]=1&map[b]=2"), `2`)
   331  		t.Assert(client.PutContent("/put", "a=1&b=2"), `1`)
   332  
   333  		// POST
   334  		t.Assert(client.PostContent("/post", "array[]=1&array[]=2"), `["1","2"]`)
   335  		t.Assert(client.PostContent("/post", "slice=1&slice=2"), `2`)
   336  		t.Assert(client.PostContent("/post", "bool=1"), `true`)
   337  		t.Assert(client.PostContent("/post", "bool=0"), `false`)
   338  		t.Assert(client.PostContent("/post", "float32=0.11"), `0.11`)
   339  		t.Assert(client.PostContent("/post", "float64=0.22"), `0.22`)
   340  		t.Assert(client.PostContent("/post", "int=-10000"), `-10000`)
   341  		t.Assert(client.PostContent("/post", "int=10000"), `10000`)
   342  		t.Assert(client.PostContent("/post", "uint=10000"), `10000`)
   343  		t.Assert(client.PostContent("/post", "uint=9"), `9`)
   344  		t.Assert(client.PostContent("/post", "string=key"), `key`)
   345  		t.Assert(client.PostContent("/post", "map[a]=1&map[b]=2"), `2`)
   346  		t.Assert(client.PostContent("/post", "a=1&b=2"), `1`)
   347  
   348  		// DELETE
   349  		t.Assert(client.DeleteContent("/delete", "array[]=1&array[]=2"), `["1","2"]`)
   350  		t.Assert(client.DeleteContent("/delete", "slice=1&slice=2"), `2`)
   351  		t.Assert(client.DeleteContent("/delete", "bool=1"), `true`)
   352  		t.Assert(client.DeleteContent("/delete", "bool=0"), `false`)
   353  		t.Assert(client.DeleteContent("/delete", "float32=0.11"), `0.11`)
   354  		t.Assert(client.DeleteContent("/delete", "float64=0.22"), `0.22`)
   355  		t.Assert(client.DeleteContent("/delete", "int=-10000"), `-10000`)
   356  		t.Assert(client.DeleteContent("/delete", "int=10000"), `10000`)
   357  		t.Assert(client.DeleteContent("/delete", "uint=10000"), `10000`)
   358  		t.Assert(client.DeleteContent("/delete", "uint=9"), `9`)
   359  		t.Assert(client.DeleteContent("/delete", "string=key"), `key`)
   360  		t.Assert(client.DeleteContent("/delete", "map[a]=1&map[b]=2"), `2`)
   361  		t.Assert(client.DeleteContent("/delete", "a=1&b=2"), `1`)
   362  
   363  		// PATCH
   364  		t.Assert(client.PatchContent("/patch", "array[]=1&array[]=2"), `["1","2"]`)
   365  		t.Assert(client.PatchContent("/patch", "slice=1&slice=2"), `2`)
   366  		t.Assert(client.PatchContent("/patch", "bool=1"), `true`)
   367  		t.Assert(client.PatchContent("/patch", "bool=0"), `false`)
   368  		t.Assert(client.PatchContent("/patch", "float32=0.11"), `0.11`)
   369  		t.Assert(client.PatchContent("/patch", "float64=0.22"), `0.22`)
   370  		t.Assert(client.PatchContent("/patch", "int=-10000"), `-10000`)
   371  		t.Assert(client.PatchContent("/patch", "int=10000"), `10000`)
   372  		t.Assert(client.PatchContent("/patch", "uint=10000"), `10000`)
   373  		t.Assert(client.PatchContent("/patch", "uint=9"), `9`)
   374  		t.Assert(client.PatchContent("/patch", "string=key"), `key`)
   375  		t.Assert(client.PatchContent("/patch", "map[a]=1&map[b]=2"), `2`)
   376  		t.Assert(client.PatchContent("/patch", "a=1&b=2"), `1`)
   377  
   378  		// Form
   379  		t.Assert(client.PostContent("/form", "array[]=1&array[]=2"), `["1","2"]`)
   380  		t.Assert(client.PostContent("/form", "slice=1&slice=2"), `2`)
   381  		t.Assert(client.PostContent("/form", "bool=1"), `true`)
   382  		t.Assert(client.PostContent("/form", "bool=0"), `false`)
   383  		t.Assert(client.PostContent("/form", "float32=0.11"), `0.11`)
   384  		t.Assert(client.PostContent("/form", "float64=0.22"), `0.22`)
   385  		t.Assert(client.PostContent("/form", "int=-10000"), `-10000`)
   386  		t.Assert(client.PostContent("/form", "int=10000"), `10000`)
   387  		t.Assert(client.PostContent("/form", "uint=10000"), `10000`)
   388  		t.Assert(client.PostContent("/form", "uint=9"), `9`)
   389  		t.Assert(client.PostContent("/form", "string=key"), `key`)
   390  		t.Assert(client.PostContent("/form", "map[a]=1&map[b]=2"), `2`)
   391  		t.Assert(client.PostContent("/form", "a=1&b=2"), `1`)
   392  
   393  		// Map
   394  		t.Assert(client.GetContent("/map", "id=1&name=john"), `john`)
   395  		t.Assert(client.PostContent("/map", "id=1&name=john"), `john`)
   396  
   397  		// Raw
   398  		t.Assert(client.PutContent("/raw", "id=1&name=john"), `id=1&name=john`)
   399  
   400  		// Json
   401  		t.Assert(client.PostContent("/json", `{"id":1, "name":"john"}`), `john`)
   402  
   403  		// Struct
   404  		t.Assert(client.GetContent("/struct", `id=1&name=john&password1=123&password2=456`), `1john123456`)
   405  		t.Assert(client.PostContent("/struct", `id=1&name=john&password1=123&password2=456`), `1john123456`)
   406  		t.Assert(client.PostContent("/struct-with-nil", ``), ``)
   407  		t.Assert(client.PostContent("/struct-with-base", `id=1&name=john&password1=123&password2=456`), "1john1234561john")
   408  	})
   409  }
   410  
   411  func Test_Params_Header(t *testing.T) {
   412  	p, _ := ports.PopRand()
   413  	s := g.Server(p)
   414  	s.BindHandler("/header", func(r *ghttp.Request) {
   415  		r.Response.Write(r.GetHeader("test"))
   416  	})
   417  	s.SetPort(p)
   418  	s.SetDumpRouterMap(false)
   419  	s.Start()
   420  	defer s.Shutdown()
   421  
   422  	time.Sleep(100 * time.Millisecond)
   423  	gtest.C(t, func(t *gtest.T) {
   424  		prefix := fmt.Sprintf("http://127.0.0.1:%d", p)
   425  		client := g.Client()
   426  		client.SetPrefix(prefix)
   427  
   428  		t.Assert(client.Header(g.MapStrStr{"test": "123456"}).GetContent("/header"), "123456")
   429  	})
   430  }
   431  
   432  func Test_Params_SupportChars(t *testing.T) {
   433  	p, _ := ports.PopRand()
   434  	s := g.Server(p)
   435  	s.BindHandler("/form-value", func(r *ghttp.Request) {
   436  		r.Response.Write(r.GetForm("test-value"))
   437  	})
   438  	s.BindHandler("/form-array", func(r *ghttp.Request) {
   439  		r.Response.Write(r.GetForm("test-array"))
   440  	})
   441  	s.SetPort(p)
   442  	s.SetDumpRouterMap(false)
   443  	s.Start()
   444  	defer s.Shutdown()
   445  
   446  	time.Sleep(100 * time.Millisecond)
   447  	gtest.C(t, func(t *gtest.T) {
   448  		c := g.Client()
   449  		c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   450  		t.Assert(c.PostContent("/form-value", "test-value=100"), "100")
   451  		t.Assert(c.PostContent("/form-array", "test-array[]=1&test-array[]=2"), `["1","2"]`)
   452  	})
   453  }
   454  
   455  func Test_Params_Priority(t *testing.T) {
   456  	p, _ := ports.PopRand()
   457  	s := g.Server(p)
   458  	s.BindHandler("/query", func(r *ghttp.Request) {
   459  		r.Response.Write(r.GetQuery("a"))
   460  	})
   461  	s.BindHandler("/post", func(r *ghttp.Request) {
   462  		r.Response.Write(r.GetPost("a"))
   463  	})
   464  	s.BindHandler("/form", func(r *ghttp.Request) {
   465  		r.Response.Write(r.GetForm("a"))
   466  	})
   467  	s.BindHandler("/request", func(r *ghttp.Request) {
   468  		r.Response.Write(r.Get("a"))
   469  	})
   470  	s.BindHandler("/request-map", func(r *ghttp.Request) {
   471  		r.Response.Write(r.GetMap(g.Map{
   472  			"a": 1,
   473  			"b": 2,
   474  		}))
   475  	})
   476  	s.SetPort(p)
   477  	s.SetDumpRouterMap(false)
   478  	s.Start()
   479  	defer s.Shutdown()
   480  
   481  	time.Sleep(100 * time.Millisecond)
   482  	gtest.C(t, func(t *gtest.T) {
   483  		prefix := fmt.Sprintf("http://127.0.0.1:%d", p)
   484  		client := g.Client()
   485  		client.SetPrefix(prefix)
   486  
   487  		t.Assert(client.GetContent("/query?a=1", "a=100"), "100")
   488  		t.Assert(client.PostContent("/post?a=1", "a=100"), "100")
   489  		t.Assert(client.PostContent("/form?a=1", "a=100"), "100")
   490  		t.Assert(client.PutContent("/form?a=1", "a=100"), "100")
   491  		t.Assert(client.GetContent("/request?a=1", "a=100"), "100")
   492  		t.Assert(client.GetContent("/request-map?a=1&b=2&c=3", "a=100&b=200&c=300"), `{"a":"100","b":"200"}`)
   493  	})
   494  }
   495  
   496  func Test_Params_GetRequestMap(t *testing.T) {
   497  	p, _ := ports.PopRand()
   498  	s := g.Server(p)
   499  	s.BindHandler("/map", func(r *ghttp.Request) {
   500  		r.Response.Write(r.GetRequestMap())
   501  	})
   502  	s.SetPort(p)
   503  	s.SetDumpRouterMap(false)
   504  	s.Start()
   505  	defer s.Shutdown()
   506  
   507  	time.Sleep(100 * time.Millisecond)
   508  	gtest.C(t, func(t *gtest.T) {
   509  		prefix := fmt.Sprintf("http://127.0.0.1:%d", p)
   510  		client := g.Client()
   511  		client.SetPrefix(prefix)
   512  
   513  		t.Assert(
   514  			client.PostContent(
   515  				"/map",
   516  				"time_end2020-04-18 16:11:58&returnmsg=Success&attach=",
   517  			),
   518  			`{"attach":"","returnmsg":"Success"}`,
   519  		)
   520  	})
   521  }
   522  
   523  func Test_Params_Modify(t *testing.T) {
   524  	p, _ := ports.PopRand()
   525  	s := g.Server(p)
   526  	s.BindHandler("/param/modify", func(r *ghttp.Request) {
   527  		param := r.GetMap()
   528  		param["id"] = 2
   529  		paramBytes, err := gjson.Encode(param)
   530  		if err != nil {
   531  			r.Response.Write(err)
   532  			return
   533  		}
   534  		r.Request.Body = ioutil.NopCloser(bytes.NewReader(paramBytes))
   535  		r.ReloadParam()
   536  		r.Response.Write(r.GetMap())
   537  	})
   538  	s.SetPort(p)
   539  	s.SetDumpRouterMap(false)
   540  	s.Start()
   541  	defer s.Shutdown()
   542  
   543  	time.Sleep(100 * time.Millisecond)
   544  	gtest.C(t, func(t *gtest.T) {
   545  		prefix := fmt.Sprintf("http://127.0.0.1:%d", p)
   546  		client := g.Client()
   547  		client.SetPrefix(prefix)
   548  
   549  		t.Assert(
   550  			client.PostContent(
   551  				"/param/modify",
   552  				`{"id":1}`,
   553  			),
   554  			`{"id":2}`,
   555  		)
   556  	})
   557  }
   558  
   559  func Test_Params_Parse_DefaultValueTag(t *testing.T) {
   560  	type T struct {
   561  		Name  string  `d:"john"`
   562  		Score float32 `d:"60"`
   563  	}
   564  	p, _ := ports.PopRand()
   565  	s := g.Server(p)
   566  	s.BindHandler("/parse", func(r *ghttp.Request) {
   567  		var t *T
   568  		if err := r.Parse(&t); err != nil {
   569  			r.Response.WriteExit(err)
   570  		}
   571  		r.Response.WriteExit(t)
   572  	})
   573  	s.SetPort(p)
   574  	s.SetDumpRouterMap(false)
   575  	s.Start()
   576  	defer s.Shutdown()
   577  
   578  	time.Sleep(100 * time.Millisecond)
   579  	gtest.C(t, func(t *gtest.T) {
   580  		prefix := fmt.Sprintf("http://127.0.0.1:%d", p)
   581  		client := g.Client()
   582  		client.SetPrefix(prefix)
   583  
   584  		t.Assert(client.PostContent("/parse"), `{"Name":"john","Score":60}`)
   585  		t.Assert(client.PostContent("/parse", `{"name":"smith"}`), `{"Name":"smith","Score":60}`)
   586  		t.Assert(client.PostContent("/parse", `{"name":"smith", "score":100}`), `{"Name":"smith","Score":100}`)
   587  	})
   588  }
   589  
   590  func Test_Params_Parse_Validation(t *testing.T) {
   591  	type RegisterReq struct {
   592  		Name  string `p:"username"  v:"required|length:6,30#请输入账号|账号长度为:min到:max位"`
   593  		Pass  string `p:"password1" v:"required|length:6,30#请输入密码|密码长度不够"`
   594  		Pass2 string `p:"password2" v:"required|length:6,30|same:password1#请确认密码|密码长度不够|两次密码不一致"`
   595  	}
   596  
   597  	p, _ := ports.PopRand()
   598  	s := g.Server(p)
   599  	s.BindHandler("/parse", func(r *ghttp.Request) {
   600  		var req *RegisterReq
   601  		if err := r.Parse(&req); err != nil {
   602  			r.Response.Write(err)
   603  		} else {
   604  			r.Response.Write("ok")
   605  		}
   606  	})
   607  	s.SetPort(p)
   608  	s.SetDumpRouterMap(false)
   609  	s.Start()
   610  	defer s.Shutdown()
   611  
   612  	time.Sleep(100 * time.Millisecond)
   613  	gtest.C(t, func(t *gtest.T) {
   614  		prefix := fmt.Sprintf("http://127.0.0.1:%d", p)
   615  		client := g.Client()
   616  		client.SetPrefix(prefix)
   617  
   618  		t.Assert(client.GetContent("/parse"), `请输入账号; 账号长度为6到30位; 请输入密码; 密码长度不够; 请确认密码; 密码长度不够; 两次密码不一致`)
   619  		t.Assert(client.GetContent("/parse?name=john11&password1=123456&password2=123"), `密码长度不够; 两次密码不一致`)
   620  		t.Assert(client.GetContent("/parse?name=john&password1=123456&password2=123456"), `账号长度为6到30位`)
   621  		t.Assert(client.GetContent("/parse?name=john11&password1=123456&password2=123456"), `ok`)
   622  	})
   623  }
   624  
   625  func Test_Params_Parse_EmbeddedWithAliasName1(t *testing.T) {
   626  	// 获取内容列表
   627  	type ContentGetListInput struct {
   628  		Type       string
   629  		CategoryId uint
   630  		Page       int
   631  		Size       int
   632  		Sort       int
   633  		UserId     uint
   634  	}
   635  	// 获取内容列表
   636  	type ContentGetListReq struct {
   637  		ContentGetListInput
   638  		CategoryId uint `p:"cate"`
   639  		Page       int  `d:"1"  v:"min:0#分页号码错误"`
   640  		Size       int  `d:"10" v:"max:50#分页数量最大50条"`
   641  	}
   642  
   643  	p, _ := ports.PopRand()
   644  	s := g.Server(p)
   645  	s.BindHandler("/parse", func(r *ghttp.Request) {
   646  		var req *ContentGetListReq
   647  		if err := r.Parse(&req); err != nil {
   648  			r.Response.Write(err)
   649  		} else {
   650  			r.Response.Write(req.ContentGetListInput)
   651  		}
   652  	})
   653  	s.SetPort(p)
   654  	s.SetDumpRouterMap(false)
   655  	s.Start()
   656  	defer s.Shutdown()
   657  
   658  	time.Sleep(100 * time.Millisecond)
   659  	gtest.C(t, func(t *gtest.T) {
   660  		prefix := fmt.Sprintf("http://127.0.0.1:%d", p)
   661  		client := g.Client()
   662  		client.SetPrefix(prefix)
   663  
   664  		t.Assert(client.GetContent("/parse?cate=1&page=2&size=10"), `{"Type":"","CategoryId":0,"Page":2,"Size":10,"Sort":0,"UserId":0}`)
   665  	})
   666  }
   667  
   668  func Test_Params_Parse_EmbeddedWithAliasName2(t *testing.T) {
   669  	// 获取内容列表
   670  	type ContentGetListInput struct {
   671  		Type       string
   672  		CategoryId uint `p:"cate"`
   673  		Page       int
   674  		Size       int
   675  		Sort       int
   676  		UserId     uint
   677  	}
   678  	// 获取内容列表
   679  	type ContentGetListReq struct {
   680  		ContentGetListInput
   681  		CategoryId uint `p:"cate"`
   682  		Page       int  `d:"1"  v:"min:0#分页号码错误"`
   683  		Size       int  `d:"10" v:"max:50#分页数量最大50条"`
   684  	}
   685  
   686  	p, _ := ports.PopRand()
   687  	s := g.Server(p)
   688  	s.BindHandler("/parse", func(r *ghttp.Request) {
   689  		var req *ContentGetListReq
   690  		if err := r.Parse(&req); err != nil {
   691  			r.Response.Write(err)
   692  		} else {
   693  			r.Response.Write(req.ContentGetListInput)
   694  		}
   695  	})
   696  	s.SetPort(p)
   697  	s.SetDumpRouterMap(false)
   698  	s.Start()
   699  	defer s.Shutdown()
   700  
   701  	time.Sleep(100 * time.Millisecond)
   702  	gtest.C(t, func(t *gtest.T) {
   703  		prefix := fmt.Sprintf("http://127.0.0.1:%d", p)
   704  		client := g.Client()
   705  		client.SetPrefix(prefix)
   706  
   707  		t.Assert(client.GetContent("/parse?cate=1&page=2&size=10"), `{"Type":"","CategoryId":1,"Page":2,"Size":10,"Sort":0,"UserId":0}`)
   708  	})
   709  }