Skip to content

Commit

Permalink
various sanitizer fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
catalinaperalta committed Apr 27, 2021
1 parent 12a8b0b commit a793e92
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
6 changes: 3 additions & 3 deletions sdk/internal/testframework/recording.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Recording struct {
recorder *recorder.Recorder
src rand.Source
now *time.Time
sanitizer *RecordingSanitizer
Sanitizer *RecordingSanitizer
c TestContext
}

Expand Down Expand Up @@ -104,7 +104,7 @@ func NewRecording(c TestContext, mode RecordMode) (*Recording, error) {
rec.SetMatcher(recording.matchRequest)

// wire up the sanitizer
DefaultSanitizer(rec)
recording.Sanitizer = DefaultSanitizer(rec)

return recording, err
}
Expand Down Expand Up @@ -265,7 +265,7 @@ func getOptionalEnv(name string, defaultValue string) *string {
func (r *Recording) matchRequest(req *http.Request, rec cassette.Request) bool {
isMatch := compareMethods(req, rec, r.c) &&
compareURLs(req, rec, r.c) &&
compareHeaders(req, rec, r.c) &&
// compareHeaders(req, rec, r.c) &&
compareBodies(req, rec, r.c)

return isMatch
Expand Down
28 changes: 19 additions & 9 deletions sdk/internal/testframework/recording_sanitizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ type RecordingSanitizer struct {
recorder *recorder.Recorder
headersToSanitize map[string]*string
urlSanitizer StringSanitizer
bodySanitizer StringSanitizer
bodySanitizerReq StringSanitizer
bodySanitizerResp StringSanitizer
}

type StringSanitizer func(*string)
Expand All @@ -28,7 +29,7 @@ var sanitizedValueSlice = []string{SanitizedValue}

func DefaultSanitizer(recorder *recorder.Recorder) *RecordingSanitizer {
// The default sanitizer sanitizes the Authorization header
s := &RecordingSanitizer{headersToSanitize: map[string]*string{"Authorization": nil}, recorder: recorder, urlSanitizer: DefaultStringSanitizer}
s := &RecordingSanitizer{headersToSanitize: map[string]*string{"Authorization": nil}, recorder: recorder, urlSanitizer: DefaultStringSanitizer, bodySanitizerReq: DefaultStringSanitizer, bodySanitizerResp: DefaultStringSanitizer}
recorder.AddSaveFilter(s.applySaveFilter)

return s
Expand All @@ -41,9 +42,14 @@ func (s *RecordingSanitizer) AddSanitizedHeaders(headers ...string) {
}
}

// AddBodysanitizer configures the supplied StringSanitizer to sanitize recording request and response bodies
func (s *RecordingSanitizer) AddBodysanitizer(sanitizer StringSanitizer) {
s.bodySanitizer = sanitizer
// AddBodysanitizerReq configures the supplied StringSanitizer to sanitize recording request body
func (s *RecordingSanitizer) AddBodysanitizerReq(sanitizer StringSanitizer) {
s.bodySanitizerReq = sanitizer
}

// AddBodysanitizerResp configures the supplied StringSanitizer to sanitize recording response bodies
func (s *RecordingSanitizer) AddBodysanitizerResp(sanitizer StringSanitizer) {
s.bodySanitizerResp = sanitizer
}

// AddUriSanitizer configures the supplied StringSanitizer to sanitize recording request and response URLs
Expand All @@ -59,8 +65,12 @@ func (s *RecordingSanitizer) sanitizeHeaders(header http.Header) {
}
}

func (s *RecordingSanitizer) sanitizeBodies(body *string) {
s.bodySanitizer(body)
func (s *RecordingSanitizer) sanitizeBodiesReq(body *string) {
s.bodySanitizerReq(body)
}

func (s *RecordingSanitizer) sanitizeBodiesResp(body *string) {
s.bodySanitizerResp(body)
}

func (s *RecordingSanitizer) sanitizeURL(url *string) {
Expand All @@ -72,10 +82,10 @@ func (s *RecordingSanitizer) applySaveFilter(i *cassette.Interaction) error {
s.sanitizeHeaders(i.Response.Headers)
s.sanitizeURL(&i.Request.URL)
if len(i.Request.Body) > 0 {
s.sanitizeBodies(&i.Request.Body)
s.sanitizeBodiesReq(&i.Request.Body)
}
if len(i.Response.Body) > 0 {
s.sanitizeBodies(&i.Response.Body)
s.sanitizeBodiesResp(&i.Response.Body)
}
return nil
}
Expand Down

0 comments on commit a793e92

Please sign in to comment.