github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_z_unit_feature_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" 13 "testing" 14 "time" 15 16 "github.com/gogf/gf/v2/encoding/gjson" 17 "github.com/gogf/gf/v2/frame/g" 18 "github.com/gogf/gf/v2/net/ghttp" 19 "github.com/gogf/gf/v2/test/gtest" 20 "github.com/gogf/gf/v2/util/guid" 21 ) 22 23 func Test_Params_Basic(t *testing.T) { 24 type User struct { 25 Id int 26 Name string 27 Pass1 string `p:"password1"` 28 Pass2 string `p:"password2"` 29 } 30 s := g.Server(guid.S()) 31 // GET 32 s.BindHandler("/get", func(r *ghttp.Request) { 33 if r.GetQuery("array") != nil { 34 r.Response.Write(r.GetQuery("array")) 35 } 36 if r.GetQuery("slice") != nil { 37 r.Response.Write(r.GetQuery("slice")) 38 } 39 if r.GetQuery("bool") != nil { 40 r.Response.Write(r.GetQuery("bool").Bool()) 41 } 42 if r.GetQuery("float32") != nil { 43 r.Response.Write(r.GetQuery("float32").Float32()) 44 } 45 if r.GetQuery("float64") != nil { 46 r.Response.Write(r.GetQuery("float64").Float64()) 47 } 48 if r.GetQuery("int") != nil { 49 r.Response.Write(r.GetQuery("int").Int()) 50 } 51 if r.GetQuery("uint") != nil { 52 r.Response.Write(r.GetQuery("uint").Uint()) 53 } 54 if r.GetQuery("string") != nil { 55 r.Response.Write(r.GetQuery("string").String()) 56 } 57 if r.GetQuery("map") != nil { 58 r.Response.Write(r.GetQueryMap()["map"].(map[string]interface{})["b"]) 59 } 60 if r.GetQuery("a") != nil { 61 r.Response.Write(r.GetQueryMapStrStr()["a"]) 62 } 63 }) 64 // PUT 65 s.BindHandler("/put", func(r *ghttp.Request) { 66 if r.Get("array") != nil { 67 r.Response.Write(r.Get("array")) 68 } 69 if r.Get("slice") != nil { 70 r.Response.Write(r.Get("slice")) 71 } 72 if r.Get("bool") != nil { 73 r.Response.Write(r.Get("bool").Bool()) 74 } 75 if r.Get("float32") != nil { 76 r.Response.Write(r.Get("float32").Float32()) 77 } 78 if r.Get("float64") != nil { 79 r.Response.Write(r.Get("float64").Float64()) 80 } 81 if r.Get("int") != nil { 82 r.Response.Write(r.Get("int").Int()) 83 } 84 if r.Get("uint") != nil { 85 r.Response.Write(r.Get("uint").Uint()) 86 } 87 if r.Get("string") != nil { 88 r.Response.Write(r.Get("string").String()) 89 } 90 if r.Get("map") != nil { 91 r.Response.Write(r.GetMap()["map"].(map[string]interface{})["b"]) 92 } 93 if r.Get("a") != nil { 94 r.Response.Write(r.GetMapStrStr()["a"]) 95 } 96 }) 97 98 // DELETE 99 s.BindHandler("/delete", func(r *ghttp.Request) { 100 if r.Get("array") != nil { 101 r.Response.Write(r.Get("array")) 102 } 103 if r.Get("slice") != nil { 104 r.Response.Write(r.Get("slice")) 105 } 106 if r.Get("bool") != nil { 107 r.Response.Write(r.Get("bool").Bool()) 108 } 109 if r.Get("float32") != nil { 110 r.Response.Write(r.Get("float32").Float32()) 111 } 112 if r.Get("float64") != nil { 113 r.Response.Write(r.Get("float64").Float64()) 114 } 115 if r.Get("int") != nil { 116 r.Response.Write(r.Get("int").Int()) 117 } 118 if r.Get("uint") != nil { 119 r.Response.Write(r.Get("uint").Uint()) 120 } 121 if r.Get("string") != nil { 122 r.Response.Write(r.Get("string").String()) 123 } 124 if r.Get("map") != nil { 125 r.Response.Write(r.GetMap()["map"].(map[string]interface{})["b"]) 126 } 127 if r.Get("a") != nil { 128 r.Response.Write(r.GetMapStrStr()["a"]) 129 } 130 }) 131 // PATCH 132 s.BindHandler("/patch", 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.Get("bool").Bool()) 141 } 142 if r.Get("float32") != nil { 143 r.Response.Write(r.Get("float32").Float32()) 144 } 145 if r.Get("float64") != nil { 146 r.Response.Write(r.Get("float64").Float64()) 147 } 148 if r.Get("int") != nil { 149 r.Response.Write(r.Get("int").Int()) 150 } 151 if r.Get("uint") != nil { 152 r.Response.Write(r.Get("uint").Uint()) 153 } 154 if r.Get("string") != nil { 155 r.Response.Write(r.Get("string").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 // Form 165 s.BindHandler("/form", func(r *ghttp.Request) { 166 if r.Get("array") != nil { 167 r.Response.Write(r.GetForm("array")) 168 } 169 if r.Get("slice") != nil { 170 r.Response.Write(r.GetForm("slice")) 171 } 172 if r.Get("bool") != nil { 173 r.Response.Write(r.GetForm("bool").Bool()) 174 } 175 if r.Get("float32") != nil { 176 r.Response.Write(r.GetForm("float32").Float32()) 177 } 178 if r.Get("float64") != nil { 179 r.Response.Write(r.GetForm("float64").Float64()) 180 } 181 if r.Get("int") != nil { 182 r.Response.Write(r.GetForm("int").Int()) 183 } 184 if r.Get("uint") != nil { 185 r.Response.Write(r.GetForm("uint").Uint()) 186 } 187 if r.Get("string") != nil { 188 r.Response.Write(r.GetForm("string").String()) 189 } 190 if r.Get("map") != nil { 191 r.Response.Write(r.GetFormMap()["map"].(map[string]interface{})["b"]) 192 } 193 if r.Get("a") != nil { 194 r.Response.Write(r.GetFormMapStrStr()["a"]) 195 } 196 }) 197 s.BindHandler("/map", func(r *ghttp.Request) { 198 if m := r.GetQueryMap(); len(m) > 0 { 199 r.Response.Write(m["name"]) 200 return 201 } 202 if m := r.GetMap(); len(m) > 0 { 203 r.Response.Write(m["name"]) 204 return 205 } 206 }) 207 s.BindHandler("/raw", func(r *ghttp.Request) { 208 r.Response.Write(r.GetBody()) 209 }) 210 s.BindHandler("/json", func(r *ghttp.Request) { 211 j, err := r.GetJson() 212 if err != nil { 213 r.Response.Write(err) 214 return 215 } 216 r.Response.Write(j.Get("name")) 217 }) 218 s.BindHandler("/struct", func(r *ghttp.Request) { 219 if m := r.GetQueryMap(); len(m) > 0 { 220 user := new(User) 221 r.GetQueryStruct(user) 222 r.Response.Write(user.Id, user.Name, user.Pass1, user.Pass2) 223 return 224 } 225 if m := r.GetMap(); len(m) > 0 { 226 user := new(User) 227 r.GetStruct(user) 228 r.Response.Write(user.Id, user.Name, user.Pass1, user.Pass2) 229 return 230 } 231 }) 232 s.BindHandler("/struct-with-nil", func(r *ghttp.Request) { 233 user := (*User)(nil) 234 err := r.GetStruct(&user) 235 r.Response.Write(err) 236 }) 237 s.BindHandler("/struct-with-base", func(r *ghttp.Request) { 238 type Base struct { 239 Pass1 string `p:"password1"` 240 Pass2 string `p:"password2"` 241 } 242 type UserWithBase1 struct { 243 Id int 244 Name string 245 Base 246 } 247 type UserWithBase2 struct { 248 Id int 249 Name string 250 Pass Base 251 } 252 if m := r.GetMap(); len(m) > 0 { 253 user1 := new(UserWithBase1) 254 user2 := new(UserWithBase2) 255 r.GetStruct(user1) 256 r.GetStruct(user2) 257 r.Response.Write(user1.Id, user1.Name, user1.Pass1, user1.Pass2) 258 r.Response.Write(user2.Id, user2.Name, user2.Pass.Pass1, user2.Pass.Pass2) 259 } 260 }) 261 s.SetDumpRouterMap(false) 262 s.Start() 263 defer s.Shutdown() 264 265 time.Sleep(100 * time.Millisecond) 266 gtest.C(t, func(t *gtest.T) { 267 client := g.Client() 268 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 269 // GET 270 t.Assert(client.GetContent(ctx, "/get", "array[]=1&array[]=2"), `["1","2"]`) 271 t.Assert(client.GetContent(ctx, "/get", "slice=1&slice=2"), `2`) 272 t.Assert(client.GetContent(ctx, "/get", "bool=1"), `true`) 273 t.Assert(client.GetContent(ctx, "/get", "bool=0"), `false`) 274 t.Assert(client.GetContent(ctx, "/get", "float32=0.11"), `0.11`) 275 t.Assert(client.GetContent(ctx, "/get", "float64=0.22"), `0.22`) 276 t.Assert(client.GetContent(ctx, "/get", "int=-10000"), `-10000`) 277 t.Assert(client.GetContent(ctx, "/get", "int=10000"), `10000`) 278 t.Assert(client.GetContent(ctx, "/get", "uint=10000"), `10000`) 279 t.Assert(client.GetContent(ctx, "/get", "uint=9"), `9`) 280 t.Assert(client.GetContent(ctx, "/get", "string=key"), `key`) 281 t.Assert(client.GetContent(ctx, "/get", "map[a]=1&map[b]=2"), `2`) 282 t.Assert(client.GetContent(ctx, "/get", "a=1&b=2"), `1`) 283 284 // PUT 285 t.Assert(client.PutContent(ctx, "/put", "array[]=1&array[]=2"), `["1","2"]`) 286 t.Assert(client.PutContent(ctx, "/put", "slice=1&slice=2"), `2`) 287 t.Assert(client.PutContent(ctx, "/put", "bool=1"), `true`) 288 t.Assert(client.PutContent(ctx, "/put", "bool=0"), `false`) 289 t.Assert(client.PutContent(ctx, "/put", "float32=0.11"), `0.11`) 290 t.Assert(client.PutContent(ctx, "/put", "float64=0.22"), `0.22`) 291 t.Assert(client.PutContent(ctx, "/put", "int=-10000"), `-10000`) 292 t.Assert(client.PutContent(ctx, "/put", "int=10000"), `10000`) 293 t.Assert(client.PutContent(ctx, "/put", "uint=10000"), `10000`) 294 t.Assert(client.PutContent(ctx, "/put", "uint=9"), `9`) 295 t.Assert(client.PutContent(ctx, "/put", "string=key"), `key`) 296 t.Assert(client.PutContent(ctx, "/put", "map[a]=1&map[b]=2"), `2`) 297 t.Assert(client.PutContent(ctx, "/put", "a=1&b=2"), `1`) 298 299 // DELETE 300 t.Assert(client.DeleteContent(ctx, "/delete", "array[]=1&array[]=2"), `["1","2"]`) 301 t.Assert(client.DeleteContent(ctx, "/delete", "slice=1&slice=2"), `2`) 302 t.Assert(client.DeleteContent(ctx, "/delete", "bool=1"), `true`) 303 t.Assert(client.DeleteContent(ctx, "/delete", "bool=0"), `false`) 304 t.Assert(client.DeleteContent(ctx, "/delete", "float32=0.11"), `0.11`) 305 t.Assert(client.DeleteContent(ctx, "/delete", "float64=0.22"), `0.22`) 306 t.Assert(client.DeleteContent(ctx, "/delete", "int=-10000"), `-10000`) 307 t.Assert(client.DeleteContent(ctx, "/delete", "int=10000"), `10000`) 308 t.Assert(client.DeleteContent(ctx, "/delete", "uint=10000"), `10000`) 309 t.Assert(client.DeleteContent(ctx, "/delete", "uint=9"), `9`) 310 t.Assert(client.DeleteContent(ctx, "/delete", "string=key"), `key`) 311 t.Assert(client.DeleteContent(ctx, "/delete", "map[a]=1&map[b]=2"), `2`) 312 t.Assert(client.DeleteContent(ctx, "/delete", "a=1&b=2"), `1`) 313 314 // PATCH 315 t.Assert(client.PatchContent(ctx, "/patch", "array[]=1&array[]=2"), `["1","2"]`) 316 t.Assert(client.PatchContent(ctx, "/patch", "slice=1&slice=2"), `2`) 317 t.Assert(client.PatchContent(ctx, "/patch", "bool=1"), `true`) 318 t.Assert(client.PatchContent(ctx, "/patch", "bool=0"), `false`) 319 t.Assert(client.PatchContent(ctx, "/patch", "float32=0.11"), `0.11`) 320 t.Assert(client.PatchContent(ctx, "/patch", "float64=0.22"), `0.22`) 321 t.Assert(client.PatchContent(ctx, "/patch", "int=-10000"), `-10000`) 322 t.Assert(client.PatchContent(ctx, "/patch", "int=10000"), `10000`) 323 t.Assert(client.PatchContent(ctx, "/patch", "uint=10000"), `10000`) 324 t.Assert(client.PatchContent(ctx, "/patch", "uint=9"), `9`) 325 t.Assert(client.PatchContent(ctx, "/patch", "string=key"), `key`) 326 t.Assert(client.PatchContent(ctx, "/patch", "map[a]=1&map[b]=2"), `2`) 327 t.Assert(client.PatchContent(ctx, "/patch", "a=1&b=2"), `1`) 328 329 // Form 330 t.Assert(client.PostContent(ctx, "/form", "array[]=1&array[]=2"), `["1","2"]`) 331 t.Assert(client.PostContent(ctx, "/form", "slice=1&slice=2"), `2`) 332 t.Assert(client.PostContent(ctx, "/form", "bool=1"), `true`) 333 t.Assert(client.PostContent(ctx, "/form", "bool=0"), `false`) 334 t.Assert(client.PostContent(ctx, "/form", "float32=0.11"), `0.11`) 335 t.Assert(client.PostContent(ctx, "/form", "float64=0.22"), `0.22`) 336 t.Assert(client.PostContent(ctx, "/form", "int=-10000"), `-10000`) 337 t.Assert(client.PostContent(ctx, "/form", "int=10000"), `10000`) 338 t.Assert(client.PostContent(ctx, "/form", "uint=10000"), `10000`) 339 t.Assert(client.PostContent(ctx, "/form", "uint=9"), `9`) 340 t.Assert(client.PostContent(ctx, "/form", "string=key"), `key`) 341 t.Assert(client.PostContent(ctx, "/form", "map[a]=1&map[b]=2"), `2`) 342 t.Assert(client.PostContent(ctx, "/form", "a=1&b=2"), `1`) 343 344 // Map 345 t.Assert(client.GetContent(ctx, "/map", "id=1&name=john"), `john`) 346 t.Assert(client.PostContent(ctx, "/map", "id=1&name=john"), `john`) 347 348 // Raw 349 t.Assert(client.PutContent(ctx, "/raw", "id=1&name=john"), `id=1&name=john`) 350 351 // Json 352 t.Assert(client.PostContent(ctx, "/json", `{"id":1, "name":"john"}`), `john`) 353 354 // Struct 355 t.Assert(client.GetContent(ctx, "/struct", `id=1&name=john&password1=123&password2=456`), `1john123456`) 356 t.Assert(client.PostContent(ctx, "/struct", `id=1&name=john&password1=123&password2=456`), `1john123456`) 357 t.Assert(client.PostContent(ctx, "/struct-with-nil", ``), ``) 358 t.Assert(client.PostContent(ctx, "/struct-with-base", `id=1&name=john&password1=123&password2=456`), "1john1234561john") 359 }) 360 } 361 362 func Test_Params_Header(t *testing.T) { 363 s := g.Server(guid.S()) 364 s.BindHandler("/header", func(r *ghttp.Request) { 365 r.Response.Write(r.GetHeader("test")) 366 }) 367 s.SetDumpRouterMap(false) 368 s.Start() 369 defer s.Shutdown() 370 371 time.Sleep(100 * time.Millisecond) 372 gtest.C(t, func(t *gtest.T) { 373 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 374 client := g.Client() 375 client.SetPrefix(prefix) 376 377 t.Assert(client.Header(g.MapStrStr{"test": "123456"}).GetContent(ctx, "/header"), "123456") 378 }) 379 } 380 381 func Test_Params_SupportChars(t *testing.T) { 382 s := g.Server(guid.S()) 383 s.BindHandler("/form-value", func(r *ghttp.Request) { 384 r.Response.Write(r.GetForm("test-value")) 385 }) 386 s.BindHandler("/form-array", func(r *ghttp.Request) { 387 r.Response.Write(r.GetForm("test-array")) 388 }) 389 s.SetDumpRouterMap(false) 390 s.Start() 391 defer s.Shutdown() 392 393 time.Sleep(100 * time.Millisecond) 394 gtest.C(t, func(t *gtest.T) { 395 c := g.Client() 396 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 397 t.Assert(c.PostContent(ctx, "/form-value", "test-value=100"), "100") 398 t.Assert(c.PostContent(ctx, "/form-array", "test-array[]=1&test-array[]=2"), `["1","2"]`) 399 }) 400 } 401 402 func Test_Params_Priority(t *testing.T) { 403 s := g.Server(guid.S()) 404 s.BindHandler("/query", func(r *ghttp.Request) { 405 r.Response.Write(r.GetQuery("a")) 406 }) 407 s.BindHandler("/form", func(r *ghttp.Request) { 408 r.Response.Write(r.GetForm("a")) 409 }) 410 s.BindHandler("/request", func(r *ghttp.Request) { 411 r.Response.Write(r.Get("a")) 412 }) 413 s.BindHandler("/request-map", func(r *ghttp.Request) { 414 r.Response.Write(r.GetMap(g.Map{ 415 "a": 1, 416 "b": 2, 417 })) 418 }) 419 s.SetDumpRouterMap(false) 420 s.Start() 421 defer s.Shutdown() 422 423 time.Sleep(100 * time.Millisecond) 424 gtest.C(t, func(t *gtest.T) { 425 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 426 client := g.Client() 427 client.SetPrefix(prefix) 428 429 t.Assert(client.GetContent(ctx, "/query?a=1", "a=100"), "100") 430 t.Assert(client.PostContent(ctx, "/form?a=1", "a=100"), "100") 431 t.Assert(client.PutContent(ctx, "/form?a=1", "a=100"), "100") 432 t.Assert(client.GetContent(ctx, "/request?a=1", "a=100"), "100") 433 t.Assert(client.GetContent(ctx, "/request-map?a=1&b=2&c=3", "a=100&b=200&c=300"), `{"a":"100","b":"200"}`) 434 }) 435 } 436 437 func Test_Params_GetRequestMap(t *testing.T) { 438 s := g.Server(guid.S()) 439 s.BindHandler("/map", func(r *ghttp.Request) { 440 r.Response.Write(r.GetRequestMap()) 441 }) 442 s.BindHandler("/withKVMap", func(r *ghttp.Request) { 443 m := r.GetRequestMap(map[string]interface{}{"id": 2}) 444 r.Response.Write(m["id"]) 445 }) 446 s.BindHandler("/paramsMapWithKVMap", func(r *ghttp.Request) { 447 r.SetParam("name", "john") 448 m := r.GetRequestMap(map[string]interface{}{"id": 2}) 449 r.Response.Write(m["id"]) 450 }) 451 s.BindHandler("/{name}.map", func(r *ghttp.Request) { 452 m := r.GetRequestMap(map[string]interface{}{"id": 2}) 453 r.Response.Write(m["id"]) 454 }) 455 s.SetDumpRouterMap(false) 456 s.Start() 457 defer s.Shutdown() 458 459 time.Sleep(100 * time.Millisecond) 460 gtest.C(t, func(t *gtest.T) { 461 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 462 client := g.Client() 463 client.SetPrefix(prefix) 464 465 t.Assert( 466 client.PostContent(ctx, 467 "/map", 468 "time_end2020-04-18 16:11:58&returnmsg=Success&attach=", 469 ), 470 `{"attach":"","returnmsg":"Success"}`, 471 ) 472 473 t.Assert(client.PostContent(ctx, "/john.map", "name=john"), 2) 474 475 t.Assert(client.PostContent(ctx, "/withKVMap", "name=john"), 2) 476 477 t.Assert(client.PostContent(ctx, "/paramsMapWithKVMap"), 2) 478 479 client.SetContentType("application/json") 480 t.Assert(client.GetContent(ctx, "/withKVMap", "name=john"), 2) 481 }) 482 } 483 484 func Test_Params_Modify(t *testing.T) { 485 s := g.Server(guid.S()) 486 s.BindHandler("/param/modify", func(r *ghttp.Request) { 487 param := r.GetMap() 488 param["id"] = 2 489 paramBytes, err := gjson.Encode(param) 490 if err != nil { 491 r.Response.Write(err) 492 return 493 } 494 r.Request.Body = io.NopCloser(bytes.NewReader(paramBytes)) 495 r.ReloadParam() 496 r.Response.Write(r.GetMap()) 497 }) 498 s.SetDumpRouterMap(false) 499 s.Start() 500 defer s.Shutdown() 501 502 time.Sleep(100 * time.Millisecond) 503 gtest.C(t, func(t *gtest.T) { 504 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 505 client := g.Client() 506 client.SetPrefix(prefix) 507 508 t.Assert( 509 client.PostContent(ctx, 510 "/param/modify", 511 `{"id":1}`, 512 ), 513 `{"id":2}`, 514 ) 515 }) 516 } 517 518 func Test_Params_Parse_DefaultValueTag(t *testing.T) { 519 type T struct { 520 Name string `d:"john"` 521 Score float32 `d:"60"` 522 } 523 s := g.Server(guid.S()) 524 s.BindHandler("/parse", func(r *ghttp.Request) { 525 var t *T 526 if err := r.Parse(&t); err != nil { 527 r.Response.WriteExit(err) 528 } 529 r.Response.WriteExit(t) 530 }) 531 s.SetDumpRouterMap(false) 532 s.Start() 533 defer s.Shutdown() 534 535 time.Sleep(100 * time.Millisecond) 536 gtest.C(t, func(t *gtest.T) { 537 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 538 client := g.Client() 539 client.SetPrefix(prefix) 540 541 t.Assert(client.PostContent(ctx, "/parse"), `{"Name":"john","Score":60}`) 542 t.Assert(client.PostContent(ctx, "/parse", `{"name":"smith"}`), `{"Name":"smith","Score":60}`) 543 t.Assert(client.PostContent(ctx, "/parse", `{"name":"smith", "score":100}`), `{"Name":"smith","Score":100}`) 544 }) 545 } 546 547 func Test_Params_Parse_Validation(t *testing.T) { 548 type RegisterReq struct { 549 Name string `p:"username" v:"required|length:6,30#请输入账号|账号长度为{min}到{max}位"` 550 Pass string `p:"password1" v:"required|length:6,30#请输入密码|密码长度不够"` 551 Pass2 string `p:"password2" v:"required|length:6,30|same:password1#请确认密码|密码长度不够|两次密码不一致"` 552 } 553 554 s := g.Server(guid.S()) 555 s.BindHandler("/parse", func(r *ghttp.Request) { 556 var req *RegisterReq 557 if err := r.Parse(&req); err != nil { 558 r.Response.Write(err) 559 } else { 560 r.Response.Write("ok") 561 } 562 }) 563 s.SetDumpRouterMap(false) 564 s.Start() 565 defer s.Shutdown() 566 567 time.Sleep(100 * time.Millisecond) 568 gtest.C(t, func(t *gtest.T) { 569 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 570 client := g.Client() 571 client.SetPrefix(prefix) 572 573 t.Assert(client.GetContent(ctx, "/parse"), `请输入账号`) 574 t.Assert(client.GetContent(ctx, "/parse?name=john11&password1=123456&password2=123"), `密码长度不够`) 575 t.Assert(client.GetContent(ctx, "/parse?name=john&password1=123456&password2=123456"), `账号长度为6到30位`) 576 t.Assert(client.GetContent(ctx, "/parse?name=john11&password1=123456&password2=123456"), `ok`) 577 }) 578 } 579 580 func Test_Params_Parse_EmbeddedWithAliasName1(t *testing.T) { 581 // 获取内容列表 582 type ContentGetListInput struct { 583 Type string 584 CategoryId uint 585 Page int 586 Size int 587 Sort int 588 UserId uint 589 } 590 // 获取内容列表 591 type ContentGetListReq struct { 592 ContentGetListInput 593 CategoryId uint `p:"cate"` 594 Page int `d:"1" v:"min:0#分页号码错误"` 595 Size int `d:"10" v:"max:50#分页数量最大50条"` 596 } 597 598 s := g.Server(guid.S()) 599 s.BindHandler("/parse", func(r *ghttp.Request) { 600 var req *ContentGetListReq 601 if err := r.Parse(&req); err != nil { 602 r.Response.Write(err) 603 } else { 604 r.Response.Write(req.ContentGetListInput) 605 } 606 }) 607 s.SetDumpRouterMap(false) 608 s.Start() 609 defer s.Shutdown() 610 611 time.Sleep(100 * time.Millisecond) 612 gtest.C(t, func(t *gtest.T) { 613 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 614 client := g.Client() 615 client.SetPrefix(prefix) 616 617 t.Assert(client.GetContent(ctx, "/parse?cate=1&page=2&size=10"), `{"Type":"","CategoryId":0,"Page":2,"Size":10,"Sort":0,"UserId":0}`) 618 }) 619 } 620 621 func Test_Params_Parse_EmbeddedWithAliasName2(t *testing.T) { 622 // 获取内容列表 623 type ContentGetListInput struct { 624 Type string 625 CategoryId uint `p:"cate"` 626 Page int 627 Size int 628 Sort int 629 UserId uint 630 } 631 // 获取内容列表 632 type ContentGetListReq struct { 633 ContentGetListInput 634 CategoryId uint `p:"cate"` 635 Page int `d:"1" v:"min:0#分页号码错误"` 636 Size int `d:"10" v:"max:50#分页数量最大50条"` 637 } 638 639 s := g.Server(guid.S()) 640 s.BindHandler("/parse", func(r *ghttp.Request) { 641 var req *ContentGetListReq 642 if err := r.Parse(&req); err != nil { 643 r.Response.Write(err) 644 } else { 645 r.Response.Write(req.ContentGetListInput) 646 } 647 }) 648 s.SetDumpRouterMap(false) 649 s.Start() 650 defer s.Shutdown() 651 652 time.Sleep(100 * time.Millisecond) 653 gtest.C(t, func(t *gtest.T) { 654 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 655 client := g.Client() 656 client.SetPrefix(prefix) 657 658 t.Assert(client.GetContent(ctx, "/parse?cate=1&page=2&size=10"), `{"Type":"","CategoryId":1,"Page":2,"Size":10,"Sort":0,"UserId":0}`) 659 }) 660 } 661 662 func Test_Params_GetParam(t *testing.T) { 663 s := g.Server(guid.S()) 664 s.BindHandler("/", func(r *ghttp.Request) { 665 r.Response.Write(r.GetParam("key", "val")) 666 }) 667 s.SetDumpRouterMap(false) 668 s.Start() 669 defer s.Shutdown() 670 671 time.Sleep(100 * time.Millisecond) 672 gtest.C(t, func(t *gtest.T) { 673 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 674 client := g.Client() 675 client.SetPrefix(prefix) 676 677 t.Assert(client.PostContent(ctx, "/"), "val") 678 }) 679 } 680 681 func Test_Params_SetQuery(t *testing.T) { 682 s := g.Server(guid.S()) 683 s.BindHandler("/SetQuery", func(r *ghttp.Request) { 684 r.SetQuery("a", 100) 685 r.Response.Write(r.GetQuery("a")) 686 }) 687 s.SetDumpRouterMap(false) 688 s.Start() 689 defer s.Shutdown() 690 691 time.Sleep(100 * time.Millisecond) 692 gtest.C(t, func(t *gtest.T) { 693 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 694 client := g.Client() 695 client.SetPrefix(prefix) 696 697 t.Assert(client.GetContent(ctx, "/SetQuery"), "100") 698 t.Assert(client.GetContent(ctx, "/SetQuery?a=1"), "100") 699 }) 700 } 701 702 func Test_Params_GetQuery(t *testing.T) { 703 s := g.Server(guid.S()) 704 s.BindHandler("/GetQuery", func(r *ghttp.Request) { 705 r.Response.Write(r.GetQuery("a", 200)) 706 }) 707 s.SetDumpRouterMap(false) 708 s.Start() 709 defer s.Shutdown() 710 711 time.Sleep(100 * time.Millisecond) 712 gtest.C(t, func(t *gtest.T) { 713 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 714 client := g.Client() 715 client.SetPrefix(prefix) 716 717 t.Assert(client.GetContent(ctx, "/GetQuery"), 200) 718 t.Assert(client.SetContentType("application/json").GetContent(ctx, "/GetQuery", "a=100"), 100) 719 }) 720 } 721 722 func Test_Params_GetQueryMap(t *testing.T) { 723 s := g.Server(guid.S()) 724 s.BindHandler("/GetQueryMap", func(r *ghttp.Request) { 725 if m := r.GetQueryMap(); len(m) > 0 { 726 r.Response.Write(m["name"]) 727 } 728 }) 729 s.BindHandler("/GetQueryMapWithKVMap", func(r *ghttp.Request) { 730 if m := r.GetQueryMap(map[string]interface{}{"id": 1}); len(m) > 0 { 731 r.Response.Write(m["id"]) 732 } 733 }) 734 s.SetDumpRouterMap(false) 735 s.Start() 736 defer s.Shutdown() 737 738 time.Sleep(100 * time.Millisecond) 739 740 gtest.C(t, func(t *gtest.T) { 741 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 742 client := g.Client() 743 client.SetPrefix(prefix) 744 client.SetContentType("application/json") 745 t.Assert(client.GetContent(ctx, "/GetQueryMap", "id=1&name=john"), `john`) 746 }) 747 gtest.C(t, func(t *gtest.T) { 748 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 749 client := g.Client() 750 client.SetPrefix(prefix) 751 t.Assert(client.GetContent(ctx, "/GetQueryMapWithKVMap"), 1) 752 t.Assert(client.GetContent(ctx, "/GetQueryMapWithKVMap", "name=john"), 1) 753 t.Assert(client.GetContent(ctx, "/GetQueryMapWithKVMap", "id=2&name=john"), 2) 754 client.SetContentType("application/json") 755 t.Assert(client.GetContent(ctx, "/GetQueryMapWithKVMap", "name=john"), 1) 756 t.Assert(client.GetContent(ctx, "/GetQueryMapWithKVMap", "id=2&name=john"), 2) 757 }) 758 } 759 760 func Test_Params_GetQueryMapStrStr(t *testing.T) { 761 s := g.Server(guid.S()) 762 s.BindHandler("/GetQueryMapStrStr", func(r *ghttp.Request) { 763 r.Response.Write(r.GetQueryMapStrStr()) 764 }) 765 s.SetDumpRouterMap(false) 766 s.Start() 767 defer s.Shutdown() 768 769 time.Sleep(100 * time.Millisecond) 770 gtest.C(t, func(t *gtest.T) { 771 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 772 client := g.Client() 773 client.SetPrefix(prefix) 774 775 t.Assert(client.GetContent(ctx, "/GetQueryMapStrStr"), "") 776 }) 777 } 778 779 func Test_Params_GetQueryMapStrVar(t *testing.T) { 780 s := g.Server(guid.S()) 781 s.BindHandler("/GetQueryMapStrVar", func(r *ghttp.Request) { 782 m := r.GetQueryMapStrVar() 783 r.Response.Write(m["id"]) 784 }) 785 s.SetDumpRouterMap(false) 786 s.Start() 787 defer s.Shutdown() 788 789 time.Sleep(100 * time.Millisecond) 790 gtest.C(t, func(t *gtest.T) { 791 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 792 client := g.Client() 793 client.SetPrefix(prefix) 794 795 t.Assert(client.GetContent(ctx, "/GetQueryMapStrVar"), "") 796 t.Assert(client.GetContent(ctx, "/GetQueryMapStrVar", "id=1"), 1) 797 }) 798 } 799 800 func Test_Params_GetRequest(t *testing.T) { 801 s := g.Server(guid.S()) 802 s.BindHandler("/GetRequest", func(r *ghttp.Request) { 803 r.Response.Write(r.GetRequest("id")) 804 }) 805 s.BindHandler("/GetRequestWithDef", func(r *ghttp.Request) { 806 r.Response.Write(r.GetRequest("id", 2)) 807 }) 808 s.SetDumpRouterMap(false) 809 s.Start() 810 defer s.Shutdown() 811 812 time.Sleep(100 * time.Millisecond) 813 gtest.C(t, func(t *gtest.T) { 814 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 815 client := g.Client() 816 client.SetPrefix(prefix) 817 818 t.Assert(client.GetContent(ctx, "/GetRequestWithDef"), 2) 819 820 client.SetContentType("application/json") 821 t.Assert(client.GetContent(ctx, "/GetRequest", "id=1"), 1) 822 }) 823 } 824 825 func Test_Params_GetRequestMapStrStr(t *testing.T) { 826 s := g.Server(guid.S()) 827 s.BindHandler("/GetRequestMapStrStr", func(r *ghttp.Request) { 828 r.Response.Write(r.GetRequestMapStrStr()) 829 }) 830 s.SetDumpRouterMap(false) 831 s.Start() 832 defer s.Shutdown() 833 834 time.Sleep(100 * time.Millisecond) 835 gtest.C(t, func(t *gtest.T) { 836 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 837 client := g.Client() 838 client.SetPrefix(prefix) 839 840 t.Assert(client.GetContent(ctx, "/GetRequestMapStrStr"), "") 841 }) 842 } 843 844 func Test_Params_GetRequestMapStrVar(t *testing.T) { 845 s := g.Server(guid.S()) 846 s.BindHandler("/GetRequestMapStrVar", func(r *ghttp.Request) { 847 m := r.GetRequestMapStrVar() 848 r.Response.Write(m["id"]) 849 }) 850 s.SetDumpRouterMap(false) 851 s.Start() 852 defer s.Shutdown() 853 854 time.Sleep(100 * time.Millisecond) 855 gtest.C(t, func(t *gtest.T) { 856 prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 857 client := g.Client() 858 client.SetPrefix(prefix) 859 860 t.Assert(client.GetContent(ctx, "/GetRequestMapStrVar"), "") 861 t.Assert(client.GetContent(ctx, "/GetRequestMapStrVar", "id=1"), 1) 862 }) 863 }