英文:
Contract test for a Java consumer and JavaScript provider
问题
我们有两个服务,其中一个是另一个的消费者。消费者使用Java编写,提供者使用JavaScript编写。
在消费者一侧,我们使用pact-jvm定义了一个消费者契约测试,并且能够生成一个契约。在这个契约中,响应被定义如下:
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": [
"test"
]
}
在我们的提供者一侧,我们现在尝试使用pact-js定义一个提供者契约测试,但是我们遇到了一个问题,即在pact-js中我们使用MessageProviderPact
来验证契约。但是在运行测试时,期望的响应体应该在一个类似这样的内容属性中:
Key: - is expected
+ is actual
Matching keys and values are not shown
-[
- "test"
-]
+{
+ "contents": {
+ "statusCode": 200,
+ "body": "[test]"
+ }
+}
用于pact验证的代码:
const p = new MessageProviderPact({
messageProviders: {
'': handler
},
provider: 'provider-service',
pactUrls: [
path.resolve(
process.cwd(),
'pacts',
'consumer-service-provider-service.json'
)
]
});
对于如何解决这个问题,有什么想法吗?在消费者一侧是否可能使用类似带有内容键的消息结构?或者在提供者一侧是否有其他解决方法?
英文:
We have 2 services where one is the consumer of the other. The consumer is written in Java and the provider is written in JavaScript.
On the consumer side we've defined a consumer-contract-test using pact-jvm and we're able to generate a contract. In this contract the response is defined like:
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": [
"test"
]
},
On our provider we are now trying to define a provider-contract-test using pact-js but we are hitting a problem where in pact-js we are using a MessageProviderPact
to verify the contract. But when running the test, the body is expected to be in a content attribute like this:
Key: - is expected
+ is actual
Matching keys and values are not shown
-[
- "test"
-]
+{
+ "contents": {
+ "statusCode": 200,
+ "body": "[test]"
+ }
+}
Code used for pact verification
const p = new MessageProviderPact({
messageProviders: {
'': handler
},
provider: 'provider-service',
pactUrls: [
path.resolve(
process.cwd(),
'pacts',
'consumer-service-provider-service.json'
)
]
});
Any idea's on how to solve this? Is it possible to use a similar message structure with content key on the consumer side? Or can we solve it any other way on the provider side
答案1
得分: 0
MessageProviderPact
用于消息队列类型的交互,而不是 HTTP 交互。我认为你需要使用标准的 Verifier
类(https://github.com/pact-foundation/pact-js/#provider-api-testing):
const { Verifier } = require('@pact-foundation/pact');
let opts = {
...
};
new Verifier(opts).verifyProvider().then(function () {
// 做一些操作
});
英文:
MessageProviderPact
is for message queue type interactions, not HTTP interactions. I think you need the standard Verifier
class (https://github.com/pact-foundation/pact-js/#provider-api-testing):
const { Verifier } = require('@pact-foundation/pact');
let opts = {
...
};
new Verifier(opts).verifyProvider().then(function () {
// do something
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论