1 回答
TA贡献1757条经验 获得超7个赞
这个问题为我解决了这个问题。 https://github.com/dnaeon/go-vcr/issues/59
下面的例子
package example_test
import (
"context"
"github.com/dnaeon/go-vcr/cassette"
"github.com/dnaeon/go-vcr/recorder"
"github.com/google/go-github/v33/github"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"
"net/http"
"path"
"testing"
)
func TestGithub(t *testing.T) {
//custom http.Transport, since github uses oauth2 authentication
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: "YOUR_GITHUB_TOKEN"},
)
tr := &oauth2.Transport{
Base: http.DefaultTransport,
Source: oauth2.ReuseTokenSource(nil, ts),
}
// Start our recorder
vcrRecorder, err := recorder.NewAsMode(path.Join("testdata", "fixtures", t.Name()), recorder.ModeRecording, tr)
require.NoError(t, err)
defer vcrRecorder.Stop() // NEWLY ADDED CODE HERE
// Filter out dynamic & sensitive data/headers
// Your test code will continue to see the real access token and
// it is redacted before the recorded interactions are saved
// =====> commenting out this section has no impact on missing recording
vcrRecorder.AddSaveFilter(func(i *cassette.Interaction) error {
delete(i.Request.Headers, "Authorization")
delete(i.Request.Headers, "User-Agent")
i.Request.Headers["Authorization"] = []string{"Basic UExBQ0VIT0xERVI6UExBQ0VIT0xERVI="} //PLACEHOLDER:PLACEHOLDER
return nil
})
// custom http.client
httpClient := &http.Client{
Transport: vcrRecorder,
}
ghClient := github.NewClient(httpClient)
// =====> actual test, should create cassettes, but does not.
_, _, err = ghClient.Users.Get(context.Background(), "")
require.NoError(t, err)
}
- 1 回答
- 0 关注
- 99 浏览
添加回答
举报