Skip to content

Commit

Permalink
fix(debug_mode): Fix FASTLY_DEBUG_MODE when used in combination with …
Browse files Browse the repository at this point in the history
…go-vcr. (#561)

While running tests, if go-vcr was enabled (VCR_DISABLE was not set to
'1'), and FASTLY_DEBUG_MODE was set to 'true', and no go-vcr cassettes
existed for the tests being run, then any tests which sent HTTP
request with bodies (PUT, PATCH, etc.) would fail as the combination
of these features would result in an empty body being sent as part of
the request.

This was discovered while running tests for new endpoints, so no
cassettes existed, and FASTLY_DEBUG_MODE was enabled in order to
monitor the actual HTTP requests and responses.
  • Loading branch information
kpfleming authored Nov 6, 2024
1 parent 6d6ad25 commit 924d909
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions fastly/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,22 @@ func (c *Client) Request(verb, p string, ro *RequestOptions) (*http.Response, er

if c.DebugMode {
r := req.Clone(context.Background())
// 'r' and 'req' both have a reference to a Body that
// is an io.Reader, but only one of them can read its
// contents since io.Reader is not seekable and cannot
// be rewound

r.Header.Del("Fastly-Key")
dump, _ := httputil.DumpRequest(r, true)

// httputil.DumpRequest has read the Body from 'r',
// and set r.Body to an io.ReadCloser that will return
// the same bytes as the original Body, so we can
// stuff that into 'req' for when the request is
// actually sent; it can't be read in 'r', but the
// lifetime of 'r' ends at the end of this block
req.Body = r.Body

fmt.Printf("http.Request (dump): %q\n", dump)
}

Expand Down

0 comments on commit 924d909

Please sign in to comment.