Wiremock URL matching logic

19.8k views Asked by At

I am trying to compare the abilities of Soap UI and Wiremock using the following requirement (which is realistic enough for most cases in my projects).

Goal is to create a mock for a currency price service. The requirements:

  • Accessible at

    mytesthost/priceservice/getprice

  • Expects one parameter called the 'cur' which defines the currenypair like: cur=EURHUF

  • When called as below should respond with an XML response saved in file EURHUF.xml.

    mytesthost/priceservice/getprice?cur=EURHUF

  • When called as below should respond with an XML response saved in file EURUSD.xml.

    mytesthost/priceservice/getprice?cur=EURUSD

  • When called with any other currencypair it should respond with an error response stored in NOCURR.xml

Implementing this in Soap UI boils down to preparing the result than implementing a few lines of Groovy code to select the response.

When approaching the problem with wiremock i can match for the two 'happpy' path cases but don't know how to achieve the fallback case (with NOCURR.xml).

Example on how i am doing the matching:

{
    "request": {
        "method": "GET",
        "url": "/priceservice/getprice?cur=EURUSD"
    },
    "response": {
        "status": 200,
        "bodyFileName": "EURUSD.xml"
    }
}

Can i achieve this with wiremock? I am mainly interested to do this via the Json configuration but if the Java API would be the way that is also fine.

1

There are 1 answers

0
dbalakirev On BEST ANSWER

Found the solution. So we have three Json mapping files:

  1. For EURUSD matching
  2. For CHFHUF matching
  3. For everything else - even non existing currency pairs

For the 1st and the 2nd the mapping is like this:

{
    "priority": 1,
    "request": {
        "method": "GET",
        "url": "/priceservice/getprice?cur=CHFHUF"
    },
    "response": {
        "status": 200,
        "bodyFileName": "CHFHUF.xml"
    }
}

Please note the priority=1!

Where as for the 'else' case we have:

{
    "priority": 2,
    "request": {
        "method": "GET",
        "urlPattern": "/priceservice/.*"
    },
    "response": {
        "status": 200,
        "bodyFileName": "NOCURR.xml"
    }
}

Not only this has a lower priority (2) also instead of 'url' i added 'userPattern' for regex matching.