Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wiremock URL matching logic

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.

like image 342
dbalakirev Avatar asked Nov 12 '14 16:11

dbalakirev


People also ask

How to match just the path of the URL in wiremock?

In WireMock, the URLs can be matched either by equality or by regular expression. And you can match just the path of the URL or the path and query together. Let’s see both scenarios with examples. { "request": { "urlPath": "provide your url here" ... }, ... } Now let’s demonstrate this with the help of Postman.

What attributes does wiremock support for matching requests?

WireMock supports matching of requests to stubs and verification queries using the following attributes: URL HTTP Method Query parameters Headers Basic authentication (a special case of header matching) Cookies Request body Multipart/form-data Here’s an example showing all attributes being matched using WireMock’s in-built match operators.

How does request matching work with JSON mappings in wiremock?

One of the most important features of WireMock is Request Matching. WireMock supports matching of requests to stubs and verification queries using the following attributes: URL, HTTP Method, Query parameters, Headers, Cookies, Request body, etc. So in this article, we are going to see how Request Matching works with JSON Mappings in WireMock.

How do I change the regex match type in wiremock?

By default WireMock uses an equality check, but this can be changed in the Advanced section. Choosing the the Path regex match type can be particularly useful in cases where the API you’re mocking uses path parameters and you wish to provide a meaningful response to a specific URL pattern regardless of the specific parameter values.


1 Answers

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.

like image 195
dbalakirev Avatar answered Sep 16 '22 13:09

dbalakirev