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.
is_true
Section titled “is_true”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.
Parameters
Section titled “Parameters”matcher: A function that takes a reference to anHttpMockRequestand returns a boolean indicating whether the request matches.
Example
Section titled “Example”use httpmock::prelude::*;
// Arrangelet 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 requestlet response = reqwest::blocking::get(server.url("/test")).unwrap();
// Assertm.assert();assert_eq!(response.status(), 200);Returns
Section titled “Returns”When: Returns the modified When object with the new custom matcher added to the expectations.
is_false
Section titled “is_false”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.
Parameters
Section titled “Parameters”matcher: A function that takes a reference to anHttpMockRequestand returns a boolean indicating whether the request matches.
Example
Section titled “Example”use httpmock::prelude::*;
// Arrangelet 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 requestlet response = reqwest::blocking::get(server.url("/test")).unwrap();
// Assertm.assert();assert_eq!(response.status(), 404);Returns
Section titled “Returns”When: Returns the modified When object with the new custom matcher added to the expectations.