use crate::client::Client;
use crate::error::Result;
use crate::request::Request;
use crate::response::Response;
use super::hop;
pub(crate) async fn send_lean(client: &Client, request: Request) -> Result<Response> {
let params = hop::prepare_first_hop(client, request);
let timeout = params.timeout;
let hop = hop::build_hop_request(client, params)?;
let response = client.send_single_request(hop, &timeout).await?;
hop::update_cookie_jar(client, &response);
Ok(response)
}
#[cfg(test)]
mod tests {
use super::hop::test_support;
use super::*;
#[tokio::test]
async fn lean_path_prepares_the_expected_hop() {
for with_request_auth in [true, false] {
test_support::clear_recorded();
let client = test_support::scenario_client();
let request = test_support::scenario_request(&client, with_request_auth);
let _ = send_lean(&client, request).await;
let captured = test_support::take_recorded().expect("a prepared hop was captured");
assert_eq!(
captured,
test_support::expected_snapshot(with_request_auth),
"the lean path's prepared hop must match the shared expectation"
);
}
}
#[cfg(feature = "cookies")]
#[tokio::test]
async fn lean_path_updates_the_cookie_jar() {
use crate::cookie::CookieJar;
let (url, server) = test_support::spawn_set_cookie_server().await;
let client = Client::builder().cookie_jar(CookieJar::new()).build();
let response = client.get(&url).unwrap().send().await.expect("response");
assert_eq!(response.status(), http::StatusCode::OK);
server.await.expect("server task");
assert_eq!(
client
.cookies()
.cookies_for_url(&url::Url::parse(&url).expect("valid URL")),
Some(test_support::EXPECTED_COOKIE_HEADER.to_owned()),
"the lean path must leave the same jar state as the redirect path"
);
}
}