Comments (20)
Hi,
If all you wanted to do was verify the right request was sent (i.e. you didn't need to be that specific in the stub mapping) you could pull out the relevant request via WireMock.findAll(...)
then use JSONAsset.assertJsonEquals()
from json-lib.
I'd definitely consider a patch that incorporated something along those lines to support stub matching. Perhaps (as with the JSON path matching strategy) by adding another body matching method to the DSL e.g. withRequestBody(equalToJson("{ ... }")
.
To your point about writing a custom ValuePattern, I agree the fact that commands are sent over the wire is a bit of a limitation. This is a tradeoff I decided to make to allow WireMock to be run in a local or distributed fashion fairly transparently. I recently did some refactoring which paves the way for being able to issue commands directly when running everything locally, so this might open up some options for an extension point around matching. I quite like the idea of using Hamcrest as a basis for this.
from wiremock.
Thank you for your recommendation about the findAll method. I will experiment with this to see its limitations. If this is insufficient I will patch it to include an equalToJson method, as I think this would be a very useful feature to have for testing JSON REST APIs.
I agree that Hamcrest matchers would make for very readable tests and hopefully decrease the amount of work you have to do in terms of matching criteria.
Cheers.
from wiremock.
Thanks for the feedback. I'll take all this into consideration next time I have some time for new feature work.
from wiremock.
We use a standalone WireMock server to do both recording and playback. Our REST requests have the same URL but different JSON body. Mapping files are generated automatically without my writing any single stubFor code. When WireMock is in the playback mode, it can't pick up the right recorded files for each test. I guess it is because it can't correctly match the full JSON body content. That's really no good. The auto-generated mapping files contain "body" rather than "bodyPatterns".
from wiremock.
A fix for this is in the pipeline.
from wiremock.
Hi Woodz/Tom,
I am using wiremock as my MockServer and PostMan as my REST client to send a request to Mock Server. In mappings file, I am trying to use 'equalToJson' method to verify the JSON variable.
For example, I am sending the below request from my REST client to Mock Server (Wire Mock)
{
"Orders":[
"customer":{
"firstName":"Law",
"lastName":"Ad",
"emailAddress":"[email protected]",
"phone":"321"
}
]
}
In mapping file, I would like to verify the field 'emailAddress' is not empty. I have tried with equalToJson method in bodyPatterns. But I am unable to access the nested variable 'emailAddress'.
I have the following content in my mapping file.
{
"request":{
"method":"POST",
"url":"/shops/extShopId/orders",
"bodyPatterns" : [
{ "equalToJson" : "{ "Orders.customer.emailAddress": "test" }", "jsonCompareMode": "LENIENT" }
]
},
"response":{
"status":200,
"bodyFileName":"shops/extShopId/orders/orderSuccess",
"headers":{
"Content-Type":"application/json"
}
}
}
Kindly help me, how would I access the nested JSON variable in the mapping file?
Thanks and Regards,
Raja
from wiremock.
It looks like you're trying to use a JSON path with the JSON equality feature. You probably want something like this:
{ "equalToJson" : "{ \"Orders\": [{\"customer\": {\"firstName\": \"Law\", \"lastName\": \"Ad\", \"emailAddress\": \"[email protected]\", \"phone\": \"321\" }}]}",
"jsonCompareMode": "LENIENT" }
from wiremock.
Hi Tom,
Thank you for your response. I have tried your code. It is partially working. Now I could able to access the nested JSON object and verify using 'equalToJson' method only when there is no array in the JSON request.
For example:
if the REST request is
{
"Orders":{
"customer":{
"firstName":"Law",
"lastName":"Ad",
"emailAddress":"[email protected]",
"phone":"321"
}
}
}
Then the following JSON equality method is working fine.
{ "equalToJson" : "{ "Orders": {"customer": {"firstName": "Law", "lastName": "Ad", "emailAddress": "[email protected]", "phone": "321" }}}",
"jsonCompareMode": "LENIENT" }
But I would like to access the nested JSON object which has arrays as well like in my previous post.
Kindly help me on this.
from wiremock.
Not sure I totally understand. You're saying if you change both the stub mapping and your request's body to a JSON doc containing an array, it stops working?
If that's the case, I'd suggest double-checking they're both identical. The JSON within a JSON attribute can make it easy to miss bits.
from wiremock.
Hi Tom,
Yes if the request body contains array (like below), then the 'equalToJson' is not identifying the JSON object.
{
"Orders":[
"customer":{
"firstName":"Law",
"lastName":"Ad",
"emailAddress":"[email protected]",
"phone":"321"
}
]
}
from wiremock.
Hi Tom,
It is working fine. as you said I have missed some JSON bits.
Thanks for your help.
Raja
from wiremock.
But did you also change the "equalToJson": "..." bit in the stub mapping?
from wiremock.
No, for 'equalToJson' what ever you suggested is correct. I did some mistake in the request body.
from wiremock.
Glad you managed to get it working
from wiremock.
Hi Tom,
Its me Raj again. Is it possible to use regex in 'equalToJson' method?
From the below REST request, I would like to verify whether the emailAddress is NOT NULL.
{
"Orders":[
{
"customer":{
"firstName":"Law",
"lastName":"Ad",
"emailAddress":"[email protected]",
"phone":"321"
}
}
]
}
To Verify if the email address field is empty, I am using the below in the mappings file.
The same way, I need to verify if the email Address is NOT empty.
Kindly help me.
Thanks,
Raj
from wiremock.
Unfortunately it isn't, the reason being that the it's doing a semantic comparison of the two JSON documents rather than comparing strings.
You could use the "matching" operator instead. This would make your test a little more brittle as whitespace would start to matter, but on the other hand you could just match on the field you care about e.g.
".*\"address\":\".*\".*"
from wiremock.
Thank you for your response Tom!
I could not understand clearly. You mean, use 'matches' method in bodyPatterns?
If I use like below
"bodyPatterns" : [
{ "matches" : "."emailAddress":".".*" }
]
I couldnt achieve what I want.
I have two different mappings file. One for, if the emailAddress is empty.
And another mapping file for, if the emailAddress is not empty.
Is it possible to use any regular expression with Matches method?
Thanks,
Raj
from wiremock.
Yes, any Java regex will work.
Have you considered the JSON path option?
from wiremock.
Hi Tom,
I could able to do it via matches operator itself.
I have used the below regex in mappings file to match the non empty string.
And it works perfectly fine.
Thanks,
Raj
from wiremock.
Ah, excellent.
from wiremock.
Related Issues (20)
- Expose args in Options so extensions can read it
- Possible memory leak ? HOT 15
- Prevent creating multiple stub mappings with the same ID HOT 1
- Support virtual threads in Jetty server for standalone deploy
- Error when proxying to a host that is only accessible via IPv6 HOT 1
- Formatted dates not in UTC
- __files created by default since v3.6.0 HOT 1
- absent operand must be a non-null string
- Array and data manipulation in handlebars template when using docker
- FileItemPartAdapter returning multipart's field name instead of a filename
- Avoiding reading zipfile entries repeatedly in ClasspathFileSource
- Support disabled state for stub mappings HOT 1
- Add a unique id to the template request model
- Add self-description callback(s) to MatchResult for use in Diff
- Report webhook errors and results as sub-events
- Support mTLS (client certificate) for standalone WireMock client
- urlPathTemplate does not work in combination with queryParameters HOT 1
- Allow loading a stub for a single file instead of a directory
- Make checking for duplicate IDs optional
- No command line option to set template escape disabled to false
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wiremock.