Trying to mock an API endpoint that allows a request with 2 possible payloads, but the same response:
{
"key1": "value1"
}
{
"key2": "value2"
}
Based on the Request Templating documentation, I see that there's an option to define some regex for matchesJsonPath
.
However, I'm unable to figure out how to provide a configuration that will allow key1
or key2
.
This is what I'd tried, but it doesn't seem to work:
{
// ... other configs
"request": {
"bodyPatterns": [
{
"matchesJsonPath": "$.(key1|key2)"
}
]
}
}
Is it possible to provide 1 definition that supports both payloads, or do I have to create 2 stubs?
Note: I am using a standalone Wiremock Docker image, so options for more complex handling using Java are limited.
Your JsonPath matcher is formatted incorrectly. You need to apply a filter/script (denoted by ?()
). More information about how JsonPath matchers work can be found here.
Here is what the properly formatted JsonPath matcher could look like:
{
"matchesJsonPath": "$[?(@.key1 || @.key2)]"
}
If you need the key1
and key2
to have specific values, that would look like:
{
"matchesJsonPath": "$[?(@.key1 == 'value1' || @.key2 == 'value2')]"
}