如何在githubv4中使用go-vcr?- 获取httpClient传输

huangapple go评论108阅读模式
英文:

How to use go-vcr with githubv4 ? - getting httpClient Transport

问题

看一下go-vcr的设置

  1. // 启动录制器
  2. r, err := recorder.New("fixtures/etcd")
  3. if err != nil {
  4. log.Fatal(err)
  5. }
  6. defer r.Stop() // 确保录制器在使用完后停止
  7. // 使用我们的传输创建etcd配置
  8. cfg := client.Config{
  9. Endpoints: []string{"http://127.0.0.1:2379"},
  10. HeaderTimeoutPerRequest: time.Second,
  11. Transport: r, // 注入为传输!
  12. }

尝试使用githubv4库使用此库似乎需要一种处理Oauth的方式

  1. import "golang.org/x/oauth2"
  2. func main() {
  3. src := oauth2.StaticTokenSource(
  4. &oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
  5. )
  6. httpClient := oauth2.NewClient(context.Background(), src)
  7. client := githubv4.NewClient(httpClient)
  8. // 使用client...
  9. }

我不确定如何将录制器'r'传递给oauth2客户端。如果可能的话。

有人成功过吗?我尝试使用带有'r'录制器的httpClient,但最终返回401 - 看起来这个默认客户端无法进行Oauth验证。

我想使用GraphQL API,但如果更容易的话,可以回退到REST API,但我只是想确保这是否真的可行。有其他人成功过吗?

英文:

Looking at the set up for go-vcr

  1. // Start our recorder
  2. r, err := recorder.New("fixtures/etcd")
  3. if err != nil {
  4. log.Fatal(err)
  5. }
  6. defer r.Stop() // Make sure recorder is stopped once done with it
  7. // Create an etcd configuration using our transport
  8. cfg := client.Config{
  9. Endpoints: []string{"http://127.0.0.1:2379"},
  10. HeaderTimeoutPerRequest: time.Second,
  11. Transport: r, // Inject as transport!
  12. }

Attempting to use this library using the githubv4 library seems at though it needs a way to handle Oauth

  1. import "golang.org/x/oauth2"
  2. func main() {
  3. src := oauth2.StaticTokenSource(
  4. &oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
  5. )
  6. httpClient := oauth2.NewClient(context.Background(), src)
  7. client := githubv4.NewClient(httpClient)
  8. // Use client...
  9. }

I'm not sure how to get the recorder 'r' into the oauth2 client. If at all possible.

Has anyone been successful with this? I've tried passing in a httpClient with the 'r' recorder but it ends up as a 401 - looks like this default client can't do the Oauth dance.

I'd like to use the GraphQL API but can fall back to the REST API if is is easier but I just want to make sure this isn't really possible. Has anyone else been successful with this?

答案1

得分: 0

这个问题对我来说解决了这个问题。
https://github.com/dnaeon/go-vcr/issues/59

下面是示例代码:

  1. package example_test
  2. import (
  3. "context"
  4. "github.com/dnaeon/go-vcr/cassette"
  5. "github.com/dnaeon/go-vcr/recorder"
  6. "github.com/google/go-github/v33/github"
  7. "github.com/stretchr/testify/require"
  8. "golang.org/x/oauth2"
  9. "net/http"
  10. "path"
  11. "testing"
  12. )
  13. func TestGithub(t *testing.T) {
  14. // 自定义http.Transport,因为github使用oauth2身份验证
  15. ts := oauth2.StaticTokenSource(
  16. &oauth2.Token{AccessToken: "YOUR_GITHUB_TOKEN"},
  17. )
  18. tr := &oauth2.Transport{
  19. Base: http.DefaultTransport,
  20. Source: oauth2.ReuseTokenSource(nil, ts),
  21. }
  22. // 启动录制器
  23. vcrRecorder, err := recorder.NewAsMode(path.Join("testdata", "fixtures", t.Name()), recorder.ModeRecording, tr)
  24. require.NoError(t, err)
  25. defer vcrRecorder.Stop() // 新添加的代码
  26. // 过滤动态和敏感数据/头部信息
  27. // 在记录的交互保存之前,您的测试代码将继续看到真实的访问令牌,并且在保存之前会进行编辑
  28. // =====> 注释掉这一部分对缺失的录音没有影响
  29. vcrRecorder.AddSaveFilter(func(i *cassette.Interaction) error {
  30. delete(i.Request.Headers, "Authorization")
  31. delete(i.Request.Headers, "User-Agent")
  32. i.Request.Headers["Authorization"] = []string{"Basic UExBQ0VIT0xERVI6UExBQ0VIT0xERVI="} //PLACEHOLDER:PLACEHOLDER
  33. return nil
  34. })
  35. // 自定义http.client
  36. httpClient := &http.Client{
  37. Transport: vcrRecorder,
  38. }
  39. ghClient := github.NewClient(httpClient)
  40. // =====> 实际测试,应该创建cassettes,但没有。
  41. _, _, err = ghClient.Users.Get(context.Background(), "")
  42. require.NoError(t, err)
  43. }

希望对你有帮助!

英文:

This issue resolved this question for me.
https://github.com/dnaeon/go-vcr/issues/59

Example below

  1. package example_test
  2. import (
  3. "context"
  4. "github.com/dnaeon/go-vcr/cassette"
  5. "github.com/dnaeon/go-vcr/recorder"
  6. "github.com/google/go-github/v33/github"
  7. "github.com/stretchr/testify/require"
  8. "golang.org/x/oauth2"
  9. "net/http"
  10. "path"
  11. "testing"
  12. )
  13. func TestGithub(t *testing.T) {
  14. //custom http.Transport, since github uses oauth2 authentication
  15. ts := oauth2.StaticTokenSource(
  16. &oauth2.Token{AccessToken: "YOUR_GITHUB_TOKEN"},
  17. )
  18. tr := &oauth2.Transport{
  19. Base: http.DefaultTransport,
  20. Source: oauth2.ReuseTokenSource(nil, ts),
  21. }
  22. // Start our recorder
  23. vcrRecorder, err := recorder.NewAsMode(path.Join("testdata", "fixtures", t.Name()), recorder.ModeRecording, tr)
  24. require.NoError(t, err)
  25. defer vcrRecorder.Stop() // NEWLY ADDED CODE HERE
  26. // Filter out dynamic & sensitive data/headers
  27. // Your test code will continue to see the real access token and
  28. // it is redacted before the recorded interactions are saved
  29. // =====> commenting out this section has no impact on missing recording
  30. vcrRecorder.AddSaveFilter(func(i *cassette.Interaction) error {
  31. delete(i.Request.Headers, "Authorization")
  32. delete(i.Request.Headers, "User-Agent")
  33. i.Request.Headers["Authorization"] = []string{"Basic UExBQ0VIT0xERVI6UExBQ0VIT0xERVI="} //PLACEHOLDER:PLACEHOLDER
  34. return nil
  35. })
  36. // custom http.client
  37. httpClient := &http.Client{
  38. Transport: vcrRecorder,
  39. }
  40. ghClient := github.NewClient(httpClient)
  41. // =====> actual test, should create cassettes, but does not.
  42. _, _, err = ghClient.Users.Get(context.Background(), "")
  43. require.NoError(t, err)
  44. }

huangapple
  • 本文由 发表于 2022年2月27日 13:25:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/71282327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定