Skip to content

Commit

Permalink
httputil: debug transport layer (tmc#845)
Browse files Browse the repository at this point in the history
* feat: debug transport body
  • Loading branch information
gluonfield authored Jun 14, 2024
1 parent 62b0532 commit afcdc4b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ require (
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/getsentry/sentry-go v0.12.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/etcd-io/bbolt v1.3.3/go.mod h1:ZF2nL25h33cCyBtcyWeZ2/I3HQOfTP+0PIEvHjkjCrw=
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
Expand Down
51 changes: 51 additions & 0 deletions httputil/debug_transport_body.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package httputil

import (
"bytes"
"encoding/json"
"io"
"net/http"

"github.com/fatih/color"
)

var DebugHTTPColorJSON = &http.Client{ //nolint:gochecknoglobals
Transport: &logJSONTransport{http.DefaultTransport},
}

type logJSONTransport struct {
Transport http.RoundTripper
}

func (t *logJSONTransport) RoundTrip(req *http.Request) (*http.Response, error) {
requestBody, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}
req.Body = io.NopCloser(bytes.NewBuffer(requestBody))

var requestBodyJSON bytes.Buffer
if err := json.Indent(&requestBodyJSON, requestBody, "", " "); err != nil {
return nil, err
}
color.Blue(requestBodyJSON.String()) //nolint:forbidigo

resp, err := t.Transport.RoundTrip(req)
if err != nil {
return nil, err
}

responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
resp.Body = io.NopCloser(bytes.NewBuffer(responseBody))

var responseBodyJSON bytes.Buffer
if err := json.Indent(&responseBodyJSON, responseBody, "", " "); err != nil {
return nil, err
}
color.Green(responseBodyJSON.String()) //nolint:forbidigo

return resp, nil
}

0 comments on commit afcdc4b

Please sign in to comment.