Skip to content

Commit

Permalink
Fix replay bug with go-vcr v3 (#3914)
Browse files Browse the repository at this point in the history
* Remove count header for PUT requests

* Add logging for rejected replay matches
  • Loading branch information
theunrepentantgeek authored Apr 8, 2024
1 parent 9a4a581 commit be29b9c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
8 changes: 6 additions & 2 deletions v2/internal/testcommon/vcr/v3/test_recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,18 @@ func NewTestRecorder(

// verify custom request count header matches, if present
if header := r.Header.Get(COUNT_HEADER); header != "" {
if header != i.Headers.Get(COUNT_HEADER) {
interactionHeader := i.Headers.Get(COUNT_HEADER)
if header != interactionHeader {
log.Info("Request count header mismatch", COUNT_HEADER, header, "interaction", interactionHeader)
return false
}
}

// verify custom body hash header matches, if present
if header := r.Header.Get(HASH_HEADER); header != "" {
if header != i.Headers.Get(HASH_HEADER) {
interactionHeader := i.Headers.Get(HASH_HEADER)
if header != interactionHeader {
log.Info("Request body hash header mismatch", HASH_HEADER, header, "interaction", interactionHeader)
return false
}
}
Expand Down
16 changes: 3 additions & 13 deletions v2/internal/testcommon/vcr/v3/tracking_roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var _ http.RoundTripper = &requestCounter{}

func (rt *requestCounter) RoundTrip(req *http.Request) (*http.Response, error) {
if rt.useHash(req) {
rt.addContentHeaders(req)
rt.addHashHeader(req)
} else {
rt.addCountHeader(req)
}
Expand Down Expand Up @@ -86,8 +86,8 @@ func (rt *requestCounter) addCountHeader(req *http.Request) {
req.Header.Set(COUNT_HEADER, fmt.Sprintf("%d", count))
}

// addContentHeaders adds headers to the request based on the content of the request body
func (rt *requestCounter) addContentHeaders(request *http.Request) {
// addHashHeader adds a header to the request based on a hash of the content of the request body
func (rt *requestCounter) addHashHeader(request *http.Request) {
// Read all the content of the request body
var body bytes.Buffer
_, err := body.ReadFrom(request.Body)
Expand All @@ -102,22 +102,12 @@ func (rt *requestCounter) addContentHeaders(request *http.Request) {
// Calculate a hash based on body string
hash := sha256.Sum256([]byte(bodyString))

// Format hash as a hex string
hashString := fmt.Sprintf("%x", hash)

// Allocate a number based on the hash (not the URL)
rt.countsMutex.Lock()
count := rt.counts[hashString]
rt.counts[hashString] = count + 1
rt.countsMutex.Unlock()

// Set the headers
if request.Header == nil {
request.Header = make(http.Header)
}

request.Header.Set(HASH_HEADER, fmt.Sprintf("%x", hash))
request.Header.Set(COUNT_HEADER, fmt.Sprintf("%d", count))

// Reset the body so it can be read again
request.Body = io.NopCloser(&body)
Expand Down

0 comments on commit be29b9c

Please sign in to comment.