英文:
How to return a bad request (400, 500) with Pact in Go?
问题
问题:我们如何将错误请求响应作为ProviderStateV3Response
返回,因为它是一个映射接口?
回答:要返回错误请求响应作为ProviderStateV3Response
,你可以创建一个包含错误信息的ProviderStateV3Response
对象,并将其返回。以下是一个示例代码:
"A product with id 2 doesn't exists": func(setup bool, s ProviderStateV3) (ProviderStateV3Response, error) {
response := ProviderStateV3Response{
"status": 400,
"body": "Bad Request",
}
return response, nil
},
在上面的示例中,我们创建了一个包含状态码和响应体的ProviderStateV3Response
对象,并将其作为响应返回。你可以根据需要自定义错误信息。
英文:
I'm working in the adoption of Pact in my company, but on Golang, we hit a hurdle on the basic case where a consumer as 2 states for one endpoint:
- Given("A product with id 1 exists").
- Given("A product with id 2 doesn't exists").
Our trouble is on the doesn't exists case.
Consumer
mockProvider.AddInteraction().
Given("The product with ID 66 doesn't exists").
UponReceiving("a request Product 66").
WithRequest(http.MethodGet, S("/api/v1/product/66")).
WillRespondWith(http.StatusNotFound).
Provider
func TestContract(t *testing.T) {
SetLogLevel("TRACE")
verifier := HTTPVerifier{}
err := verifier.VerifyProvider(t, VerifyRequest{
ProviderBaseURL: "http://localhost:8080",
Provider: "ms.pact-provider-example-for-go",
ProviderVersion: "example", // os.Getenv("APP_SHA"),
BrokerURL: "https://…", // os.Getenv("PACT_BROKER_BASE_URL"),
PublishVerificationResults: false,
StateHandlers: StateHandlers{
"A product with id 1 exists": func(setup bool, s ProviderStateV3) (ProviderStateV3Response, error) {
…
return response, nil
},
"A product with id 2 doesn't exists": func(setup bool, s ProviderStateV3) (ProviderStateV3Response, error) {
// ???
},
},
})
require.NoError(t, err)
}
Question
How can we return a bad request reponse as ProviderStateV3Response
is a map interface?
答案1
得分: 2
StateHandlers
的存在并不是为了直接改变响应(这可能会影响测试的有效性),而是为了修改当前测试中提供程序的内部状态。它们使用状态名称(以及可选的参数)来确定应该配置哪个状态。
当测试执行时,提供程序应该使用该状态执行其通常的代码,并相应地做出响应。
StateHandlers: StateHandlers{
"A product with id 1 exists": func(setup bool, s ProviderStateV3) (ProviderStateV3Response, error) {
// 修改提供程序的内部状态,使得数据库中存在ID为1的产品
return response, nil
},
"A product with id 2 doesn't exist": func(setup bool, s ProviderStateV3) (ProviderStateV3Response, error) {
// 修改提供程序的内部状态,使得数据库中不存在ID为2的产品
},
},
在存储库中有一些示例,例如 https://github.com/pact-foundation/pact-go/blob/master/examples/mux/provider/user_service_test.go#L94-L120。
状态是抽象的,它并不意味着状态是如何配置的。可以通过多种方式实现状态转换,例如更新数据库或配置存根等。
英文:
StateHandlers
don't exist to change the response directly (that could impact the validity of the test), they exist to modify the internal state of the provider for the current test. The use the state name (and optionally, parameters) to determine what state should be configured.
When the test executes, the provider should execute its usual code with that state in place, and respond accordingly.
StateHandlers: StateHandlers{
"A product with id 1 exists": func(setup bool, s ProviderStateV3) (ProviderStateV3Response, error) {
// modify internal state of the provider, so that product with ID 1 exists in the database
return response, nil
},
"A product with id 2 doesn't exists": func(setup bool, s ProviderStateV3) (ProviderStateV3Response, error) {
// modify internal state of the provider, so that product with ID 2 does not exist in the database
},
},
There are examples in the repository, such as https://github.com/pact-foundation/pact-go/blob/master/examples/mux/provider/user_service_test.go#L94-L120.
States are abstract - it doesn't imply how the state is configured. It could achieve the state transition in multiple ways such as updating a database or configuring a stub etc.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论