From 56dc2a4b5bf7bd6c7bd244885ac61e77ddf863c7 Mon Sep 17 00:00:00 2001 From: Namilskyy Date: Mon, 1 Dec 2025 20:18:27 +0300 Subject: Refactored http tests --- tests/http_funcs.rs | 99 ++++++++++++++++++++++++++--------------------------- 1 file changed, 49 insertions(+), 50 deletions(-) diff --git a/tests/http_funcs.rs b/tests/http_funcs.rs index 55d8c3c..6f43603 100644 --- a/tests/http_funcs.rs +++ b/tests/http_funcs.rs @@ -19,6 +19,9 @@ mod http_package_tests { let _ = env_logger::builder().is_test(true).try_init(); } + use std::time::Duration; + use tokio::time::timeout; + #[tokio::test] async fn test_http_fetch_index_success() -> Result<(), Box> { init_logger(); // Initialize logger @@ -26,21 +29,20 @@ mod http_package_tests { let mut server = mockito::Server::new_async().await; let index_toml_content = r#" -[[packages]] -name = "test-pkg" -version = "1.0.0" -arch = "X86_64" -url = "/repo/test-pkg.mesk" -descr = "A test package from index" -license = "MIT" -"#; + [[packages]] + name = "test-pkg" + version = "1.0.0" + arch = "X86_64" + url = "/repo/test-pkg.mesk" + descr = "A test package from index" + license = "MIT" + "#; let temp_dir = TempDir::new()?; let index_toml_path = temp_dir.path().join("INDEX.toml"); fs::write(&index_toml_path, index_toml_content)?; let archive_path = temp_dir.path().join("INDEX.tar.gz"); - // Создаём архив let file = fs::File::create(&archive_path)?; let gz_encoder = flate2::write::GzEncoder::new(file, flate2::Compression::default()); let mut tar_builder = tar::Builder::new(gz_encoder); @@ -68,45 +70,42 @@ license = "MIT" let mut http_pkg = HTTPPackage::new(config_with_mock_url); debug!("Attempting to fetch index via HTTP"); - let result = http_pkg.fetch_index_http().await; - - info!("Checking if fetch_index_http result is OK"); - assert!(result.is_ok(), "fetch_index_http should return Ok"); - - info!("Checking if fetch_index_http returned true"); - assert_eq!( - result.unwrap(), - true, - "fetch_index_http should indicate success" - ); - - info!("Checking if index_packages is populated"); - assert!( - http_pkg.index_packages.is_some(), - "index_packages should not be None after successful fetch" - ); - - let packages = http_pkg.index_packages.as_ref().unwrap(); - info!("Checking number of packages in the index"); - assert_eq!( - packages.len(), - 1, - "There should be exactly one package in the index" - ); - - info!("Checking if 'test-pkg' exists in the index"); - assert!( - packages.contains_key("test-pkg"), - "The package 'test-pkg' should be present in the index" - ); - - let pkg_info = packages.get("test-pkg").unwrap(); - - info!("Checking if package URL is correctly formed"); - assert!( - pkg_info.url.starts_with(&server.url()), - "Package URL should be prefixed with the server URL" - ); + let result = timeout(Duration::from_secs(10), http_pkg.fetch_index_http()).await; + + match result { + Ok(Ok(true)) => { + info!("Checking if index_packages is populated"); + assert!( + http_pkg.index_packages.is_some(), + "index_packages should not be None after successful fetch" + ); + + let packages = http_pkg.index_packages.as_ref().unwrap(); + info!("Checking number of packages in the index"); + assert_eq!( + packages.len(), + 1, + "There should be exactly one package in the index" + ); + + info!("Checking if 'test-pkg' exists in the index"); + assert!( + packages.contains_key("test-pkg"), + "The package 'test-pkg' should be present in the index" + ); + + let pkg_info = packages.get("test-pkg").unwrap(); + + info!("Checking if package URL is correctly formed"); + assert!( + pkg_info.url.starts_with(&server.url()), + "Package URL should be prefixed with the server URL" + ); + } + Ok(Ok(false)) => panic!("fetch_index_http should indicate success"), + Ok(Err(e)) => panic!("fetch_index_http failed: {:?}", e), + Err(_) => panic!("fetch_index_http timed out"), + } Ok(()) } @@ -119,7 +118,7 @@ license = "MIT" let mut server = mockito::Server::new_async().await; let _mock = server .mock("GET", "/INDEX.tar.gz") - .with_status(404) // Ошибка HTTP + .with_status(404) .create_async() .await; @@ -238,4 +237,4 @@ license = "MIT" Ok(()) } -} +} \ No newline at end of file -- cgit v1.2.3