From 1a0186bc6384061cccba85875a1c6899c8ffb800 Mon Sep 17 00:00:00 2001 From: Patrick Zheng Date: Fri, 11 Oct 2024 14:19:20 +0800 Subject: [PATCH] update Signed-off-by: Patrick Zheng --- internal/trace/transport.go | 17 +++++++++-------- internal/trace/transport_test.go | 4 ++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/internal/trace/transport.go b/internal/trace/transport.go index 620a2173b..6346ea4dd 100644 --- a/internal/trace/transport.go +++ b/internal/trace/transport.go @@ -58,7 +58,7 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error var logs strings.Builder fmt.Fprintf(&logs, "> Request: %q %q\n", req.Method, req.URL) fmt.Fprintln(&logs, "> Request headers:") - fmt.Fprint(&logs, logHeader(req.Header)) + logHeader(req.Header, &logs) resp, err = t.RoundTripper.RoundTrip(req) if err != nil { @@ -70,26 +70,27 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error } else { fmt.Fprintf(&logs, "< Response status: %q\n", resp.Status) fmt.Fprintln(&logs, "< Response headers:") - fmt.Fprint(&logs, logHeader(resp.Header)) + logHeader(resp.Header, &logs) e.Debug(logs.String()) } return resp, err } -// logHeader returns string of provided header keys and values, with auth header +// logHeader logs the provided header keys and values, with auth header // scrubbed. -func logHeader(header http.Header) string { +func logHeader(header http.Header, logs *strings.Builder) { + if logs == nil { + return + } if len(header) > 0 { - var logs strings.Builder for k, v := range header { if strings.EqualFold(k, "Authorization") { v = []string{"*****"} } - fmt.Fprintf(&logs, " %q: %q\n", k, strings.Join(v, ", ")) + fmt.Fprintf(logs, " %q: %q\n", k, strings.Join(v, ", ")) } - return logs.String() } else { - return " Empty header\n" + fmt.Fprintln(logs, " Empty header") } } diff --git a/internal/trace/transport_test.go b/internal/trace/transport_test.go index 58e6dfbc2..1d1854640 100644 --- a/internal/trace/transport_test.go +++ b/internal/trace/transport_test.go @@ -87,3 +87,7 @@ func TestTransport_RoundTrip(t *testing.T) { } }) } + +func TestLogHeaderWithNilLogs(t *testing.T) { + logHeader(http.Header{}, nil) +}