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 vue+element 生成table
    • ¥15 实验 4 FIFO 算法和 LRU 算法-C 程序实现
    • ¥30 电脑画面同步投屏,通过同wifi的方式投屏方法,接收投屏端不需要安装第三方软件,
    • ¥15 有偿拼接大疆精灵4RGB影像
    • ¥15 MATLAB特殊符号
    • ¥15 Arduino实现音频混响
    • ¥15 cuda.jit加速报错
    • ¥15 Octave 安装工具箱出错 Only Win32 target is supported!
    • ¥15 docker save的不能在另一台设备运行
    • ¥15 Unity Animation Rigging使用问题