Request body JSON matcher (or custom matchers)
See original GitHub issueI want to create a mock that matches a request only if it has certain things in its JSON body (e.g. _.isMatch(request, partialContents)
). The recently-added regex body matcher lets me match a single simple field in the body, but I want to match more complicated structures.
I can see three potential ways to be able to do this.
- Have mockttp define such a matcher
- Define my own custom matcher on the MockRuleBuilder
- I could do something fancy in
thenCallback
, but this seems less clean
Are any of these possible?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Request Matchers - MockServer
JSON Object Body Matchers "STRICT" match type matches all fields and the order of arrays. In "STRICT" match type extra fields will cause...
Read more >Matching JSON requests - WireMock
When stubbing API functions that accept JSON request bodies we may want to return different responses based on the JSON sent. WireMock provides...
Read more >Matching JSON request body based on JSON Path with ...
If your amnt value is a string, you may have to write a custom matcher that can parse the body and convert the...
Read more >How to Use Custom RSpec Matchers to Specify Behaviour
Learn how to use custom RSpec matchers to write better, less repetitive tests. ... Example: Matching JSON Documents to JsonPath Expressions.
Read more >Helper Wizards for Request Matching and Dynamic Response ...
If a user sends a request that matches your request condition, a custom... ... is automatically taken from the Equals XML or Equals...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@hanvyj @drbr, it’s your lucky day: I had a little time spare this afternoon, so I’ve implemented
withJsonBody
(exact match) andwithJsonBodyIncluding
(fuzzy match).See https://github.com/httptoolkit/mockttp/commit/d15316d9fae5a3765c017b02f299537935665a50 for full details. Now released as version 0.14.6, enjoy 😃.
@hanvyj Yep, that’s essentially the code I ended up writing. It’s working like a charm 😃
So, no need for the json or predicate matcher, but if it were available, I might eventually rewrite my code to use it.