redmelon7896
redmelon7896

Reputation: 25

Pact: How to have dynamic UUID in pact request header?

I am trying to generate a pact contract for our provider, which is all fine. I get a problem when I try to run an integration test for it. I am using matchHeader in my code, to specify a regex for UUID. When I deploy the pact to our repository, a generated UUID is stored there. I have integration tests which use the stub url, and send a request built at runtime. The request generates a new UUID every time which is different from the one on our repository and the request is therefore not matched. How can I make it so that the consumer contract on our repository expects a regex instead of a hardcoded value?

Here is my pact definition

@Pact(consumer = "consumer", provider = "provider")
    fun getResponse(builder: PactDslWithProvider): RequestResponsePact {
        return builder
            .given("data exists")
            .uponReceiving("a request to receive data")
            .path(String.format("/path/%s", "1234"))
            .method("GET")
            .matchHeader(
                "UUID-header-name","[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}",
            )
            .willRespondWith()
            .status(200)
            .body(
                newJsonBody {
                    it.stringType("Data", "data")
                }.build(),
            ).toPact()
    }

And the generated pact file

{
  "consumer": {
    "name": "consumer"
  },
  "interactions": [
    {
      "description": "a request to receive data",
      "providerStates": [
        {
          "name": "data exists"
        }
      ],
      "request": {
        "headers": {
          "UUID-header-name": "ae4e41d7-1c0c-4b6c-8a34-d9e4def7d82a"
        },
        "matchingRules": {
          "header": {
            "UUID-header-name": {
              "combine": "AND",
              "matchers": [
                {
                  "match": "regex",
                  "regex": "[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}"
                }
              ]
            }
          }
        },
        "method": "GET",
        "path": "/path/1234"
      },
      "response": {
        "body": {
          "Data": "data"
        },
        "headers": {
          "Content-Type": "application/json; charset=UTF-8"
        },
        "matchingRules": {
          "body": {
            "$.Data": {
              "combine": "AND",
              "matchers": [
                {
                  "match": "type"
                }
              ]
            },
          "header": {
            "Content-Type": {
              "combine": "AND",
              "matchers": [
                {
                  "match": "regex",
                  "regex": "application/json(;\\s?charset=[\\w\\-]+)?"
                }
              ]
            }
          }
        },
        "status": 200
      }
    }
  ],
  "metadata": {
    "pact-jvm": {
      "version": "4.4.4"
    },
    "pactSpecification": {
      "version": "3.0.0"
    }
  },
  "provider": {
    "name": "provider"
  }
}

The data is edited because it is sensitive, so might be some issues, but the main point is that I have matching rules in the generated pact request header, but the stub url does not give me a response. I get

{
    "message": "No interaction found for GET /path/1234",
    "interaction_diffs": [
        {
            "description": "a request to receive data",
            "provider_state": "data exists",
            "headers": {
                "UUID-header-name": {
                    "EXPECTED": "610cea32-ccc3-42ae-a2ce-f829c0ca3e91",
                    "ACTUAL": "f3b269bb-d783-4450-a1ed-f949de4bdb93"
                }
            }
        }
    ]
}

I understand this is because pact generates a UUID according to the regex and adds it to the repository, but is there a way that I can modify it so that it accepts any valid UUID in the header and gives me a response when I use the stub url?

Upvotes: 0

Views: 94

Answers (0)

Related Questions