diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-22 02:09:59 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-22 02:09:59 +0900 |
| commit | 822d9a872807a92a5337ee8b7bab96dc9845cdbb (patch) | |
| tree | 4931365df5978caffade69cad2154718a9076f66 /crates/shirabe/tests/util/http/proxy_manager_test.rs | |
| parent | f691b864b687f251c3b266e8cff2774d730567ba (diff) | |
| download | php-shirabe-822d9a872807a92a5337ee8b7bab96dc9845cdbb.tar.gz php-shirabe-822d9a872807a92a5337ee8b7bab96dc9845cdbb.tar.zst php-shirabe-822d9a872807a92a5337ee8b7bab96dc9845cdbb.zip | |
test: port more test cases
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/tests/util/http/proxy_manager_test.rs')
| -rw-r--r-- | crates/shirabe/tests/util/http/proxy_manager_test.rs | 202 |
1 files changed, 189 insertions, 13 deletions
diff --git a/crates/shirabe/tests/util/http/proxy_manager_test.rs b/crates/shirabe/tests/util/http/proxy_manager_test.rs index 0c177f7..dede524 100644 --- a/crates/shirabe/tests/util/http/proxy_manager_test.rs +++ b/crates/shirabe/tests/util/http/proxy_manager_test.rs @@ -38,7 +38,7 @@ impl Drop for TearDown { } #[test] -#[ignore = "not yet ported (ProxyManager is driven by proxy environment variables)"] +#[ignore = "ProxyManager::get_instance returns a shared &'static Mutex, so instance identity (=== / after reset) cannot be expressed through the public API"] fn test_instantiation() { let _tear_down = TearDown; set_up(); @@ -46,49 +46,225 @@ fn test_instantiation() { } #[test] -#[ignore = "not yet ported (ProxyManager is driven by proxy environment variables)"] +#[ignore] fn test_get_proxy_for_request_throws_on_bad_proxy_url() { let _tear_down = TearDown; set_up(); - todo!() + + Platform::put_env("http_proxy", "localhost"); + ProxyManager::reset(); + let mutex = ProxyManager::get_instance(); + let guard = mutex.lock().unwrap(); + let proxy_manager = guard.as_ref().unwrap(); + + assert!( + proxy_manager + .get_proxy_for_request("http://example.com") + .is_err() + ); } #[test] -#[ignore = "not yet ported (ProxyManager is driven by proxy environment variables)"] +#[ignore] fn test_lowercase_overrides_uppercase() { let _tear_down = TearDown; set_up(); - todo!() + + // server, url, expectedUrl + let cases: Vec<(Vec<(&str, &str)>, &str, &str)> = vec![ + ( + vec![ + ("HTTP_PROXY", "http://upper.com"), + ("http_proxy", "http://lower.com"), + ], + "http://repo.org", + "http://lower.com:80", + ), + ( + vec![ + ("CGI_HTTP_PROXY", "http://upper.com"), + ("cgi_http_proxy", "http://lower.com"), + ], + "http://repo.org", + "http://lower.com:80", + ), + ( + vec![ + ("HTTPS_PROXY", "http://upper.com"), + ("https_proxy", "http://lower.com"), + ], + "https://repo.org", + "http://lower.com:80", + ), + ]; + + for (server, url, expected_url) in cases { + set_up(); + for (name, value) in &server { + Platform::put_env(name, value); + } + ProxyManager::reset(); + + let mutex = ProxyManager::get_instance(); + let guard = mutex.lock().unwrap(); + let proxy = guard.as_ref().unwrap().get_proxy_for_request(url).unwrap(); + assert_eq!(expected_url, proxy.get_status(None).unwrap()); + } } #[test] -#[ignore = "not yet ported (ProxyManager is driven by proxy environment variables)"] +#[ignore] fn test_cgi_proxy_is_only_used_when_no_http_proxy() { let _tear_down = TearDown; set_up(); - todo!() + + // server, expectedUrl + let cases: Vec<(Vec<(&str, &str)>, &str)> = vec![ + ( + vec![("CGI_HTTP_PROXY", "http://cgi.com:80")], + "http://cgi.com:80", + ), + ( + vec![ + ("http_proxy", "http://http.com:80"), + ("CGI_HTTP_PROXY", "http://cgi.com:80"), + ], + "http://http.com:80", + ), + ]; + + for (server, expected_url) in cases { + set_up(); + for (name, value) in &server { + Platform::put_env(name, value); + } + ProxyManager::reset(); + + let mutex = ProxyManager::get_instance(); + let guard = mutex.lock().unwrap(); + let proxy = guard + .as_ref() + .unwrap() + .get_proxy_for_request("http://repo.org") + .unwrap(); + assert_eq!(expected_url, proxy.get_status(None).unwrap()); + } } #[test] -#[ignore = "not yet ported (ProxyManager is driven by proxy environment variables)"] +#[ignore] fn test_no_http_proxy_does_not_use_https_proxy() { let _tear_down = TearDown; set_up(); - todo!() + + Platform::put_env("https_proxy", "https://proxy.com:443"); + ProxyManager::reset(); + let mutex = ProxyManager::get_instance(); + let guard = mutex.lock().unwrap(); + let proxy = guard + .as_ref() + .unwrap() + .get_proxy_for_request("http://repo.org") + .unwrap(); + assert_eq!("", proxy.get_status(None).unwrap()); } #[test] -#[ignore = "not yet ported (ProxyManager is driven by proxy environment variables)"] +#[ignore] fn test_no_https_proxy_does_not_use_http_proxy() { let _tear_down = TearDown; set_up(); - todo!() + + Platform::put_env("http_proxy", "http://proxy.com:80"); + ProxyManager::reset(); + let mutex = ProxyManager::get_instance(); + let guard = mutex.lock().unwrap(); + let proxy = guard + .as_ref() + .unwrap() + .get_proxy_for_request("https://repo.org") + .unwrap(); + assert_eq!("", proxy.get_status(None).unwrap()); } #[test] -#[ignore = "not yet ported (ProxyManager is driven by proxy environment variables)"] +#[ignore] fn test_get_proxy_for_request() { + use indexmap::IndexMap; + use shirabe_php_shim::PhpMixed; + let _tear_down = TearDown; set_up(); - todo!() + + let server = vec![ + ("http_proxy", "http://user:p%40ss@proxy.com"), + ("https_proxy", "https://proxy.com:443"), + ("no_proxy", "other.repo.org"), + ]; + + let http_options = + |pairs: &[(&str, PhpMixed)]| -> Option<IndexMap<String, IndexMap<String, PhpMixed>>> { + let mut http: IndexMap<String, PhpMixed> = IndexMap::new(); + for (k, v) in pairs { + http.insert((*k).to_string(), v.clone()); + } + let mut options: IndexMap<String, IndexMap<String, PhpMixed>> = IndexMap::new(); + options.insert("http".to_string(), http); + Some(options) + }; + + // server, url, options, status, excluded + let cases: Vec<( + Vec<(&str, &str)>, + &str, + Option<IndexMap<String, IndexMap<String, PhpMixed>>>, + &str, + bool, + )> = vec![ + (vec![], "http://repo.org", None, "", false), + ( + server.clone(), + "http://repo.org", + http_options(&[ + ("proxy", PhpMixed::String("tcp://proxy.com:80".to_string())), + ( + "header", + PhpMixed::String("Proxy-Authorization: Basic dXNlcjpwQHNz".to_string()), + ), + ("request_fulluri", PhpMixed::Bool(true)), + ]), + "http://***:***@proxy.com:80", + false, + ), + ( + server.clone(), + "https://repo.org", + http_options(&[("proxy", PhpMixed::String("ssl://proxy.com:443".to_string()))]), + "https://proxy.com:443", + false, + ), + ( + server.clone(), + "https://other.repo.org", + None, + "excluded by no_proxy", + true, + ), + ]; + + for (srv, url, options, status, excluded) in cases { + set_up(); + for (name, value) in &srv { + Platform::put_env(name, value); + } + ProxyManager::reset(); + + let mutex = ProxyManager::get_instance(); + let guard = mutex.lock().unwrap(); + let proxy = guard.as_ref().unwrap().get_proxy_for_request(url).unwrap(); + + assert_eq!(options.as_ref(), proxy.get_context_options()); + assert_eq!(status, proxy.get_status(None).unwrap()); + assert_eq!(excluded, proxy.is_excluded_by_no_proxy()); + } } |
