question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Request body JSON matcher (or custom matchers)

See original GitHub issue

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

  1. Have mockttp define such a matcher
  2. Define my own custom matcher on the MockRuleBuilder
  3. I could do something fancy in thenCallback, but this seems less clean

Are any of these possible?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
pimterrycommented, May 17, 2019

@hanvyj @drbr, it’s your lucky day: I had a little time spare this afternoon, so I’ve implemented withJsonBody (exact match) and withJsonBodyIncluding (fuzzy match).

See https://github.com/httptoolkit/mockttp/commit/d15316d9fae5a3765c017b02f299537935665a50 for full details. Now released as version 0.14.6, enjoy 😃.

0reactions
drbrcommented, May 15, 2019

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found