11package acctest
22
33import (
4+ "encoding/base64"
5+ "encoding/json"
6+ "encoding/xml"
47 "flag"
58 "net/http"
69 "os"
@@ -15,6 +18,8 @@ import (
1518 "github.com/scaleway/terraform-provider-scaleway/v2/internal/provider"
1619 "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport"
1720 "github.com/stretchr/testify/require"
21+ "gopkg.in/dnaeon/go-vcr.v4/pkg/cassette"
22+ "gopkg.in/dnaeon/go-vcr.v4/pkg/recorder"
1823)
1924
2025// UpdateCassettes will update all cassettes of a given test
@@ -29,10 +34,42 @@ type TestTools struct {
2934 Cleanup func ()
3035}
3136
37+ // s3Encoder encodes binary payloads as base64 because serialization changed on go-vcr.v4
38+ func s3Encoder (i * cassette.Interaction ) error {
39+ if strings .HasSuffix (i .Request .Host , "scw.cloud" ) {
40+ if i .Request .Body != "" && i .Request .Headers .Get ("Content-Type" ) == "application/octet-stream" {
41+ requestBody := []byte (i .Request .Body )
42+ if ! json .Valid (requestBody ) {
43+ err := xml .Unmarshal (requestBody , new (any ))
44+ if err != nil {
45+ i .Request .Body = base64 .StdEncoding .EncodeToString (requestBody )
46+ }
47+ }
48+ }
49+
50+ if i .Response .Body != "" && i .Response .Headers .Get ("Content-Type" ) == "binary/octet-stream" {
51+ responseBody := []byte (i .Response .Body )
52+ if ! json .Valid (responseBody ) {
53+ err := xml .Unmarshal (responseBody , new (any ))
54+ if err != nil {
55+ i .Response .Body = base64 .StdEncoding .EncodeToString (responseBody )
56+ }
57+ }
58+ }
59+ }
60+
61+ return nil
62+ }
63+
3264func NewRecordedClient (t * testing.T , pkgFolder string , update bool ) (client * http.Client , cleanup func (), err error ) {
3365 t .Helper ()
3466
35- r , err := vcr .NewHTTPRecorder (t , pkgFolder , update )
67+ s3EncoderHook := vcr.AdditionalHook {
68+ HookFunc : s3Encoder ,
69+ Kind : recorder .AfterCaptureHook ,
70+ }
71+
72+ r , err := vcr .NewHTTPRecorder (t , pkgFolder , update , nil , s3EncoderHook )
3673 if err != nil {
3774 return nil , nil , err
3875 }
0 commit comments