Go 如何将条带指向条带模拟本地服务器?
我正在使用执行条带操作,例如创建新客户、添加信用卡、创建订阅等。我想进行端到端测试。有一个模拟HTTP服务器,它的响应类似于真正的条带API。我在终端本地运行。我如何指向我在go应用程序中使用的stripe go来调用这个模拟HTTP服务器,而不是真正的stripe API 在stripe go文档中有以下内容Go 如何将条带指向条带模拟本地服务器?,go,integration-testing,stripe-payments,Go,Integration Testing,Stripe Payments,我正在使用执行条带操作,例如创建新客户、添加信用卡、创建订阅等。我想进行端到端测试。有一个模拟HTTP服务器,它的响应类似于真正的条带API。我在终端本地运行。我如何指向我在go应用程序中使用的stripe go来调用这个模拟HTTP服务器,而不是真正的stripe API 在stripe go文档中有以下内容 // Setup stripe.Key = "sk_key" stripe.SetBackend("api", backend) // optional, useful for moc
// Setup
stripe.Key = "sk_key"
stripe.SetBackend("api", backend) // optional, useful for mocking
我是否应该传递一些特殊的东西作为后端
,以便stripe go知道然后调用哪个是stripe mock的地址
这是条带。收进
// 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 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
}
这是后端
// 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 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
}
从内部看,公开的后端配置
结构实现了后端
接口。因此,以下措施应该有效:
httpClient := &http.Client{Timeout: defaultHTTPTimeout}
mockBackend := &stripe.BackendConfiguration{
"api",
"http://localhost:12111",
httpClient,
}
stripe.SetBackend("api", mockBackend)