Matchers
When configuring a Mock
, you can specify one or more matchers for incoming requests.
The Mock
is only triggered if the incoming request satisfies all the matchers attached to it.
Common matchers
The wiremock
crate provides an extensive collection of matchers out of the box.
Check out the documentation of the matchers
module for the full list.
Writing your own matchers
Occasionally, you'll need to write your own matchers, either because you need to match on a property that's not supported by the built-in matchers, or because you want to build a higher-level matcher out of existing ones.
To write a custom matcher, you need to implement
the Match
trait:
#![allow(unused)] fn main() { pub trait Match: Send + Sync { // Required method fn matches(&self, request: &Request) -> bool; } }
The trait is quite straight-forward. It has a single method, matches
, that takes a reference to the incoming Request
and returns a bool
: true
if the request matches, false
otherwise.
Exercise
The exercise for this section is located in 07_http_mocking/02_match