Skip to content

Commit 1f97f4f

Browse files
authored
feat: Support redacting the response body (#54)
1 parent 8448fcf commit 1f97f4f

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

internal/record/recording_https_proxy.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (r *RecordingHTTPSProxy) proxyRequest(w http.ResponseWriter, req *http.Requ
182182
}
183183

184184
func (r *RecordingHTTPSProxy) recordResponse(recReq *store.RecordedRequest, resp *http.Response, fileName string, shaSum string, body []byte) error {
185-
recordedResponse, err := store.NewRecordedResponse(resp, body)
185+
recordedResponse, err := store.NewRecordedResponse(resp, r.redactor, body)
186186
if err != nil {
187187
return err
188188
}
@@ -204,9 +204,9 @@ func (r *RecordingHTTPSProxy) recordResponse(recReq *store.RecordedRequest, resp
204204
recordPath := filepath.Join(r.recordingDir, fileName+".json")
205205

206206
recordDir := filepath.Dir(recordPath)
207-
if err := os.MkdirAll(recordDir, 0755); err != nil {
208-
return err
209-
}
207+
if err := os.MkdirAll(recordDir, 0755); err != nil {
208+
return err
209+
}
210210

211211
// Default to overwriting the file.
212212
fileMode := os.O_TRUNC

internal/store/store.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"strings"
3131

3232
"github.com/google/test-server/internal/config"
33+
"github.com/google/test-server/internal/redact"
3334
)
3435

3536
const HeadSHA = "b4d6e60a9b97e7b98c63df9308728c5c88c0b40c398046772c63447b94608b4d"
@@ -162,7 +163,7 @@ func (r *RecordedRequest) RedactHeaders(headers []string) {
162163
}
163164
}
164165

165-
func NewRecordedResponse(resp *http.Response, body []byte) (*RecordedResponse, error) {
166+
func NewRecordedResponse(resp *http.Response, redactor *redact.Redact, body []byte) (*RecordedResponse, error) {
166167
if resp.Header.Get("Content-Encoding") == "gzip" {
167168
gzipReader, err := gzip.NewReader(bytes.NewReader(body))
168169
if err != nil {
@@ -206,7 +207,7 @@ func NewRecordedResponse(resp *http.Response, body []byte) (*RecordedResponse, e
206207
continue
207208
}
208209

209-
bodySegments = append(bodySegments, jsonMap)
210+
bodySegments = append(bodySegments, redactor.Map(jsonMap))
210211
}
211212
}
212213

@@ -215,7 +216,7 @@ func NewRecordedResponse(resp *http.Response, body []byte) (*RecordedResponse, e
215216
return nil, err
216217
}
217218
} else {
218-
bodySegments = append(bodySegments, bodySegment)
219+
bodySegments = append(bodySegments, redactor.Map(bodySegment))
219220
}
220221

221222
recordedResponse := &RecordedResponse{

0 commit comments

Comments
 (0)