duanmorong9597 2017-11-28 10:01 采纳率: 100%
浏览 110
已采纳

如何将stripe-go指向stripe-mock本地服务器?

I am using stripe-go to perform Stripe actions such as create a new customer, add a credit card, create a subscription and so on. I want to perform end-to-end testing. There is stripe-mock which is a mock HTTP server that responds like the real Stripe API. I have that running locally in a terminal. How do I point my stripe-go which I am using in my Go application to make calls to this mock HTTP server instead of the real Stripe API.

In the stripe-go documentation there is this

// Setup
stripe.Key = "sk_key"

stripe.SetBackend("api", backend) // optional, useful for mocking

Am I supposed to pass something special as backend so that stripe-go knows to then make calls to http://localhost:12111 which is the address of stripe-mock?

This is stripe.SetBackend

// SetBackend sets the backend used in the binding.
func SetBackend(backend SupportedBackend, b Backend) {
    switch backend {
    case APIBackend:
        backends.API = b
    case UploadsBackend:
        backends.Uploads = b
    }
}

This is Backend

// Backend is an interface for making calls against a Stripe service.
// This interface exists to enable mocking for during testing if needed.
type Backend interface {
    Call(method, path, key string, body *RequestValues, params *Params, v interface{}) error
    CallMultipart(method, path, key, boundary string, body io.Reader, params *Params, v interface{}) error
}
  • 写回答

1条回答 默认 最新

  • dongshadu4498 2017-11-28 11:23
    关注

    Looking in stripe.go, the publicly exposed BackendConfiguration struct implements the Backend interface. So the following should work:

    httpClient := &http.Client{Timeout: defaultHTTPTimeout}
    
    mockBackend := &stripe.BackendConfiguration{
          "api", 
          "http://localhost:12111", 
          httpClient,
    }
    
    stripe.SetBackend("api", mockBackend)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端