Skip to content

Commit c850d96

Browse files
committed
read request body when matching
1 parent 37fd92a commit c850d96

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

internal/core/testing_recorder.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package core
22

33
import (
4+
"bytes"
5+
"io"
6+
"log"
47
"net/http"
58
"regexp"
69
"testing"
@@ -27,6 +30,9 @@ func cassetteResponseFilter(i *cassette.Interaction) error {
2730
i.Request.URL = regexp.MustCompile(`pack\.local%2Fbuilder%2F[0-9a-f]{20}`).ReplaceAllString(i.Request.URL, "pack.local%2Fbuilder%2F11111111111111111111")
2831
i.Request.URL = regexp.MustCompile(`pack\.local/builder/[0-9a-f]{20}`).ReplaceAllString(i.Request.URL, "pack.local/builder/11111111111111111111")
2932

33+
i.Request.Body = regexp.MustCompile(`pack\.local/builder/[0-9a-f]{20}`).ReplaceAllString(i.Response.Body, "pack.local/builder/11111111111111111111")
34+
i.Response.Body = regexp.MustCompile(`pack\.local/builder/[0-9a-f]{20}`).ReplaceAllString(i.Response.Body, "pack.local/builder/11111111111111111111")
35+
3036
return nil
3137
}
3238

@@ -39,6 +45,15 @@ func cassetteMatcher(r *http.Request, i cassette.Request) bool {
3945
r.URL.RawQuery = regexp.MustCompile(`pack\.local%2Fbuilder%2F[0-9a-f]{20}`).ReplaceAllString(r.URL.RawQuery, "pack.local%2Fbuilder%2F11111111111111111111")
4046
r.URL.Path = regexp.MustCompile(`pack\.local/builder/[0-9a-f]{20}`).ReplaceAllString(r.URL.Path, "pack.local/builder/11111111111111111111")
4147

48+
if r.Body != nil && r.Body != http.NoBody {
49+
reqBody, err := io.ReadAll(r.Body)
50+
if err != nil {
51+
log.Fatal("failed to read request body")
52+
}
53+
r.Body.Close()
54+
r.Body = io.NopCloser(bytes.NewBuffer(reqBody))
55+
}
56+
4257
return cassette.DefaultMatcher(r, i)
4358
}
4459

0 commit comments

Comments
 (0)