use anyhow::Result;
use wiremock::matchers::{any, method};
use wiremock::{Mock, MockServer, ResponseTemplate};
use uv_client::BaseClientBuilder;
use uv_configuration::ProxyUrl;
#[tokio::test]
async fn http_proxy() -> Result<()> {
let target_server = MockServer::start().await;
Mock::given(method("GET"))
.respond_with(ResponseTemplate::new(200))
.mount(&target_server)
.await;
let proxy_server = MockServer::start().await;
Mock::given(any())
.respond_with(ResponseTemplate::new(200))
.mount(&proxy_server)
.await;
let client = BaseClientBuilder::default()
.http_proxy(Some(proxy_server.uri().parse::<ProxyUrl>()?))
.build()?;
let response = client
.for_host(&target_server.uri().parse()?)
.get(target_server.uri())
.send()
.await?;
assert_eq!(response.status(), 200);
let received_requests = proxy_server.received_requests().await.unwrap();
assert_eq!(received_requests.len(), 1);
Ok(())
}
#[tokio::test]
async fn no_proxy() -> Result<()> {
let target_server = MockServer::start().await;
Mock::given(method("GET"))
.respond_with(ResponseTemplate::new(200))
.mount(&target_server)
.await;
let proxy_server = MockServer::start().await;
Mock::given(any())
.respond_with(ResponseTemplate::new(200))
.mount(&proxy_server)
.await;
let target_host = target_server.address().ip().to_string();
let client = BaseClientBuilder::default()
.http_proxy(Some(proxy_server.uri().parse::<ProxyUrl>()?))
.no_proxy(Some(vec![target_host]))
.build()?;
let response = client
.for_host(&target_server.uri().parse()?)
.get(target_server.uri())
.send()
.await?;
assert_eq!(response.status(), 200);
let received_requests = proxy_server.received_requests().await.unwrap();
assert_eq!(received_requests.len(), 0);
Ok(())
}