wiremock request matching with comparison between two query parameters

2.9k Views Asked by At

I am using wiremock (to mock our Rest API) for selenium tests. I have a rest end point like http://localhost:8080/compare-date?from=2018-03-01&to=2018-03-10

For this, we are trying to create some intelligent mock using Java, which will be invoked only when to date is less than the from date. Ex: http://localhost:8080/compare-date?from=2018-03-01&to=2018-02-10

But, i am unable to achieve this. Kindly suggest how this can be done. Thanks in advance.

Thanks, Dharani

3

There are 3 best solutions below

0
On

At the moment your best bet would be to create a custom RequestMatcher implementation that can determine whether a date falls between the query parameters.

0
On

Ideally you should already expect the response as part of your test. That being said, if you want this functionality from your stub, then you will have to extend it.

There are a number of ways you can extend WireMock, most of them are found on this page: http://wiremock.org/docs/extending-wiremock/.

Honestly, I would probably use specific years for particular response and create rules for them.

0
On

This is now possible with LinqMatcher from the C# .NET Version from WireMock.Net.

Example JSON to match your request can be:

{
    "Request": {
        "Path": {
            "Matchers": [
                {
                    "Name": "WildcardMatcher",
                    "Pattern": "/linq"
                }
            ]
        },
        "Params": [
            {
                "Name": "from",
                "Matchers": [
                    {
                        "Name": "LinqMatcher",
                        "Pattern": "DateTime.Parse(it) > \"2018-03-01 00:00:00\""
                    }
                ]
            }
        ],
        "Body": {}
    },
    "Response": {
        "Body": "linq match !!!"
    }
}