github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_unit_client_dump_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  	"fmt"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/gogf/gf/frame/g"
    15  	"github.com/gogf/gf/net/ghttp"
    16  	"github.com/gogf/gf/test/gtest"
    17  	"github.com/gogf/gf/text/gstr"
    18  )
    19  
    20  func Test_Client_Request_13_Dump(t *testing.T) {
    21  	p, _ := ports.PopRand()
    22  	s := g.Server(p)
    23  	s.BindHandler("/hello", func(r *ghttp.Request) {
    24  		r.Response.WriteHeader(200)
    25  		r.Response.WriteJson(g.Map{"field": "test_for_response_body"})
    26  	})
    27  	s.BindHandler("/hello2", func(r *ghttp.Request) {
    28  		r.Response.WriteHeader(200)
    29  		r.Response.Writeln(g.Map{"field": "test_for_response_body"})
    30  	})
    31  	s.SetPort(p)
    32  	s.SetDumpRouterMap(false)
    33  	s.Start()
    34  	defer s.Shutdown()
    35  
    36  	time.Sleep(100 * time.Millisecond)
    37  	gtest.C(t, func(t *gtest.T) {
    38  		url := fmt.Sprintf("http://127.0.0.1:%d", p)
    39  		client := g.Client().SetPrefix(url).ContentJson().SetDump(true)
    40  		r, err := client.Post("/hello", g.Map{"field": "test_for_request_body"})
    41  		t.Assert(err, nil)
    42  		dumpedText := r.RawRequest()
    43  		t.Assert(gstr.Contains(dumpedText, "test_for_request_body"), true)
    44  		dumpedText2 := r.RawResponse()
    45  		fmt.Println(dumpedText2)
    46  		t.Assert(gstr.Contains(dumpedText2, "test_for_response_body"), true)
    47  
    48  		client2 := g.Client().SetPrefix(url).ContentType("text/html")
    49  		r2, err := client2.Dump().Post("/hello2", g.Map{"field": "test_for_request_body"})
    50  		t.Assert(err, nil)
    51  		dumpedText3 := r2.RawRequest()
    52  		t.Assert(gstr.Contains(dumpedText3, "test_for_request_body"), true)
    53  		dumpedText4 := r2.RawResponse()
    54  		t.Assert(gstr.Contains(dumpedText4, "test_for_request_body"), false)
    55  	})
    56  }