GithubHelp home page GithubHelp logo

Body JSON matcher thoughts about wiremock HOT 20 CLOSED

Woodz avatar Woodz commented on July 1, 2024
Body JSON matcher thoughts

from wiremock.

Comments (20)

tomakehurst avatar tomakehurst commented on July 1, 2024

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.

Woodz avatar Woodz commented on July 1, 2024

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.

tomakehurst avatar tomakehurst commented on July 1, 2024

Thanks for the feedback. I'll take all this into consideration next time I have some time for new feature work.

from wiremock.

jz-infogroup avatar jz-infogroup commented on July 1, 2024

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.

tomakehurst avatar tomakehurst commented on July 1, 2024

A fix for this is in the pipeline.

from wiremock.

rajakm avatar rajakm commented on July 1, 2024

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.

tomakehurst avatar tomakehurst commented on July 1, 2024

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.

rajakm avatar rajakm commented on July 1, 2024

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.

tomakehurst avatar tomakehurst commented on July 1, 2024

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.

rajakm avatar rajakm commented on July 1, 2024

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.

rajakm avatar rajakm commented on July 1, 2024

Hi Tom,

It is working fine. as you said I have missed some JSON bits.
Thanks for your help.

Raja

from wiremock.

tomakehurst avatar tomakehurst commented on July 1, 2024

But did you also change the "equalToJson": "..." bit in the stub mapping?

from wiremock.

rajakm avatar rajakm commented on July 1, 2024

No, for 'equalToJson' what ever you suggested is correct. I did some mistake in the request body.

from wiremock.

tomakehurst avatar tomakehurst commented on July 1, 2024

Glad you managed to get it working

from wiremock.

rajakm avatar rajakm commented on July 1, 2024

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.
image

The same way, I need to verify if the email Address is NOT empty.

Kindly help me.

Thanks,
Raj

from wiremock.

tomakehurst avatar tomakehurst commented on July 1, 2024

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.

rajakm avatar rajakm commented on July 1, 2024

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.

tomakehurst avatar tomakehurst commented on July 1, 2024

Yes, any Java regex will work.

Have you considered the JSON path option?

from wiremock.

rajakm avatar rajakm commented on July 1, 2024

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.
image

And it works perfectly fine.

Thanks,
Raj

from wiremock.

tomakehurst avatar tomakehurst commented on July 1, 2024

Ah, excellent.

from wiremock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.