Matchers¶
Matchers determine how recorded interactions are matched to incoming requests during playback.
Overview¶
By default, grpcvcr matches requests by method name only. You can customize matching behavior using different matchers or combining them.
from grpcvcr import MethodMatcher, RequestMatcher
# Match by method name and request body
matcher = MethodMatcher() & RequestMatcher()
Built-in Matchers¶
| Matcher | Description |
|---|---|
MethodMatcher |
Match by RPC method name |
RequestMatcher |
Match by request body |
MetadataMatcher |
Match by request metadata |
AllMatcher |
Combine multiple matchers |
CustomMatcher |
Custom matching function |
API Reference¶
Bases: ABC
Base class for request matchers.
Matchers determine whether an incoming request matches a recorded
interaction. They can be combined using the & operator.
Example
matches
abstractmethod
¶
Check if a request matches a recorded interaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
InteractionRequest
|
The incoming request to match. |
required |
recorded
|
InteractionRequest
|
The recorded request to compare against. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the requests match, False otherwise. |
Source code in src/grpcvcr/matchers.py
__and__ ¶
__and__(other: Matcher) -> AllMatcher
Combine this matcher with another using AND logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Matcher
|
Another matcher to combine with. |
required |
Returns:
| Type | Description |
|---|---|
AllMatcher
|
An AllMatcher that requires both matchers to succeed. |
Source code in src/grpcvcr/matchers.py
Bases: Matcher
Matches requests by metadata (headers).
Can be configured to match specific keys only, or to ignore certain keys (like timestamps or request IDs).
Example
keys
class-attribute
instance-attribute
¶
If set, only these metadata keys are compared.
ignore_keys
class-attribute
instance-attribute
¶
If set, these metadata keys are ignored during comparison.
matches ¶
Check if metadata matches according to configured rules.
Source code in src/grpcvcr/matchers.py
Bases: Matcher
Combines multiple matchers with AND logic.
All contained matchers must return True for the request to match.
Usually created implicitly via the & operator.
Example
Bases: Matcher
Matches requests using a custom function.
Useful for complex matching logic like comparing specific protobuf fields or implementing fuzzy matching.
Example
def match_user_id(req: InteractionRequest, rec: InteractionRequest) -> bool:
# Deserialize and compare specific fields
req_msg = UserRequest.FromString(req.get_body_bytes())
rec_msg = UserRequest.FromString(rec.get_body_bytes())
return req_msg.user_id == rec_msg.user_id
matcher = MethodMatcher() & CustomMatcher(func=match_user_id)