合约测试用于Java消费者和JavaScript提供者。

huangapple go评论103阅读模式
英文:

Contract test for a Java consumer and JavaScript provider

问题

我们有两个服务,其中一个是另一个的消费者。消费者使用Java编写,提供者使用JavaScript编写。

在消费者一侧,我们使用pact-jvm定义了一个消费者契约测试,并且能够生成一个契约。在这个契约中,响应被定义如下:

  1. "response": {
  2. "status": 200,
  3. "headers": {
  4. "Content-Type": "application/json"
  5. },
  6. "body": [
  7. "test"
  8. ]
  9. }

在我们的提供者一侧,我们现在尝试使用pact-js定义一个提供者契约测试,但是我们遇到了一个问题,即在pact-js中我们使用MessageProviderPact来验证契约。但是在运行测试时,期望的响应体应该在一个类似这样的内容属性中:

  1. Key: - is expected
  2. + is actual
  3. Matching keys and values are not shown
  4. -[
  5. - "test"
  6. -]
  7. +{
  8. + "contents": {
  9. + "statusCode": 200,
  10. + "body": "[test]"
  11. + }
  12. +}

用于pact验证的代码:

  1. const p = new MessageProviderPact({
  2. messageProviders: {
  3. '': handler
  4. },
  5. provider: 'provider-service',
  6. pactUrls: [
  7. path.resolve(
  8. process.cwd(),
  9. 'pacts',
  10. 'consumer-service-provider-service.json'
  11. )
  12. ]
  13. });

对于如何解决这个问题,有什么想法吗?在消费者一侧是否可能使用类似带有内容键的消息结构?或者在提供者一侧是否有其他解决方法?

英文:

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:

  1. "response": {
  2. "status": 200,
  3. "headers": {
  4. "Content-Type": "application/json"
  5. },
  6. "body": [
  7. "test"
  8. ]
  9. },

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:

  1. Key: - is expected
  2. + is actual
  3. Matching keys and values are not shown
  4. -[
  5. - "test"
  6. -]
  7. +{
  8. + "contents": {
  9. + "statusCode": 200,
  10. + "body": "[test]"
  11. + }
  12. +}

Code used for pact verification

  1. const p = new MessageProviderPact({
  2. messageProviders: {
  3. '': handler
  4. },
  5. provider: 'provider-service',
  6. pactUrls: [
  7. path.resolve(
  8. process.cwd(),
  9. 'pacts',
  10. 'consumer-service-provider-service.json'
  11. )
  12. ]
  13. });

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):

  1. const { Verifier } = require('@pact-foundation/pact');
  2. let opts = {
  3. ...
  4. };
  5. new Verifier(opts).verifyProvider().then(function () {
  6. // 做一些操作
  7. });
英文:

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):

  1. const { Verifier } = require('@pact-foundation/pact');
  2. let opts = {
  3. ...
  4. };
  5. new Verifier(opts).verifyProvider().then(function () {
  6. // do something
  7. });

huangapple
  • 本文由 发表于 2020年9月1日 14:53:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63682740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定