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?