Hello, Is there a way to express that a response s...
# general
b
Hello, Is there a way to express that a response should contain a particular key, but that its value should be an opaque Json object? I appreciate that pact is not really designed for pass-through scenarios, but wondered if this could still be expressed.
m
I think so, which language? Basically, just expect the key and an empty JSON object. Any additional keys in the response will be ignored
i.e. Pact follows Postel’s law in this case
b
Thanks, Matt. That makes sense. The language is C++. I am struggling to express expecting an empty object but supplying a sample one when running the consumer test.
m
I assume something like this is not plausible (I’m not very familiar with the C++ syntax)
Copy code
auto provider = Pact("TodoAppCpp", "TodoServiceCpp");
  provider
    .given("i have a list of projects")
    .uponReceiving("a request for projects")
    .withRequest("GET", "/projects")
    .withQuery(query)
    .withHeaders(headers)
    .willRespondWith(200)
    .withResponseHeaders(res_headers)
    .withResponseJsonBody(Object({
      { "someKey", Object({}) }
    }));
b
Thanks, Mat. This does add
"someKey": {}
in the contract to the correct place in the interaction
response.body
and no matching rules. I assume then that during verification any payload received for
someKey
will be ignored (and the verification will pass), while a missing
someKey
will fail the verification. The thing that I am after is that I would also like to supply a sample payload, so that when we run the consumer test that generates the contract we can also exercise and verify the payload handling.
m
The behaviour you described is correct.
The thing that I am after is that I would also like to supply a sample payload, so that when we run the consumer test that generates the contract we can also exercise and verify the payload handling. (edited)
hmm whatever you put into the payload will then need to be there during verification (unless you post-process the pact file to remove it?)
b
That's what I thought. Sounds like what I am after would be a
json
matcher. Thanks for your help!