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 python怎么在已有视频文件后添加新帧
  • ¥20 虚幻UE引擎如何让多个同一个蓝图的NPC执行一样的动画,
  • ¥15 fluent里模拟降膜反应的UDF编写
  • ¥15 MYSQL 多表拼接link
  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵