Wiremock URL matching logic

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

The goal is to create a layout for the currency pricing service. Requirements:

  • Available in

    mytesthost / priceservice / getprice

  • Expects one parameter called "cur" which defines currenypair for example: cur = EURHUF

  • When invoked as shown below, it should respond to the XML response stored in the EURHUF.xml file.

    mytesthost / priceservice / getprice? Current = EURHUF

  • When invoked as shown below, it should respond to the XML response stored in the EURUSD.xml file.

    mytesthost / priceservice / getprice? Current = EURUSD

  • When called using any other currency pair, it should respond with an error response stored in NOCURR.xml

Implementing this in a soap UI boils down to preparing the result, not implementing a few lines of Groovy code to select an answer.

When approaching the wiremock problem, I can match the two happy path cases, but I don't know how to achieve the fallback case (using NOCURR.xml).

An example of how I am doing the mapping:

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

      

Can I achieve this with wiremock? I'm mostly interested in doing this with Json config, but if the Java API is as good as doing it.

+3


source to share


1 answer


Found a solution. So, we have three Json mapping files:

  • To match EURUSD
  • For CHFHUF compliance
  • For everything else - not even existing currency pairs

For 1st and 2nd display the following:

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

      



Pay attention to priority = 1!

Where for the "else" case we have:

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

      

Not only does this have a lower priority (2), but instead of "url" I added "userPattern" to match regexes.

+6


source







All Articles