Skip to content

Commit

Permalink
Fix request body handling
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmclean committed Aug 12, 2024
1 parent eee885b commit 392fd3c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cassette/server_replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"slices"
"strings"
"testing"
)

Expand Down Expand Up @@ -39,6 +40,10 @@ func TestInteractionReplay(t *testing.T, handler http.Handler, interaction *Inte
t.Errorf("unexpected error getting interaction request: %v", err)
}

if len(req.Form) > 0 {
req.Body = io.NopCloser(strings.NewReader(req.Form.Encode()))
}

w := httptest.NewRecorder()
handler.ServeHTTP(w, req)

Expand Down
9 changes: 9 additions & 0 deletions recorder/middleware.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package recorder

import (
"bytes"
"io"
"net/http"
"net/http/httptest"
)
Expand All @@ -9,8 +11,15 @@ import (
func (rec *Recorder) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ww := newPassthrough(w)

// Tee the body so it can be read by the next handler and by the recorder
body := &bytes.Buffer{}
r.Body = io.NopCloser(io.TeeReader(r.Body, body))

next.ServeHTTP(ww, r)

r.Body = io.NopCloser(body)

// On the server side, requests do not have Host and Scheme so it must be set
r.URL.Host = "go-vcr"
r.URL.Scheme = "http"
Expand Down

0 comments on commit 392fd3c

Please sign in to comment.