| Crates.io | reqwest-rewire |
| lib.rs | reqwest-rewire |
| version | 0.2.0 |
| created_at | 2025-12-21 17:07:51.925634+00 |
| updated_at | 2026-01-12 18:02:39.943301+00 |
| description | reqwest-rewire is a lightweight wrapper around reqwest that transparently rewrites outgoing request URLs based on user-defined rules. It is designed primarily for testing and local development, allowing you to redirect HTTP requests to mock servers without changing application code. |
| homepage | |
| repository | https://github.com/ZackPedretti/reqwest-rewire |
| max_upload_size | |
| id | 1998291 |
| size | 123,674 |
reqwest-rewire is a lightweight wrapper around reqwest that transparently rewrites outgoing request URLs based on user-defined rules.
It is designed primarily for testing and local development, allowing you to redirect HTTP requests to mock servers without changing application code.
[dependencies]
reqwest-rewire = "0.2"
In many applications, you want to:
reqwest-rewire solves this by acting as a rewiring layer in front of reqwest.
You define a set of redirect rules:
https://real-api.com/api/
→ http://localhost:3000/api-mock/
Any request matching the left-hand side is transparently rewritten before being sent.
The most specific rule always wins.
TestableClient trait or reqwest_rewire::Client enumYour application code depends on a trait, not a concrete client:
use reqwest_rewire::TestableClient;
fn fetch_data(client: Box<dyn TestableClient>) {
client
.get("https://real-api.com/api/users")
.send()
.unwrap();
}
You can also use the Client enum, which has two states:
enum Client {
ReqwestClient(reqwest::Client),
TestClient(RewireClient),
}
It does implement the TestableClient trait, but it allows you to use it instead of dynamic object:
use reqwest_rewire;
fn fetch_data(client: reqwest_rewire::Client) { // No Box<dyn ...>
client
.get("https://real-api.com/api/users")
.send()
.unwrap();
}
reqwest::Client in production let client = reqwest::Client::new();
fetch_data(&client);
RewireClient in tests use std::collections::HashMap;
use reqwest_rewire::RewireClient;
let mut redirects = HashMap::new();
redirects.insert(
"https://real-api.com/api/".to_string(),
"http://localhost:3000/api-mock/".to_string(),
);
let client = RewireClient::new(redirects);
fetch_data(&client);
No application code changes required 🎉
url::UrlRequest:
https://real-api.com/api/v1/users?id=42
Rule:
https://real-api.com/api/
→ http://localhost:3000/api-mock/
Result:
http://localhost:3000/api-mock/v1/users?id=42
* is not valid in URLs)For full HTTP mocking, consider tools like wiremock, mockito, or httpmock.
reqwest-rewire is intentionally minimal:
This makes it ideal as a composable building block in larger test setups.
0.x releases may introduce breaking changes1.0 will guarantee API stabilityLicensed under either of:
You may choose either.
Issues, ideas, and pull requests are welcome!
Please open an issue before making large changes.