Matching on multiple name/value pairs in request body

8,347 views
Skip to first unread message

Jason Schultz

unread,
Nov 16, 2016, 2:14:49 PM11/16/16
to wiremock-user
I've been using the "equalToJson" for bodypatterns matching, and in most cases, that did exactly what I want.  However, I ran into a case in which part of the incoming bodypattern contains unknown data.  In searching around, I didn't see a way to use wildcards with the equalToJson parameter, so I instead tried 3 different options that seems to work (sort of) for me:

"matchesJsonPath" : "[?(@.username == 'qapp-0923-ssomyl')]",
"matchesJsonPath" : "$.password"

"matches" : ".*username.*",
 "matches" : ".*password.*"

"contains": "username",
"contains": "password"

Of those 3 sets, the 1st set is the most desirable implementation that I'd like to use, but I have a common problem in all 3 cases.  The problem is that whichever parameter is listed last seems to be the only parameter that matters when it comes to wiremock considering the request to be a match.  For example, in the 3 sets above, I can change "username" to "XXX", but leave "password" as is in my request and wiremock still considers my request to be a match.  How do I link these parameters together so wiremock only considers a match to have occurred if ALL name/value pairs are present/correct?

Thanks,
Jason

Jason Schultz

unread,
Nov 16, 2016, 4:03:21 PM11/16/16
to wiremock-user
Finally figured it out.  If I use the format below I can get the desired behavior of wiremock only considering a match to have happened when ALL parameters are correct.  Essentially I put everything on one line as follows:

"matchesJsonPath" : "[?(@.username == 'qapp-0923-ssomyl')][?(@.password)]"

Hope that helps someone else in the future.

Tom Akehurst

unread,
Nov 17, 2016, 4:05:16 AM11/17/16
to wiremock-user
Glad you found a solution. Thanks for sharing.

Incidentally, did you know you can use equalToJson with array order and extra element invariance, meaning that you can specify a partial document and it'll still match?

Jason Schultz

unread,
Nov 17, 2016, 8:23:59 AM11/17/16
to wiremock-user
When I first started I was using "equalToJson" on the bodypatterns, but those array flags didn't seem to make a difference for what I was trying to accomplish. (Which was using wildcards for portions of the body that I didn't care about the value.)  Is there additional information/documentation for the matching API/parameters besides the matching page on the wiremock site?

Thanks!

Tom Akehurst

unread,
Nov 17, 2016, 9:30:36 AM11/17/16
to wiremock-user

Jason Schultz

unread,
Nov 17, 2016, 9:34:30 AM11/17/16
to wiremock-user
Will do.  Thanks again!

hilman_wiremockuser

unread,
May 26, 2020, 5:57:36 AM5/26/20
to wiremock-user
Dears.. I have a similar scenario where I have to match 2 different strings with contains match..like below

"contains" : "[foo][bar][space]".. It doesn't work..

What am i missing?

Thanks,
Senthil Manohar

Aaron Goff

unread,
May 26, 2020, 8:47:38 AM5/26/20
to wiremock-user
Can you provide exact examples of what the request body looks like, and your stub/mapping? FWIW I wouldn't expect "[foo][bar][space]" to work as I'm assuming the request body does not contain the exact string of "[foo][bar][space]".

The corresponding regex that I would use for your provided example would be "matches": ".*(foo|bar|space).*"

I would recommend reading the documentation around the difference between contains (substring equal) vs. matches (regex matching) - http://wiremock.org/docs/request-matching/

hilman_wiremockuser

unread,
Jun 10, 2020, 2:30:22 AM6/10/20
to wiremock-user

Hi Aaron,

please find below my stub mapping. The i need to match those 3 tracking numbers with AND condition and in a different scenario with OR condition as well. I tried 3 different "contains" operator for AND condition and it did not work (only the last condition is applied and everything else is ignored). For OR operator, i tried the "|" symbol with "contains" match, that didnt work either. 

{
    "TrackPackagesRequest": {
        "appType""WTRK",
        "appDeviceType""DESKTOP",
        "processingParameters": {},
        "supportCurrentLocation"true,
        "supportHTML"true,
        "trackingInfoList": [
            {
                "trackNumberInfo": {
                    "trackingNumber""794965936375",
                    "trackingQualifier""",
                    "trackingCarrier"""
                }
            },
            {
                "trackNumberInfo": {
                    "trackingNumber""794969478178",
                    "trackingQualifier""",
                    "trackingCarrier"""
                }
            },
            {
                "trackNumberInfo": {
                    "trackingNumber""794966848291",
                    "trackingQualifier""",
                    "trackingCarrier"""
                }
            }
        ],
        "uniqueKey"""
    }
}

Aaron Goff

unread,
Jun 10, 2020, 9:47:46 AM6/10/20
to wiremock-user
What's the mapping look like? 

I'm not 100% sure if WireMock supports matching for multiple bodyPatterns parts (maybe Tom can speak to this -- seems like OP in this thread thought that was the issue, but was able to have a workaround).

Your case seems pretty specific, and I might suggest writing a custom matcher.
Reply all
Reply to author
Forward
0 new messages