Search code examples
spring-cloud-contract

Negate a url pattern in Spring Cloud contract


I have a requirement where I need to handle invalid requests. I want to negate a url pattern in contract request, which is a groovy file. e.g.:

url value(consumer(regex('/employee/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')))

Here, I want to handle all request which doesn't match the uuid regex pattern. Is there any way by which I can do this?


Solution

  • Instead of hacking this around create 2 contracts.

    • first contract will be for that regex with priority 1
    • second will be for url with regex matching any url with priority 10

    the higher value of priority the smaller the priority. That means that if there are 2 matching contracts for that URL, the first stub will be picked.