Skip to content

Request Matchers

This section describes matcher functions that enable developers to implement custom matchers. These matchers execute user-defined code to determine if a request meets specific criteria.

Adds a custom matcher for expected HTTP requests. If this function returns true, the request is considered a match, and the mock server will respond to the request (given all other criteria are also met).

You can use this function to create custom expectations for your mock server based on any aspect of the HttpMockRequest object.

  • matcher: A function that takes a reference to an HttpMockRequest and returns a boolean indicating whether the request matches.
use httpmock::prelude::*;
// Arrange
let server = MockServer::start();
let m = server.mock(|when, then| {
when.is_true(|req: &HttpMockRequest| {
req.uri().path().contains("es")
});
then.status(200);
});
// Act: Send the HTTP request
let response = reqwest::blocking::get(server.url("/test")).unwrap();
// Assert
m.assert();
assert_eq!(response.status(), 200);

When: Returns the modified When object with the new custom matcher added to the expectations.

Adds a custom matcher for expected HTTP requests. If this function returns false, the request is considered a match, and the mock server will respond to the request (given all other criteria are also met).

You can use this function to create custom expectations for your mock server based on any aspect of the HttpMockRequest object.

  • matcher: A function that takes a reference to an HttpMockRequest and returns a boolean indicating whether the request matches.
use httpmock::prelude::*;
// Arrange
let server = MockServer::start();
let m = server.mock(|when, then| {
when.is_false(|req: &HttpMockRequest| {
req.uri().path().contains("es")
});
then.status(404);
});
// Act: Send the HTTP request
let response = reqwest::blocking::get(server.url("/test")).unwrap();
// Assert
m.assert();
assert_eq!(response.status(), 404);

When: Returns the modified When object with the new custom matcher added to the expectations.