github.com/wangyougui/gf/v2@v2.6.5/net/gclient/gclient_z_unit_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/wangyougui/gf. 6 7 package gclient_test 8 9 import ( 10 "bytes" 11 "context" 12 "crypto/tls" 13 "fmt" 14 "io" 15 "net/http" 16 "testing" 17 "time" 18 19 "github.com/gorilla/websocket" 20 21 "github.com/wangyougui/gf/v2/debug/gdebug" 22 "github.com/wangyougui/gf/v2/errors/gerror" 23 "github.com/wangyougui/gf/v2/frame/g" 24 "github.com/wangyougui/gf/v2/net/gclient" 25 "github.com/wangyougui/gf/v2/net/ghttp" 26 "github.com/wangyougui/gf/v2/os/gfile" 27 "github.com/wangyougui/gf/v2/test/gtest" 28 "github.com/wangyougui/gf/v2/text/gstr" 29 "github.com/wangyougui/gf/v2/util/guid" 30 ) 31 32 var ( 33 ctx = context.TODO() 34 ) 35 36 func Test_Client_Basic(t *testing.T) { 37 s := g.Server(guid.S()) 38 s.BindHandler("/hello", func(r *ghttp.Request) { 39 r.Response.Write("hello") 40 }) 41 s.BindHandler("/postForm", func(r *ghttp.Request) { 42 r.Response.Write(r.Get("key1")) 43 }) 44 s.SetDumpRouterMap(false) 45 s.Start() 46 defer s.Shutdown() 47 48 time.Sleep(100 * time.Millisecond) 49 gtest.C(t, func(t *gtest.T) { 50 url := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 51 client := g.Client() 52 client.SetPrefix(url) 53 54 t.Assert(g.Client().GetContent(ctx, ""), ``) 55 t.Assert(client.GetContent(ctx, "/hello"), `hello`) 56 57 _, err := g.Client().Post(ctx, "") 58 t.AssertNE(err, nil) 59 60 _, err = g.Client().PostForm(ctx, "/postForm", nil) 61 t.AssertNE(err, nil) 62 data, _ := g.Client().PostForm(ctx, url+"/postForm", map[string]string{ 63 "key1": "value1", 64 }) 65 t.Assert(data.ReadAllString(), "value1") 66 }) 67 } 68 69 func Test_Client_BasicAuth(t *testing.T) { 70 s := g.Server(guid.S()) 71 s.BindHandler("/auth", func(r *ghttp.Request) { 72 if r.BasicAuth("admin", "admin") { 73 r.Response.Write("1") 74 } else { 75 r.Response.Write("0") 76 } 77 }) 78 s.SetDumpRouterMap(false) 79 s.Start() 80 defer s.Shutdown() 81 82 time.Sleep(100 * time.Millisecond) 83 gtest.C(t, func(t *gtest.T) { 84 c := g.Client() 85 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 86 t.Assert(c.BasicAuth("admin", "123").GetContent(ctx, "/auth"), "0") 87 t.Assert(c.BasicAuth("admin", "admin").GetContent(ctx, "/auth"), "1") 88 }) 89 } 90 91 func Test_Client_Cookie(t *testing.T) { 92 s := g.Server(guid.S()) 93 s.BindHandler("/cookie", func(r *ghttp.Request) { 94 r.Response.Write(r.Cookie.Get("test")) 95 }) 96 s.SetDumpRouterMap(false) 97 s.Start() 98 defer s.Shutdown() 99 100 time.Sleep(100 * time.Millisecond) 101 gtest.C(t, func(t *gtest.T) { 102 c := g.Client() 103 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 104 105 c.SetCookie("test", "0123456789") 106 t.Assert(c.PostContent(ctx, "/cookie"), "0123456789") 107 }) 108 } 109 110 func Test_Client_MapParam(t *testing.T) { 111 s := g.Server(guid.S()) 112 s.BindHandler("/map", func(r *ghttp.Request) { 113 r.Response.Write(r.Get("test")) 114 }) 115 s.SetDumpRouterMap(false) 116 s.Start() 117 defer s.Shutdown() 118 119 time.Sleep(100 * time.Millisecond) 120 gtest.C(t, func(t *gtest.T) { 121 c := g.Client() 122 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 123 124 t.Assert(c.GetContent(ctx, "/map", g.Map{"test": "1234567890"}), "1234567890") 125 }) 126 } 127 128 func Test_Client_Cookies(t *testing.T) { 129 s := g.Server(guid.S()) 130 s.BindHandler("/cookie", func(r *ghttp.Request) { 131 r.Cookie.Set("test1", "1") 132 r.Cookie.Set("test2", "2") 133 r.Response.Write("ok") 134 }) 135 s.SetDumpRouterMap(false) 136 s.Start() 137 defer s.Shutdown() 138 139 time.Sleep(100 * time.Millisecond) 140 gtest.C(t, func(t *gtest.T) { 141 c := g.Client() 142 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 143 144 resp, err := c.Get(ctx, "/cookie") 145 t.AssertNil(err) 146 defer resp.Close() 147 148 t.AssertNE(resp.Header.Get("Set-Cookie"), "") 149 150 m := resp.GetCookieMap() 151 t.Assert(len(m), 2) 152 t.Assert(m["test1"], 1) 153 t.Assert(m["test2"], 2) 154 t.Assert(resp.GetCookie("test1"), 1) 155 t.Assert(resp.GetCookie("test2"), 2) 156 }) 157 } 158 159 func Test_Client_Chain_Header(t *testing.T) { 160 s := g.Server(guid.S()) 161 s.BindHandler("/header1", func(r *ghttp.Request) { 162 r.Response.Write(r.Header.Get("test1")) 163 }) 164 s.BindHandler("/header2", func(r *ghttp.Request) { 165 r.Response.Write(r.Header.Get("test2")) 166 }) 167 s.SetDumpRouterMap(false) 168 s.Start() 169 defer s.Shutdown() 170 171 time.Sleep(100 * time.Millisecond) 172 gtest.C(t, func(t *gtest.T) { 173 c := g.Client() 174 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 175 176 t.Assert(c.Header(g.MapStrStr{"test1": "1234567890"}).GetContent(ctx, "/header1"), "1234567890") 177 t.Assert(c.HeaderRaw("test1: 1234567890\ntest2: abcdefg").GetContent(ctx, "/header1"), "1234567890") 178 t.Assert(c.HeaderRaw("test1: 1234567890\ntest2: abcdefg").GetContent(ctx, "/header2"), "abcdefg") 179 }) 180 } 181 182 func Test_Client_Chain_Context(t *testing.T) { 183 s := g.Server(guid.S()) 184 s.BindHandler("/context", func(r *ghttp.Request) { 185 time.Sleep(1 * time.Second) 186 r.Response.Write("ok") 187 }) 188 s.SetDumpRouterMap(false) 189 s.Start() 190 defer s.Shutdown() 191 192 time.Sleep(100 * time.Millisecond) 193 gtest.C(t, func(t *gtest.T) { 194 c := g.Client() 195 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 196 197 ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond) 198 t.Assert(c.GetContent(ctx, "/context"), "") 199 200 ctx, _ = context.WithTimeout(context.Background(), 2000*time.Millisecond) 201 t.Assert(c.GetContent(ctx, "/context"), "ok") 202 }) 203 } 204 205 func Test_Client_Chain_Timeout(t *testing.T) { 206 s := g.Server(guid.S()) 207 s.BindHandler("/timeout", func(r *ghttp.Request) { 208 time.Sleep(1 * time.Second) 209 r.Response.Write("ok") 210 }) 211 s.SetDumpRouterMap(false) 212 s.Start() 213 defer s.Shutdown() 214 215 time.Sleep(100 * time.Millisecond) 216 gtest.C(t, func(t *gtest.T) { 217 c := g.Client() 218 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 219 t.Assert(c.Timeout(100*time.Millisecond).GetContent(ctx, "/timeout"), "") 220 t.Assert(c.Timeout(2000*time.Millisecond).GetContent(ctx, "/timeout"), "ok") 221 }) 222 } 223 224 func Test_Client_Chain_ContentJson(t *testing.T) { 225 s := g.Server(guid.S()) 226 s.BindHandler("/json", func(r *ghttp.Request) { 227 r.Response.Write(r.Get("name"), r.Get("score")) 228 }) 229 s.SetDumpRouterMap(false) 230 s.Start() 231 defer s.Shutdown() 232 233 time.Sleep(100 * time.Millisecond) 234 gtest.C(t, func(t *gtest.T) { 235 c := g.Client() 236 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 237 t.Assert(c.ContentJson().PostContent(ctx, "/json", g.Map{ 238 "name": "john", 239 "score": 100, 240 }), "john100") 241 t.Assert(c.ContentJson().PostContent(ctx, "/json", `{"name":"john", "score":100}`), "john100") 242 243 type User struct { 244 Name string `json:"name"` 245 Score int `json:"score"` 246 } 247 t.Assert(c.ContentJson().PostContent(ctx, "/json", User{"john", 100}), "john100") 248 }) 249 } 250 251 func Test_Client_Chain_ContentXml(t *testing.T) { 252 s := g.Server(guid.S()) 253 s.BindHandler("/xml", func(r *ghttp.Request) { 254 r.Response.Write(r.Get("name"), r.Get("score")) 255 }) 256 s.SetDumpRouterMap(false) 257 s.Start() 258 defer s.Shutdown() 259 260 time.Sleep(100 * time.Millisecond) 261 gtest.C(t, func(t *gtest.T) { 262 c := g.Client() 263 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 264 t.Assert(c.ContentXml().PostContent(ctx, "/xml", g.Map{ 265 "name": "john", 266 "score": 100, 267 }), "john100") 268 t.Assert(c.ContentXml().PostContent(ctx, "/xml", `{"name":"john", "score":100}`), "john100") 269 270 type User struct { 271 Name string `json:"name"` 272 Score int `json:"score"` 273 } 274 t.Assert(c.ContentXml().PostContent(ctx, "/xml", User{"john", 100}), "john100") 275 }) 276 } 277 278 func Test_Client_Param_Containing_Special_Char(t *testing.T) { 279 s := g.Server(guid.S()) 280 s.BindHandler("/", func(r *ghttp.Request) { 281 r.Response.Write("k1=", r.Get("k1"), "&k2=", r.Get("k2")) 282 }) 283 s.SetDumpRouterMap(false) 284 s.Start() 285 defer s.Shutdown() 286 287 time.Sleep(100 * time.Millisecond) 288 gtest.C(t, func(t *gtest.T) { 289 c := g.Client() 290 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 291 t.Assert(c.PostContent(ctx, "/", "k1=MTIxMg==&k2=100"), "k1=MTIxMg==&k2=100") 292 t.Assert(c.PostContent(ctx, "/", g.Map{ 293 "k1": "MTIxMg==", 294 "k2": 100, 295 }), "k1=MTIxMg==&k2=100") 296 }) 297 } 298 299 // It posts data along with file uploading. 300 // It does not url-encodes the parameters. 301 func Test_Client_File_And_Param(t *testing.T) { 302 s := g.Server(guid.S()) 303 s.BindHandler("/", func(r *ghttp.Request) { 304 tmpPath := gfile.Temp(guid.S()) 305 err := gfile.Mkdir(tmpPath) 306 gtest.AssertNil(err) 307 defer gfile.Remove(tmpPath) 308 309 file := r.GetUploadFile("file") 310 _, err = file.Save(tmpPath) 311 gtest.AssertNil(err) 312 r.Response.Write( 313 r.Get("json"), 314 gfile.GetContents(gfile.Join(tmpPath, gfile.Basename(file.Filename))), 315 ) 316 }) 317 s.SetDumpRouterMap(false) 318 s.Start() 319 defer s.Shutdown() 320 321 time.Sleep(100 * time.Millisecond) 322 323 gtest.C(t, func(t *gtest.T) { 324 path := gtest.DataPath("upload", "file1.txt") 325 data := g.Map{ 326 "file": "@file:" + path, 327 "json": `{"uuid": "luijquiopm", "isRelative": false, "fileName": "test111.xls"}`, 328 } 329 c := g.Client() 330 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 331 t.Assert(c.PostContent(ctx, "/", data), data["json"].(string)+gfile.GetContents(path)) 332 }) 333 } 334 335 func Test_Client_Middleware(t *testing.T) { 336 s := g.Server(guid.S()) 337 isServerHandler := false 338 s.BindHandler("/", func(r *ghttp.Request) { 339 isServerHandler = true 340 }) 341 s.SetDumpRouterMap(false) 342 s.Start() 343 defer s.Shutdown() 344 345 time.Sleep(100 * time.Millisecond) 346 347 gtest.C(t, func(t *gtest.T) { 348 var ( 349 str1 = "" 350 str2 = "resp body" 351 ) 352 c := g.Client().SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 353 c.Use(func(c *gclient.Client, r *http.Request) (resp *gclient.Response, err error) { 354 str1 += "a" 355 resp, err = c.Next(r) 356 if err != nil { 357 return nil, err 358 } 359 str1 += "b" 360 return 361 }) 362 c.Use(func(c *gclient.Client, r *http.Request) (resp *gclient.Response, err error) { 363 str1 += "c" 364 resp, err = c.Next(r) 365 if err != nil { 366 return nil, err 367 } 368 str1 += "d" 369 return 370 }) 371 c.Use(func(c *gclient.Client, r *http.Request) (resp *gclient.Response, err error) { 372 str1 += "e" 373 resp, err = c.Next(r) 374 if err != nil { 375 return nil, err 376 } 377 resp.Response.Body = io.NopCloser(bytes.NewBufferString(str2)) 378 str1 += "f" 379 return 380 }) 381 resp, err := c.Get(ctx, "/") 382 t.Assert(str1, "acefdb") 383 t.AssertNil(err) 384 t.Assert(resp.ReadAllString(), str2) 385 t.Assert(isServerHandler, true) 386 387 // test abort, abort will not send 388 var ( 389 str3 = "" 390 abortStr = "abort request" 391 ) 392 393 c = g.Client().SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 394 c.Use(func(c *gclient.Client, r *http.Request) (resp *gclient.Response, err error) { 395 str3 += "a" 396 resp, err = c.Next(r) 397 str3 += "b" 398 return 399 }) 400 c.Use(func(c *gclient.Client, r *http.Request) (*gclient.Response, error) { 401 str3 += "c" 402 return nil, gerror.New(abortStr) 403 }) 404 c.Use(func(c *gclient.Client, r *http.Request) (resp *gclient.Response, err error) { 405 str3 += "f" 406 resp, err = c.Next(r) 407 str3 += "g" 408 return 409 }) 410 resp, err = c.Get(ctx, "/") 411 t.Assert(err, abortStr) 412 t.Assert(str3, "acb") 413 t.Assert(resp, nil) 414 }) 415 } 416 417 func Test_Client_Agent(t *testing.T) { 418 s := g.Server(guid.S()) 419 s.BindHandler("/", func(r *ghttp.Request) { 420 r.Response.Write(r.UserAgent()) 421 }) 422 s.SetDumpRouterMap(false) 423 s.Start() 424 defer s.Shutdown() 425 426 time.Sleep(100 * time.Millisecond) 427 428 gtest.C(t, func(t *gtest.T) { 429 c := g.Client().SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 430 c.SetAgent("test") 431 t.Assert(c.GetContent(ctx, "/"), "test") 432 }) 433 } 434 435 func Test_Client_Request_13_Dump(t *testing.T) { 436 s := g.Server(guid.S()) 437 s.BindHandler("/hello", func(r *ghttp.Request) { 438 r.Response.WriteHeader(200) 439 r.Response.WriteJson(g.Map{"field": "test_for_response_body"}) 440 }) 441 s.BindHandler("/hello2", func(r *ghttp.Request) { 442 r.Response.WriteHeader(200) 443 r.Response.Writeln(g.Map{"field": "test_for_response_body"}) 444 }) 445 s.SetDumpRouterMap(false) 446 s.Start() 447 defer s.Shutdown() 448 449 time.Sleep(100 * time.Millisecond) 450 gtest.C(t, func(t *gtest.T) { 451 url := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 452 client := g.Client().SetPrefix(url).ContentJson() 453 r, err := client.Post(ctx, "/hello", g.Map{"field": "test_for_request_body"}) 454 t.AssertNil(err) 455 dumpedText := r.RawRequest() 456 t.Assert(gstr.Contains(dumpedText, "test_for_request_body"), true) 457 dumpedText2 := r.RawResponse() 458 fmt.Println(dumpedText2) 459 t.Assert(gstr.Contains(dumpedText2, "test_for_response_body"), true) 460 461 client2 := g.Client().SetPrefix(url).ContentType("text/html") 462 r2, err := client2.Post(ctx, "/hello2", g.Map{"field": "test_for_request_body"}) 463 t.AssertNil(err) 464 dumpedText3 := r2.RawRequest() 465 t.Assert(gstr.Contains(dumpedText3, "test_for_request_body"), true) 466 dumpedText4 := r2.RawResponse() 467 t.Assert(gstr.Contains(dumpedText4, "test_for_request_body"), false) 468 r2 = nil 469 t.Assert(r2.RawRequest(), "") 470 }) 471 472 gtest.C(t, func(t *gtest.T) { 473 url := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 474 response, _ := g.Client().Get(ctx, url, g.Map{ 475 "id": 10000, 476 "name": "john", 477 }) 478 response = nil 479 t.Assert(response.RawRequest(), "") 480 }) 481 482 gtest.C(t, func(t *gtest.T) { 483 url := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()) 484 response, _ := g.Client().Get(ctx, url, g.Map{ 485 "id": 10000, 486 "name": "john", 487 }) 488 response.RawDump() 489 t.AssertGT(len(response.Raw()), 0) 490 }) 491 } 492 493 func Test_WebSocketClient(t *testing.T) { 494 s := g.Server(guid.S()) 495 s.BindHandler("/ws", func(r *ghttp.Request) { 496 ws, err := r.WebSocket() 497 if err != nil { 498 r.Exit() 499 } 500 for { 501 msgType, msg, err := ws.ReadMessage() 502 if err != nil { 503 return 504 } 505 if err = ws.WriteMessage(msgType, msg); err != nil { 506 return 507 } 508 } 509 }) 510 s.SetDumpRouterMap(false) 511 s.Start() 512 // No closing in case of DATA RACE due to keep alive connection of WebSocket. 513 // defer s.Shutdown() 514 515 time.Sleep(100 * time.Millisecond) 516 gtest.C(t, func(t *gtest.T) { 517 client := gclient.NewWebSocket() 518 client.Proxy = http.ProxyFromEnvironment 519 client.HandshakeTimeout = time.Minute 520 521 conn, _, err := client.Dial(fmt.Sprintf("ws://127.0.0.1:%d/ws", s.GetListenedPort()), nil) 522 t.AssertNil(err) 523 defer conn.Close() 524 525 msg := []byte("hello") 526 err = conn.WriteMessage(websocket.TextMessage, msg) 527 t.AssertNil(err) 528 529 mt, data, err := conn.ReadMessage() 530 t.AssertNil(err) 531 t.Assert(mt, websocket.TextMessage) 532 t.Assert(data, msg) 533 }) 534 } 535 536 func TestLoadKeyCrt(t *testing.T) { 537 var ( 538 testCrtFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/upload/file1.txt" 539 testKeyFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/upload/file2.txt" 540 crtFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/server.crt" 541 keyFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/server.key" 542 tlsConfig = &tls.Config{} 543 ) 544 545 gtest.C(t, func(t *gtest.T) { 546 tlsConfig, _ = gclient.LoadKeyCrt("crtFile", "keyFile") 547 t.AssertNil(tlsConfig) 548 549 tlsConfig, _ = gclient.LoadKeyCrt(crtFile, "keyFile") 550 t.AssertNil(tlsConfig) 551 552 tlsConfig, _ = gclient.LoadKeyCrt(testCrtFile, testKeyFile) 553 t.AssertNil(tlsConfig) 554 555 tlsConfig, _ = gclient.LoadKeyCrt(crtFile, keyFile) 556 t.AssertNE(tlsConfig, nil) 557 }) 558 } 559 560 func TestClient_DoRequest(t *testing.T) { 561 s := g.Server(guid.S()) 562 s.BindHandler("/hello", func(r *ghttp.Request) { 563 r.Response.WriteHeader(200) 564 r.Response.WriteJson(g.Map{"field": "test_for_response_body"}) 565 }) 566 s.SetDumpRouterMap(false) 567 s.Start() 568 defer s.Shutdown() 569 570 time.Sleep(100 * time.Millisecond) 571 gtest.C(t, func(t *gtest.T) { 572 c := g.Client() 573 url := fmt.Sprintf("127.0.0.1:%d/hello", s.GetListenedPort()) 574 resp, err := c.DoRequest(ctx, http.MethodGet, url) 575 t.AssertNil(err) 576 t.AssertNE(resp, nil) 577 t.Assert(resp.ReadAllString(), "{\"field\":\"test_for_response_body\"}") 578 579 resp.Response = nil 580 bytes := resp.ReadAll() 581 t.Assert(bytes, []byte{}) 582 resp.Close() 583 }) 584 585 gtest.C(t, func(t *gtest.T) { 586 c := g.Client() 587 url := "127.0.0.1:99999/hello" 588 resp, err := c.DoRequest(ctx, http.MethodGet, url) 589 t.AssertNil(resp.Response) 590 t.AssertNE(err, nil) 591 }) 592 } 593 594 func TestClient_RequestVar(t *testing.T) { 595 gtest.C(t, func(t *gtest.T) { 596 var ( 597 url = "http://127.0.0.1:99999/var/jsons" 598 ) 599 varValue := g.Client().RequestVar(ctx, http.MethodGet, url) 600 t.AssertNil(varValue) 601 }) 602 gtest.C(t, func(t *gtest.T) { 603 type User struct { 604 Id int 605 Name string 606 } 607 var ( 608 users []User 609 url = "http://127.0.0.1:8999/var/jsons" 610 ) 611 err := g.Client().RequestVar(ctx, http.MethodGet, url).Scan(&users) 612 t.AssertNil(err) 613 t.AssertNE(users, nil) 614 }) 615 } 616 617 func TestClient_SetBodyContent(t *testing.T) { 618 s := g.Server(guid.S()) 619 s.BindHandler("/", func(r *ghttp.Request) { 620 r.Response.Write("hello") 621 }) 622 s.SetDumpRouterMap(false) 623 s.Start() 624 defer s.Shutdown() 625 626 time.Sleep(100 * time.Millisecond) 627 gtest.C(t, func(t *gtest.T) { 628 c := g.Client() 629 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 630 res, err := c.Get(ctx, "/") 631 t.AssertNil(err) 632 defer res.Close() 633 t.Assert(res.ReadAllString(), "hello") 634 res.SetBodyContent([]byte("world")) 635 t.Assert(res.ReadAllString(), "world") 636 }) 637 } 638 639 func TestClient_SetNoUrlEncode(t *testing.T) { 640 s := g.Server(guid.S()) 641 s.BindHandler("/", func(r *ghttp.Request) { 642 r.Response.Write(r.URL.RawQuery) 643 }) 644 s.SetDumpRouterMap(false) 645 s.Start() 646 defer s.Shutdown() 647 648 time.Sleep(100 * time.Millisecond) 649 gtest.C(t, func(t *gtest.T) { 650 c := g.Client() 651 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 652 653 var params = g.Map{ 654 "path": "/data/binlog", 655 } 656 t.Assert(c.GetContent(ctx, `/`, params), `path=%2Fdata%2Fbinlog`) 657 658 c.SetNoUrlEncode(true) 659 t.Assert(c.GetContent(ctx, `/`, params), `path=/data/binlog`) 660 661 c.SetNoUrlEncode(false) 662 t.Assert(c.GetContent(ctx, `/`, params), `path=%2Fdata%2Fbinlog`) 663 }) 664 } 665 666 func TestClient_NoUrlEncode(t *testing.T) { 667 s := g.Server(guid.S()) 668 s.BindHandler("/", func(r *ghttp.Request) { 669 r.Response.Write(r.URL.RawQuery) 670 }) 671 s.SetDumpRouterMap(false) 672 s.Start() 673 defer s.Shutdown() 674 675 time.Sleep(100 * time.Millisecond) 676 gtest.C(t, func(t *gtest.T) { 677 c := g.Client() 678 c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 679 680 var params = g.Map{ 681 "path": "/data/binlog", 682 } 683 t.Assert(c.GetContent(ctx, `/`, params), `path=%2Fdata%2Fbinlog`) 684 685 t.Assert(c.NoUrlEncode().GetContent(ctx, `/`, params), `path=/data/binlog`) 686 }) 687 }