1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
package httprecorder
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/dnaeon/go-vcr/cassette"
"github.com/dnaeon/go-vcr/recorder"
"github.com/scaleway/scaleway-sdk-go/errors"
"github.com/scaleway/scaleway-sdk-go/scw"
)
// IsUpdatingCassette returns true if we are updating cassettes.
func IsUpdatingCassette() bool {
return os.Getenv("SDK_UPDATE_CASSETTES") == "true"
}
// CreateRecordedScwClient creates a new scw.Client that records all HTTP requests in a cassette.
// This cassette is then replayed whenever tests are executed again. This means that once the
// requests are recorded in the cassette, no more real HTTP request must be made to run the tests.
//
// It is important to call add a `defer recorder.Stop()` so the given cassette files are correctly
// closed and saved after the requests.
//
// To update the cassette files, add `SDK_UPDATE_CASSETTES=true` to the environment variables.
// When updating cassettes, make sure your Scaleway credentials are set in your config or in the
// variables `SCW_ACCESS_KEY` and `SCW_SECRET_KEY`.
func CreateRecordedScwClient(cassetteName string) (*scw.Client, *recorder.Recorder, error) {
UpdateCassette := IsUpdatingCassette()
var activeProfile *scw.Profile
recorderMode := recorder.ModeReplaying
if UpdateCassette {
recorderMode = recorder.ModeRecording
config, err := scw.LoadConfig()
if err != nil {
return nil, nil, err
}
activeProfile, err = config.GetActiveProfile()
if err != nil {
return nil, nil, err
}
}
// Setup recorder and scw client
r, err := recorder.NewAsMode("testdata/"+cassetteName, recorderMode, nil)
if err != nil {
return nil, nil, err
}
// Add a filter which removes Authorization headers from all requests:
r.AddFilter(func(i *cassette.Interaction) error {
delete(i.Request.Headers, "x-auth-token")
delete(i.Request.Headers, "X-Auth-Token")
if UpdateCassette {
secretKey := *activeProfile.SecretKey
if i != nil && strings.Contains(fmt.Sprintf("%v", *i), secretKey) {
panic(errors.New("found secret key in cassette"))
}
}
return nil
})
// Create new http.Client where transport is the recorder
httpClient := &http.Client{Transport: r}
var client *scw.Client
if UpdateCassette {
// When updating the recoreded test requests, we need the access key and secret key.
client, err = scw.NewClient(
scw.WithHTTPClient(httpClient),
scw.WithProfile(activeProfile),
scw.WithEnv(),
scw.WithDefaultRegion(scw.RegionFrPar),
scw.WithDefaultZone(scw.ZoneFrPar1),
)
if err != nil {
return nil, nil, err
}
} else {
// No need for auth when using cassette
client, err = scw.NewClient(
scw.WithHTTPClient(httpClient),
scw.WithDefaultRegion(scw.RegionFrPar),
scw.WithDefaultZone(scw.ZoneFrPar1),
)
if err != nil {
return nil, nil, err
}
}
return client, r, nil
}
|