pact-foundation / pact-jvm

JVM version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
https://docs.pact.io
Apache License 2.0
1.08k stars 478 forks source link

Matching provider's headers #437

Open metaflow opened 7 years ago

metaflow commented 7 years ago

I am trying to create a pact when provider is free to specify any content-type but have to provide one.

.consumer("wget")
.hasPactWith("server")
.given("file exist on server")
.uponReceiving("get file")
.method("GET")
.matchPath("/.*", "/{file-path}")
.willRespondWith()
.status(200)
.matchHeader("Content-Type", ".*", "application/pdf")  // <-----------

part of generated json:

...
"response": {
  "status": 200,
  "headers": {
     "Content-Type": "application/pdf"
  },
  "matchingRules": {
    "$.headers.Content-Type": {
      "regex": ".*"
    }}},

I am running pact-jvm-provider-junit test with http target.

Test passes but comment on match is

      includes headers
        "Content-Type" with value "application/pdf" (OK)

while it is not true and it's better to indicate that it matched .*. But that is not a real issue.


Now, if I add body matcher like .body(PactDslJsonRootValue.stringMatcher(".*", "any")) then several things broke:

  1. in json it states "body": "\"any\"", (it's probably similar to #385)
  2. header stops matching and fails with Expected a response type of 'application/pdf' but the actual type was 'application/xml' (server responded with xml all that time).
uglyog commented 7 years ago

The Content-Type header is a bit special in that it is used to determine what matching implementation to apply. So changing it has the side effect that you change the matching. The DSLs (like .body(PactDslJsonRootValue.stringMatcher(".*", "any"))) work with specific content types and change the expected header, so the matcher you setup is probably being removed.

uglyog commented 7 years ago

I also agree that the comment on match should reflect that a matcher was used, instead of the example value.

github-actions[bot] commented 1 year ago

👋 Thanks, this ticket has been added to the PactFlow team's backlog as PACT-615