@@ -445,6 +445,129 @@ func TestNewHttpClient(t *testing.T) {
445
445
}
446
446
}
447
447
448
+ func TestHttpPostData (t * testing.T ) {
449
+ initDefaultClient ()
450
+ mux := http .NewServeMux ()
451
+ mux .HandleFunc ("/post_data" , func (w http.ResponseWriter , r * http.Request ) {
452
+ data , _ := io .ReadAll (r .Body )
453
+ fmt .Fprintf (w , string (data ))
454
+ })
455
+ server := httptest .NewServer (mux )
456
+ defer server .Close ()
457
+
458
+ testCases := []struct {
459
+ name string
460
+ path string
461
+ expectBody string
462
+ }{
463
+ {"return data hello" , "/post_data" , "hello" },
464
+ {"return data world" , "/post_data" , "world" },
465
+ }
466
+
467
+ for _ , tc := range testCases {
468
+ t .Run (tc .name , func (t * testing.T ) {
469
+ reqUrl := server .URL + tc .path
470
+ resp , err := testHttpClient .HttpPostData (reqUrl , []byte (tc .expectBody ))
471
+ if err != nil {
472
+ t .Errorf ("HttpPostData: request error, url=%s err=%v" , reqUrl , err )
473
+ return
474
+ }
475
+ defer resp .Body .Close ()
476
+ data , err := io .ReadAll (resp .Body )
477
+ if err != nil {
478
+ t .Errorf ("HttpPostData: read response body error, url=%s err=%v" , reqUrl , err )
479
+ return
480
+ }
481
+ actual := string (data )
482
+ if tc .expectBody != actual {
483
+ t .Errorf ("HttpPostData: expect body => %s, but actual body => %s url=%s" , tc .expectBody , actual , reqUrl )
484
+ }
485
+ })
486
+ }
487
+ }
488
+
489
+ func TestHttpPut (t * testing.T ) {
490
+ initDefaultClient ()
491
+ mux := http .NewServeMux ()
492
+ mux .HandleFunc ("/put_data" , func (w http.ResponseWriter , r * http.Request ) {
493
+ data , _ := io .ReadAll (r .Body )
494
+ fmt .Fprintf (w , string (data ))
495
+ })
496
+ server := httptest .NewServer (mux )
497
+ defer server .Close ()
498
+
499
+ testCases := []struct {
500
+ name string
501
+ path string
502
+ expectBody string
503
+ }{
504
+ {"return data hello" , "/put_data" , "hello" },
505
+ {"return data world" , "/put_data" , "world" },
506
+ }
507
+
508
+ for _ , tc := range testCases {
509
+ t .Run (tc .name , func (t * testing.T ) {
510
+ reqUrl := server .URL + tc .path
511
+ resp , err := testHttpClient .HttpPut (reqUrl , []byte (tc .expectBody ))
512
+ if err != nil {
513
+ t .Errorf ("HttpPut: request error, url=%s err=%v" , reqUrl , err )
514
+ return
515
+ }
516
+ defer resp .Body .Close ()
517
+ data , err := io .ReadAll (resp .Body )
518
+ if err != nil {
519
+ t .Errorf ("HttpPut: read response body error, url=%s err=%v" , reqUrl , err )
520
+ return
521
+ }
522
+ actual := string (data )
523
+ if tc .expectBody != actual {
524
+ t .Errorf ("HttpPut: expect body => %s, but actual body => %s url=%s" , tc .expectBody , actual , reqUrl )
525
+ }
526
+ })
527
+ }
528
+ }
529
+
530
+ func TestHttpDelete (t * testing.T ) {
531
+ initDefaultClient ()
532
+ mux := http .NewServeMux ()
533
+ mux .HandleFunc ("/delete_data" , func (w http.ResponseWriter , r * http.Request ) {
534
+ data , _ := io .ReadAll (r .Body )
535
+ fmt .Fprintf (w , string (data ))
536
+ })
537
+ server := httptest .NewServer (mux )
538
+ defer server .Close ()
539
+
540
+ testCases := []struct {
541
+ name string
542
+ path string
543
+ expectBody string
544
+ }{
545
+ {"return data hello" , "/delete_data" , "hello" },
546
+ {"return data world" , "/delete_data" , "world" },
547
+ }
548
+
549
+ for _ , tc := range testCases {
550
+ t .Run (tc .name , func (t * testing.T ) {
551
+ reqUrl := server .URL + tc .path
552
+ resp , err := testHttpClient .HttpPut (reqUrl , []byte (tc .expectBody ))
553
+ if err != nil {
554
+ t .Errorf ("HttpDelete: request error, url=%s err=%v" , reqUrl , err )
555
+ return
556
+ }
557
+ defer resp .Body .Close ()
558
+ data , err := io .ReadAll (resp .Body )
559
+ if err != nil {
560
+ t .Errorf ("HttpDelete: read response body error, url=%s err=%v" , reqUrl , err )
561
+ return
562
+ }
563
+ actual := string (data )
564
+ if tc .expectBody != actual {
565
+ t .Errorf ("HttpDelete: expect body => %s, but actual body => %s url=%s" , tc .expectBody , actual , reqUrl )
566
+ }
567
+ })
568
+ }
569
+ }
570
+
448
571
func initDefaultClient () {
449
572
testHttpClient , _ = NewHttpClient (true , "" , false )
450
573
}
0 commit comments