Recording your reqwest calls
You have 2 ways to record reqwest http calls ; either with the stubr::Record
trait (highly recommended) or the "original"
way, still supported, with a standalone recording proxy.
This requires the record-reqwest
feature.
trait (recommended)
use asserhttp::*;
use stubr::Record as _;
#[test]
#[stubr::mock("ping.json")] // 👈 spawn a mock server
fn record_reqwest_trait() {
// recording unfortunately requires using reqwest's builder hence the syntax is a bit verbose
let req = reqwest::blocking::ClientBuilder::new().build().unwrap()
.get(stubr.uri())
// 👇 this will intercept and dump all http traffic going through this client
.record() // or `record_with()` for configuring it
.build().unwrap();
reqwest::blocking::Client::default().execute(req).unwrap().expect_status_ok();
}
You can find your recorded stubs under ./target/stubs/localhost
NB: async is not supported yet
standalone
use asserhttp::*;
#[tokio::test(flavor = "multi_thread")] // 👈 required by recording proxy
#[stubr::mock("ping.json")] // 👈 spawn a mock server
async fn record_reqwest() {
// 👇 spawn the recording proxy
stubr::Stubr::record() // or `record_with()` for configuring it
// 👇 builds a reqwest client with proxy configured
.reqwest_client()
.get(stubr.uri())
.send()
.await
.expect_status_ok();
}
You can find your recorded stubs under ./target/stubs/localhost