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.
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.
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.
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.
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.
Found the solution. So we have three Json mapping files:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With