Skip to content

Commit 16826cd

Browse files
committed
add s3EncoderHook to fix SnapshotFromS3 test
1 parent a232086 commit 16826cd

File tree

2 files changed

+2582
-6850
lines changed

2 files changed

+2582
-6850
lines changed

internal/acctest/acctest.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package acctest
22

33
import (
4+
"encoding/base64"
5+
"encoding/json"
6+
"encoding/xml"
47
"net/http"
58
"os"
69
"strconv"
@@ -17,6 +20,8 @@ import (
1720
"github.com/scaleway/terraform-provider-scaleway/v2/internal/transport"
1821
"github.com/scaleway/terraform-provider-scaleway/v2/provider"
1922
"github.com/stretchr/testify/require"
23+
"gopkg.in/dnaeon/go-vcr.v4/pkg/cassette"
24+
"gopkg.in/dnaeon/go-vcr.v4/pkg/recorder"
2025
)
2126

2227
func PreCheck(_ *testing.T) {}
@@ -43,10 +48,42 @@ func folderUsesVCRv4(fullFolderPath string) bool {
4348
return false
4449
}
4550

51+
// s3Encoder encodes binary payloads as base64 because serialization changed on go-vcr.v4
52+
func s3Encoder(i *cassette.Interaction) error {
53+
if strings.HasSuffix(i.Request.Host, "scw.cloud") {
54+
if i.Request.Body != "" && i.Request.Headers.Get("Content-Type") == "application/octet-stream" {
55+
requestBody := []byte(i.Request.Body)
56+
if !json.Valid(requestBody) {
57+
err := xml.Unmarshal(requestBody, new(any))
58+
if err != nil {
59+
i.Request.Body = base64.StdEncoding.EncodeToString(requestBody)
60+
}
61+
}
62+
}
63+
64+
if i.Response.Body != "" && i.Response.Headers.Get("Content-Type") == "binary/octet-stream" {
65+
responseBody := []byte(i.Response.Body)
66+
if !json.Valid(responseBody) {
67+
err := xml.Unmarshal(responseBody, new(any))
68+
if err != nil {
69+
i.Response.Body = base64.StdEncoding.EncodeToString(responseBody)
70+
}
71+
}
72+
}
73+
}
74+
75+
return nil
76+
}
77+
4678
func NewRecordedClient(t *testing.T, pkgFolder string, update bool) (client *http.Client, cleanup func(), err error) {
4779
t.Helper()
4880

49-
r, err := vcr.NewHTTPRecorder(t, pkgFolder, update, nil)
81+
s3EncoderHook := vcr.AdditionalHook{
82+
HookFunc: s3Encoder,
83+
Kind: recorder.AfterCaptureHook,
84+
}
85+
86+
r, err := vcr.NewHTTPRecorder(t, pkgFolder, update, nil, s3EncoderHook)
5087
if err != nil {
5188
return nil, nil, err
5289
}

0 commit comments

Comments
 (0)