如何生成一个Pact文件,其中一个响应体与一个字符串数组匹配,并且具有常规

时间:2017-10-04 13:48:27

标签: arrays groovy pact pact-jvm

我是Pact的新手,也是groovy的新人。

我想写一个Pact将它交给提供者。

提供商应针对给定的请求回答一串字符串,如 ["foo", "bar", "foobar"]

这是我的工作状态,它被剥离到必要的部分,但仍然是可执行的:

import au.com.dius.pact.consumer.groovy.PactBuilder
import groovyx.net.http.RESTClient
import spock.lang.Specification

class MyPact extends Specification {
    def "some test"() {
        given:
        when:

        def pactBuilder = new PactBuilder()
        pactBuilder {
            serviceConsumer "consumer"
            hasPactWith "provider"
            port 1234
            given('some provider state')
            uponReceiving('a request')
            withAttributes(method: 'get', path: '/my/endpoint')
            willRespondWith(status: 200)
            withBody(["foo", "bar", "foobar"])
        }

        pactBuilder.run() { mockServer ->
            RESTClient('http://localhost:1234/').get(path: '/my/endpoint')
        }

        then: 1 == 1

    }
}

此测试生成以下pact文件:

{
  "provider": {
    "name": "provider"
  },
  "consumer": {
    "name": "consumer"
  },
  "interactions": [
    {
      "description": "a request",
      "request": {
        "method": "GET",
        "path": "/my/endpoint"
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json"
        },
        "body": [
          "foo",
          "bar",
          "foobar"
        ]
      },
      "providerStates": [
        {
          "name": "some provider state"
        }
      ]
    }
  ],
  "metadata": {
    "pact-specification": {
      "version": "3.0.0"
    },
    "pact-jvm": {
      "version": "3.5.6"
    }
  }
}

Pact文件希望提供者始终使用["foo", "bar", "foobar"]

回答

我想要的是一个通用的字符串数组。

当我尝试以下操作时:

withBody {
    key eachLike (3, "foo")
}

生成的Pact文件如下所示:

{
  "provider": {
    "name": "provider"
  },
  "consumer": {
    "name": "consumer"
  },
  "interactions": [
    {
      "description": "a request",
      "request": {
        "method": "GET",
        "path": "/my/endpoint"
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json"
        },
        "body": {
          "key": [
            "foo",
            "foo",
            "foo"
          ]
        },
        "matchingRules": {
          "body": {
            "$.key": {
              "matchers": [
                {
                  "match": "type"
                }
              ],
              "combine": "AND"
            }
          }
        }
      },
      "providerStates": [
        {
          "name": "some provider state"
        }
      ]
    }
  ],
  "metadata": {
    "pact-specification": {
      "version": "3.0.0"
    },
    "pact-jvm": {
      "version": "3.5.6"
    }
  }
}

有匹配规则,这看起来不错。 但是根级别上有一个对象,我需要将数组作为根。

有没有办法让数组作为根元素,而字符串匹配的类型不是值?

0 个答案:

没有答案