Pact-jvm: Matching Rules ignored on header

Created on 22 Oct 2018  路  4Comments  路  Source: pact-foundation/pact-jvm

Hey,

I've received what looks to me like a valid pact.json file.
It's supposed to be a simple check of a response and a header. The header specifies a matching rule to allow upper and lower case for a part of the response. When I run the test against my provider, however, it always fails because the the header ignores the matching rule and seems to fall back to pure string comparison.

Here is the relevant part of the pact file:

{
    "provider": {
        "name": "ProviderName"
    },
    "consumer": {
        "name": "ConsumerName"
    },
    "interactions": [
        {
            "description": "sending a ping request",
            "request": {
                "method": "GET",
                "path": "/v1/monitoring/ping"
            },
            "response": {
                "status": 200,
                "headers": {
                    "Content-Type": "application/json; charset=UTF-8"
                },
                "body": {
                    "response": "pong"
                },
                "matchingRules": {
                    "body": {

                    },
                    "header": {
                        "Content-Type": {
                            "match": [
                                {
                                    "match": "regex",
                                    "regex": "application/json;\\s?charset=(utf|UTF)-8"
                                }
                            ],
                            "combine": "AND"
                        }
                    }
                }
            }
        }
    ],
    "metadata": {
        "pactSpecification": {
            "version": "3.0.0"
        },
        "pact-jvm": {
            "version": "3.5.19"
        }
    }
}

I use the grade plugin to run the verification. (Note: Version is 3.5.19 because that is the version of the pact file I have. Upgrading to 3.6 did not fix the issue). Here is the build file:

plugins {
  id "au.com.dius.pact" version "3.5.19"
}
pact {
    serviceProviders {
        SKPortal {
            protocol = 'https'
            host = 'localhost'
            port = 6001
            path = '/'

            // Monitoring-Controller Tests
            hasPactWith('Monitoring-Controller') {
                pactSource = file("$rootDir/Pacts/testMoni.json")
            }
        }
    }
}

This is the output I get:

Gradle Distribution: Gradle wrapper from target build
Gradle Version: 4.10.2
Java Home: C:Program Files (x86)Javajre1.8.0_191
JVM Arguments: None
Program Arguments: None
Build Scans Enabled: false
Offline Mode Enabled: false
Gradle Tasks: pactVerify

Task :pactVerify_SKPortal FAILED

Verifying a pact between Monitoring-Controller and SKPortal
[Using File C:DVPJavaWorkspaceSKPortalPactTestsPactstestMoni.json]
sending a ping request
returns a response which
has status code 200 (OK)
includes headers
"Content-Type" with value "application/json; charset=UTF-8" (FAILED)
has a matching body (OK)
Skipping publishing of verification results as it has been disabled (pact.verifier.publishResults is not 'true')

Failures:

0) Verifying a pact between Monitoring-Controller and SKPortal - sending a ping request returns a response which includes headers "Content-Type" with value "application/json; charset=UTF-8"
Expected header 'Content-Type' to have value 'application/json; charset=UTF-8' but was 'application/json; charset=utf-8'

FAILURE: Build failed with an exception.

  • What went wrong:
    There were 1 pact failures for provider SKPortal

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 0s
1 actionable task: 1 executed

Honestly I'm not sure if this is a bug or if I'm doing something wrong or if the pact file is invalid.
This is the first time I'm working with a pact file so I don't have much experience to go on.

Any help is appreciated!

bug

All 4 comments

Your setup looks ok, so it must be a defect. I'll investigate further to see if I can work out what is going on.

I think the issue is the header was being sent in lower case, so was not matching the key in the matching rules map. I have put a fix in for that.

I have also corrected the content type header comparison to handle case. You shouldn't need the regex matcher now.

Released 3.5.24 with this fix

Thanks so much for having a look at it!
Seems to be working fine now.

Was this page helpful?
0 / 5 - 0 ratings