2

I'm new to Pact and new to groovy too.

I want to write a Pact to hand it to a provider.

The provider should answer on a given request with an array of strings like ["foo", "bar", "foobar"].

Here is my working state, which is stripped to the neccessary parts but still is executable:

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

    }
}

This test generates the following pact file:

{
  "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"
    }
  }
}

The Pact file expects the provider to always answer with ["foo", "bar", "foobar"]

What I want is a generic array of Strings.

When I try the following:

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

the generated Pact file looks like:

{
  "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"
    }
  }
}

There are matchingRules and this looks good. But there is an object on the root level and I need the array to be the root.

Is there a way to get the array as root element and the strings matched by type not by value?

J_A_X
  • 12,857
  • 1
  • 25
  • 31
Papillon
  • 31
  • 1
  • 4
  • This is a duplicate of another question asked yesterday: https://stackoverflow.com/questions/46523414/pact-groovy-mock-with-array-in-body I believe that this may be a bug. There's already an issue posted on github: https://github.com/DiUS/pact-jvm/issues/555 If you could please tack onto that conversation with your example, that would be very helpful. – J_A_X Oct 04 '17 at 22:39
  • I think it is another issue. In the linked question the problem is, that only 1 entry is created in the body. My Issue is to create a body like: ` "body": [ "foo", "bar", "foobar" ]` and a matcher like: `"matchingRules": { "body": { "$.key": { "matchers": [ { "match": "type" } ], "combine": "AND" } } }` but without the `.key` – Papillon Oct 05 '17 at 09:31
  • okay, the issue doesn't cover your matcher, but does cover the body array problem. Maybe create a different issue for the key matcher problem? – J_A_X Oct 05 '17 at 11:12
  • 2
    Thanks for your answer. I created an issue at github: https://github.com/DiUS/pact-jvm/issues/557 – Papillon Oct 05 '17 at 13:50

0 Answers0