如何将 stripe-go 指向 stripe-mock 本地服务器?
How to point stripe-go to stripe-mock local server?
我正在使用 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,这是一个模拟 HTTP 服务器,响应速度与真正的 Stripe API 一样。我在终端本地有 运行。我如何指向我在我的 Go 应用程序中使用的 stripe-go 来调用这个模拟 HTTP 服务器而不是真正的 Stripe API.
在 stripe-go 文档中有这个
// Setup
stripe.Key = "sk_key"
stripe.SetBackend("api", backend) // optional, useful for mocking
我是否应该传递一些特殊的东西作为 backend
以便 stripe-go 知道然后调用 http://localhost:12111 这是 stripe-mock 的地址?
这是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
}
}
这是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
}
查看 stripe.go,公开公开的 BackendConfiguration
结构实现了 Backend
接口。所以以下应该有效:
httpClient := &http.Client{Timeout: defaultHTTPTimeout}
mockBackend := &stripe.BackendConfiguration{
"api",
"http://localhost:12111",
httpClient,
}
stripe.SetBackend("api", mockBackend)
我正在使用 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,这是一个模拟 HTTP 服务器,响应速度与真正的 Stripe API 一样。我在终端本地有 运行。我如何指向我在我的 Go 应用程序中使用的 stripe-go 来调用这个模拟 HTTP 服务器而不是真正的 Stripe API.
在 stripe-go 文档中有这个
// Setup
stripe.Key = "sk_key"
stripe.SetBackend("api", backend) // optional, useful for mocking
我是否应该传递一些特殊的东西作为 backend
以便 stripe-go 知道然后调用 http://localhost:12111 这是 stripe-mock 的地址?
这是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
}
}
这是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
}
查看 stripe.go,公开公开的 BackendConfiguration
结构实现了 Backend
接口。所以以下应该有效:
httpClient := &http.Client{Timeout: defaultHTTPTimeout}
mockBackend := &stripe.BackendConfiguration{
"api",
"http://localhost:12111",
httpClient,
}
stripe.SetBackend("api", mockBackend)