tor-experiments/src/tests/get_request_tests.rs
2025-10-28 16:22:00 +03:00

72 lines
2.9 KiB
Rust

#[cfg(test)]
pub mod tests {
use anyhow::Result;
use arti_client::TorClient;
use crate::ureq_builder::*;
use arti_ureq::ureq::Agent;
use crate::tor_client_builder::{choose_tls_provider, fetch_with_client, make_tor_client, make_tor_client_config};
// We will use ureq for https which does the needed tls
const TEST_URL_HTTPS: &str = "https://check.torproject.org/api/ip";
// For manual connection using the arti tor client, we will use the following over http
const TEST_URL_HTTP_PLAIN: &str = "check.torproject.org";
const TEST_URL_PATH_HTTP_PLAIN: &str = "/api/ip";
// We will use the following onion address with the tor client.
const TEST_ONION: &str = "2gzyxa5ihm7nsggfxnu52rck2vv4rvmdlkiu3zzui5du4xyclen53wid.onion";
pub const TEST_ONION_PATH: &str = "/";
/// Builds all components
fn build_ureq_agent() -> Result<Agent> {
let tls = choose_tls_provider();
let tor = make_tor_client()?;
let connector = make_connector(tor, tls)?;
let agent = make_ureq_agent(connector, tls)?;
Ok(agent)
}
/// Test: send get request to url over Tor using ureq.
/// This test may take a little while on the first run
#[test]
fn fetches_url_over_tor_ureq() -> Result<()> {
let agent = build_ureq_agent()?;
let body = fetch_with_agent(&agent, TEST_URL_HTTPS)?;
//sanity check:
assert!(!body.trim().is_empty(), "empty body from {}", TEST_URL_HTTPS);
Ok(())
}
/// Test: send get request to url over Tor using the tor client.
/// This only works for http since there is no tls provider for https.
/// This test may take a little while on the first run
#[tokio::test(flavor = "multi_thread")]
async fn fetches_url_over_tor_client() -> Result<()> {
let config = make_tor_client_config();
let tor_client = TorClient::builder()
.config(config)
.create_unbootstrapped()?;
let body = fetch_with_client(tor_client, TEST_URL_HTTP_PLAIN, TEST_URL_PATH_HTTP_PLAIN).await?;
//sanity check:
assert!(!body.trim().is_empty(), "empty body from {}", TEST_URL_HTTP_PLAIN);
Ok(())
}
/// Test: send get request to onion address over Tor using the tor client.
/// again this is http and no tls
/// warning: this will print out an entire html page
/// This test may take a little while on the first run
#[tokio::test(flavor = "multi_thread")]
async fn fetches_onion_url_over_tor_client() -> Result<()> {
let config = make_tor_client_config();
let tor_client = TorClient::builder()
.config(config)
.create_unbootstrapped()?;
let body = fetch_with_client(tor_client, TEST_ONION, TEST_ONION_PATH).await?;
//sanity check:
assert!(!body.trim().is_empty(), "empty body from {}", TEST_ONION);
Ok(())
}
}