diff options
Diffstat (limited to 'tests/http_funcs.rs')
| -rw-r--r-- | tests/http_funcs.rs | 110 |
1 files changed, 76 insertions, 34 deletions
diff --git a/tests/http_funcs.rs b/tests/http_funcs.rs index efbaa53..232d3b8 100644 --- a/tests/http_funcs.rs +++ b/tests/http_funcs.rs @@ -1,28 +1,26 @@ -use mesk::pkgtoolkit::pkgtools::{Package, Archs}; -use mesk::net::http_package::HTTPPackage; +use mesk::net::http_package::HTTPPackage; +use mesk::pkgtoolkit::pkgtools::{Archs, Package}; use std::fs; mod shared; use shared::create_test_config; use std::collections::HashMap; -use tempfile::TempDir; +use tempfile::TempDir; use tokio; // Add these imports for logging -use log::{info, debug, error}; +use log::{debug, error, info}; #[cfg(test)] mod http_package_tests { use super::*; - + // Helper function to initialize the logger for each test. // This uses `call_once` to ensure it's only initialized once, // even if called multiple times, preventing panics. fn init_logger() { - let _ = env_logger::builder() - .is_test(true) - .try_init(); + let _ = env_logger::builder().is_test(true).try_init(); } #[tokio::test] @@ -57,7 +55,7 @@ license = "MIT" let _mock = server .mock("GET", "/INDEX.tar.gz") .with_status(200) - .with_header("content-length", &mock_body.len().to_string()) + .with_header("content-length", &mock_body.len().to_string()) .with_body(mock_body) .create_async() .await; @@ -66,7 +64,7 @@ license = "MIT" let config = create_test_config(temp_config_dir.path().to_str().unwrap()); let mut config_with_mock_url = config.clone(); - config_with_mock_url.repo.repo_url = server.url(); + config_with_mock_url.repo.repo_url = server.url(); let mut http_pkg = HTTPPackage::new(config_with_mock_url); @@ -75,25 +73,41 @@ license = "MIT" 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"); - + 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"); - + 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"); - + 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"); - + 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"); + assert!( + pkg_info.url.starts_with(&server.url()), + "Package URL should be prefixed with the server URL" + ); Ok(()) } @@ -121,11 +135,18 @@ license = "MIT" let result = http_pkg.fetch_index_http().await; info!("Checking if fetch_index_http resulted in an error as expected"); - assert!(result.is_err(), "fetch_index_http should return an Err for a 404 response"); - + assert!( + result.is_err(), + "fetch_index_http should return an Err for a 404 response" + ); + let err_msg = result.unwrap_err().to_string(); info!("Verifying error message contains 'HTTP Error: 404'"); - assert!(err_msg.contains("HTTP Error: 404"), "Error message should contain 'HTTP Error: 404', got: {}", err_msg); + assert!( + err_msg.contains("HTTP Error: 404"), + "Error message should contain 'HTTP Error: 404', got: {}", + err_msg + ); Ok(()) } @@ -155,10 +176,17 @@ license = "MIT" let pkg_info = http_pkg.fetch_package_info("test-pkg"); info!("Checking if fetch_package_info returned successfully"); - assert!(pkg_info.is_ok(), "fetch_package_info should return Ok for an existing package"); - + assert!( + pkg_info.is_ok(), + "fetch_package_info should return Ok for an existing package" + ); + info!("Checking the retrieved package name"); - assert_eq!(pkg_info.unwrap().name, "test-pkg", "Retrieved package name should be 'test-pkg'"); + assert_eq!( + pkg_info.unwrap().name, + "test-pkg", + "Retrieved package name should be 'test-pkg'" + ); Ok(()) } @@ -171,16 +199,23 @@ license = "MIT" let temp_config_dir = TempDir::new()?; let config = create_test_config(temp_config_dir.path().to_str().unwrap()); - let http_pkg = HTTPPackage::new(config.clone()); + let http_pkg = HTTPPackage::new(config.clone()); debug!("Attempting to fetch package info before index load"); let result = http_pkg.fetch_package_info("nonexistent-pkg"); info!("Checking if fetch_package_info failed due to unloaded index"); - assert!(result.is_err(), "fetch_package_info should fail when index is not loaded"); - + assert!( + result.is_err(), + "fetch_package_info should fail when index is not loaded" + ); + let err_msg = result.unwrap_err().to_string(); info!("Verifying error message contains 'Index not loaded'"); - assert!(err_msg.contains("Index not loaded"), "Error message should contain 'Index not loaded', got: {}", err_msg); + assert!( + err_msg.contains("Index not loaded"), + "Error message should contain 'Index not loaded', got: {}", + err_msg + ); let mut http_pkg_empty_index = HTTPPackage::new(config); http_pkg_empty_index.index_packages = Some(HashMap::new()); @@ -188,12 +223,19 @@ license = "MIT" let result_empty = http_pkg_empty_index.fetch_package_info("nonexistent-pkg"); info!("Checking if fetch_package_info failed due to missing package in index"); - assert!(result_empty.is_err(), "fetch_package_info should fail for a package not in the index"); - + assert!( + result_empty.is_err(), + "fetch_package_info should fail for a package not in the index" + ); + let err_msg_empty = result_empty.unwrap_err().to_string(); info!("Verifying error message contains 'not found in index'"); - assert!(err_msg_empty.contains("not found in index"), "Error message should contain 'not found in index', got: {}", err_msg_empty); + assert!( + err_msg_empty.contains("not found in index"), + "Error message should contain 'not found in index', got: {}", + err_msg_empty + ); Ok(()) } -}
\ No newline at end of file +} |
