Skip to content

Commit

Permalink
test: Add custom transport test case
Browse files Browse the repository at this point in the history
  • Loading branch information
timandy committed Aug 3, 2023
1 parent 6c10f9d commit 8f48ae4
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3193,3 +3193,61 @@ func Test_AddMissingPort(t *testing.T) {
})
}
}

type TransportEx struct {
base RoundTripper
reqLog *string
respLog *string
}

func (t TransportEx) RoundTrip(hc *HostClient, req *Request, resp *Response) (bool, error) {
req.Header.Set("trace-id", "123")
*t.reqLog = req.String()
retry, err := t.base.RoundTrip(hc, req, resp)
*t.respLog = resp.String()
return retry, err
}

func TestClientTransportEx(t *testing.T) {
var reqLog string
var respLog string

sHTTP := startEchoServer(t, "tcp", "127.0.0.1:")
defer sHTTP.Stop()

sHTTPS := startEchoServerTLS(t, "tcp", "127.0.0.1:")
defer sHTTPS.Stop()

c := &Client{
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
},
ConfigureClient: func(hc *HostClient) error {
hc.Transport = TransportEx{base: DefaultTransport, reqLog: &reqLog, respLog: &respLog}
return nil
},
}

for i := 0; i < 4; i++ {
addr := "http://" + sHTTP.Addr()
if i&1 != 0 {
addr = "https://" + sHTTPS.Addr()
}
// test get
testClientGet(t, c, addr, 20)
if !strings.Contains(reqLog, "Trace-Id: 123") {
t.Errorf("request log should contains: %v", "Trace-Id: 123")
}
if !strings.Contains(respLog, "HTTP/1.1 200 OK") {
t.Errorf("response log should contains: %v", "HTTP/1.1 200 OK")
}
// test post
testClientPost(t, c, addr, 10)
if !strings.Contains(reqLog, "Trace-Id: 123") {
t.Errorf("request log should contains: %v", "Trace-Id: 123")
}
if !strings.Contains(respLog, "HTTP/1.1 200 OK") {
t.Errorf("response log should contains: %v", "HTTP/1.1 200 OK")
}
}
}

0 comments on commit 8f48ae4

Please sign in to comment.