From 6f0f6bbe83d4787fa584c714a0db53213d07d97c Mon Sep 17 00:00:00 2001 From: annhiluc Date: Thu, 23 Oct 2025 04:09:14 +0000 Subject: [PATCH] feat: Support redacting the response body --- internal/record/recording_https_proxy.go | 8 ++++---- internal/store/store.go | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/internal/record/recording_https_proxy.go b/internal/record/recording_https_proxy.go index 02575c5..4becf1a 100644 --- a/internal/record/recording_https_proxy.go +++ b/internal/record/recording_https_proxy.go @@ -182,7 +182,7 @@ func (r *RecordingHTTPSProxy) proxyRequest(w http.ResponseWriter, req *http.Requ } func (r *RecordingHTTPSProxy) recordResponse(recReq *store.RecordedRequest, resp *http.Response, fileName string, shaSum string, body []byte) error { - recordedResponse, err := store.NewRecordedResponse(resp, body) + recordedResponse, err := store.NewRecordedResponse(resp, r.redactor, body) if err != nil { return err } @@ -204,9 +204,9 @@ func (r *RecordingHTTPSProxy) recordResponse(recReq *store.RecordedRequest, resp recordPath := filepath.Join(r.recordingDir, fileName+".json") recordDir := filepath.Dir(recordPath) - if err := os.MkdirAll(recordDir, 0755); err != nil { - return err - } + if err := os.MkdirAll(recordDir, 0755); err != nil { + return err + } // Default to overwriting the file. fileMode := os.O_TRUNC diff --git a/internal/store/store.go b/internal/store/store.go index 107b8ea..2241e89 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -30,6 +30,7 @@ import ( "strings" "github.com/google/test-server/internal/config" + "github.com/google/test-server/internal/redact" ) const HeadSHA = "b4d6e60a9b97e7b98c63df9308728c5c88c0b40c398046772c63447b94608b4d" @@ -162,7 +163,7 @@ func (r *RecordedRequest) RedactHeaders(headers []string) { } } -func NewRecordedResponse(resp *http.Response, body []byte) (*RecordedResponse, error) { +func NewRecordedResponse(resp *http.Response, redactor *redact.Redact, body []byte) (*RecordedResponse, error) { if resp.Header.Get("Content-Encoding") == "gzip" { gzipReader, err := gzip.NewReader(bytes.NewReader(body)) if err != nil { @@ -206,7 +207,7 @@ func NewRecordedResponse(resp *http.Response, body []byte) (*RecordedResponse, e continue } - bodySegments = append(bodySegments, jsonMap) + bodySegments = append(bodySegments, redactor.Map(jsonMap)) } } @@ -215,7 +216,7 @@ func NewRecordedResponse(resp *http.Response, body []byte) (*RecordedResponse, e return nil, err } } else { - bodySegments = append(bodySegments, bodySegment) + bodySegments = append(bodySegments, redactor.Map(bodySegment)) } recordedResponse := &RecordedResponse{