Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

httputil: debug transport layer #845

Merged
merged 12 commits into from
Jun 14, 2024
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
}
Loading