Path
This section describes matcher functions designed to target and match the request path, such as http://localhost:8080/my-path
.
path
Specifies the expected URL path that incoming requests must match for the mock server to respond. This is useful for targeting specific endpoints, such as API routes, to ensure only relevant requests trigger the mock response.
Parameters
path
: A string or other value convertible toString
that represents the expected URL path.
Example
use httpmock::prelude::*;
// Start a new mock serverlet server = MockServer::start();
// Create a mock that matches requests to `/test`let mock = server.mock(|when, then| { when.path("/test"); then.status(200); // Respond with a 200 status code});
// Make a request to the mock server using the specified pathlet response = reqwest::blocking::get(server.url("/test")).unwrap();
// Ensure the request was successfulassert_eq!(response.status(), 200);
// Verify that the mock was called at least oncemock.assert();
Returns
The updated When
instance, allowing method chaining for additional configuration.
path_not
Specifies the URL path that incoming requests must not match for the mock server to respond. This is helpful when you need to exclude specific endpoints while allowing others through.
To add multiple excluded paths, invoke this function multiple times.
Parameters
path
: A string or other value convertible toString
that represents the URL path to exclude.
Example
use httpmock::prelude::*;
// Start a new mock serverlet server = MockServer::start();
// Create a mock that will not match requests to `/exclude`let mock = server.mock(|when, then| { when.path_not("/exclude"); then.status(200); // Respond with status 200 for all other paths});
// Make a request to a path that does not match the exclusionlet response = reqwest::blocking::get(server.url("/include")).unwrap();
// Ensure the request was successfulassert_eq!(response.status(), 200);
// Verify that the mock was called at least oncemock.assert();
Returns
The updated When
instance, allowing method chaining for further configuration.
path_includes
Specifies a substring that the URL path must contain for the mock server to respond. This constraint is useful for matching URLs based on partial segments, especially when exact path matching isn’t required.
Parameters
substring
: A string or any value convertible toString
representing the substring that must be present in the URL path.
Example
use httpmock::prelude::*;
// Start a new mock serverlet server = MockServer::start();
// Create a mock that matches any path containing the substring "es"let mock = server.mock(|when, then| { when.path_includes("es"); then.status(200); // Respond with a 200 status code for matched requests});
// Make a request to a path containing "es" to trigger the mock responselet response = reqwest::blocking::get(server.url("/test")).unwrap();
// Ensure the request was successfulassert_eq!(response.status(), 200);
// Ensure that the mock was called at least oncemock.assert();
Returns
The updated When
instance to allow method chaining for further configuration.
path_excludes
Specifies a substring that the URL path must not contain for the mock server to respond. This constraint is useful for excluding requests to paths containing particular segments or patterns.
Parameters
substring
: A string or other value convertible toString
representing the substring that should not appear in the URL path.
Example
use httpmock::prelude::*;
// Start a new mock serverlet server = MockServer::start();
// Create a mock that matches any path not containing the substring "xyz"let mock = server.mock(|when, then| { when.path_excludes("xyz"); then.status(200); // Respond with status 200 for paths excluding "xyz"});
// Make a request to a path that does not contain "xyz"let response = reqwest::blocking::get(server.url("/testpath")).unwrap();
// Ensure the request was successfulassert_eq!(response.status(), 200);
// Ensure the mock server returned the expected responsemock.assert();
Returns
The updated When
instance to enable method chaining for additional configuration.
path_prefix
Specifies a prefix that the URL path must start with for the mock server to respond. This is useful when only the initial segments of a path need to be validated, such as checking specific API routes.
Parameters
prefix
: A string or other value convertible toString
representing the prefix that the URL path should start with.
Example
use httpmock::prelude::*;
// Start a new mock serverlet server = MockServer::start();
// Create a mock that matches any path starting with the prefix "/api"let mock = server.mock(|when, then| { when.path_prefix("/api"); then.status(200); // Respond with a 200 status code for matched requests});
// Make a request to a path starting with "/api"let response = reqwest::blocking::get(server.url("/api/v1/resource")).unwrap();
// Ensure the request was successfulassert_eq!(response.status(), 200);
// Verify that the mock was called at least oncemock.assert();
Returns
The updated When
instance to allow method chaining for further configuration.
path_prefix_not
Specifies a prefix that the URL path must not start with for the mock server to respond. This constraint is useful for excluding paths that begin with particular segments or patterns.
Parameters
prefix
: A string or other value convertible toString
representing the prefix that the URL path should not start with.
Example
use httpmock::prelude::*;
// Start a new mock serverlet server = MockServer::start();
// Create a mock that matches any path not starting with the prefix "/admin"let mock = server.mock(|when, then| { when.path_prefix_not("/admin"); then.status(200); // Respond with status 200 for paths excluding "/admin"});
// Make a request to a path that does not start with "/admin"let response = reqwest::blocking::get(server.url("/public/home")).unwrap();
// Ensure the request was successfulassert_eq!(response.status(), 200);
// Verify that the mock server returned the expected responsemock.assert();
Returns
The updated When
instance to allow method chaining for additional configuration.
path_suffix
Specifies a suffix that the URL path must end with for the mock server to respond. This is useful when the final segments of a path need to be validated, such as file extensions or specific patterns.
Parameters
suffix
: A string or other value convertible toString
representing the suffix that the URL path should end with.
Example
use httpmock::prelude::*;
// Start a new mock serverlet server = MockServer::start();
// Create a mock that matches any path ending with the suffix ".html"let mock = server.mock(|when, then| { when.path_suffix(".html"); then.status(200); // Respond with a 200 status code for matched requests});
// Make a request to a path ending with ".html"let response = reqwest::blocking::get(server.url("/about/index.html")).unwrap();
// Ensure the request was successfulassert_eq!(response.status(), 200);
// Verify that the mock was called at least oncemock.assert();
Returns
The updated When
instance to allow method chaining for further configuration.
path_suffix_not
Specifies a suffix that the URL path must not end with for the mock server to respond. This constraint is useful for excluding paths with specific file extensions or patterns.
Parameters
suffix
: A string or other value convertible toString
representing the suffix that the URL path should not end with.
Example
use httpmock::prelude::*;
// Start a new mock serverlet server = MockServer::start();
// Create a mock that matches any path not ending with the suffix ".json"let mock = server.mock(|when, then| { when.path_suffix_not(".json"); then.status(200); // Respond with a 200 status code for paths excluding ".json"});
// Make a request to a path that does not end with ".json"let response = reqwest::blocking::get(server.url("/about/index.html")).unwrap();
// Ensure the request was successfulassert_eq!(response.status(), 200);
// Verify that the mock was called at least oncemock.assert();
Returns
The updated When
instance to allow method chaining for further configuration.
path_matches
Specifies a regular expression that the URL path must match for the mock server to respond. This method allows flexible matching using regex patterns, making it useful for various matching scenarios.
Parameters
regex
: An expression that implementsInto<Regex>
, representing the regex pattern to match against the URL path.
Example
use httpmock::prelude::*;
// Start a new mock serverlet server = MockServer::start();
// Create a mock that matches paths ending with the suffix "le"let mock = server.mock(|when, then| { when.path_matches(r"le$"); then.status(200); // Respond with a 200 status code for paths matching the pattern});
// Make a request to a path ending with "le"let response = reqwest::blocking::get(server.url("/example")).unwrap();
// Ensure the request was successfulassert_eq!(response.status(), 200);
// Verify that the mock server returned the expected responsemock.assert();
Returns
The updated When
instance to allow method chaining for additional configuration.
Errors
This function will panic if the provided regex pattern is invalid.