#![allow(clippy::unwrap_used, clippy::expect_used)]
use liburlx::Error;
#[test]
fn url_parse_error_for_malformed_url() {
let mut easy = liburlx::Easy::new();
let err = easy.url("not a url at all !!!").unwrap_err();
assert!(matches!(err, Error::UrlParse(_)), "Expected UrlParse error, got: {err:?}");
}
#[test]
fn url_parse_error_for_empty_url() {
let mut easy = liburlx::Easy::new();
let err = easy.url("").unwrap_err();
assert!(
matches!(err, Error::UrlParse(_)),
"Expected UrlParse error for empty URL, got: {err:?}"
);
}
#[test]
fn url_parse_error_for_garbage() {
let mut easy = liburlx::Easy::new();
let err = easy.url("://").unwrap_err();
assert!(
matches!(err, Error::UrlParse(_)),
"Expected UrlParse error for garbage URL, got: {err:?}"
);
}
#[test]
fn valid_url_patterns() {
let mut easy = liburlx::Easy::new();
assert!(easy.url("http://example.com").is_ok());
assert!(easy.url("https://example.com").is_ok());
assert!(easy.url("http://example.com:8080/path").is_ok());
assert!(easy.url("http://user:pass@example.com/").is_ok());
assert!(easy.url("ftp://ftp.example.com/pub/file.txt").is_ok());
assert!(easy.url("file:///tmp/test.txt").is_ok());
}
#[test]
fn url_with_query_and_fragment() {
let url = liburlx::Url::parse("http://example.com/path?key=value&a=b#section").unwrap();
assert_eq!(url.path(), "/path");
assert_eq!(url.query(), Some("key=value&a=b"));
assert_eq!(url.fragment(), Some("section"));
}
#[test]
fn url_percent_encoded_path() {
let url = liburlx::Url::parse("http://example.com/path%20with%20spaces").unwrap();
assert!(url.path().contains("path"));
}
#[test]
fn error_display_readable() {
let mut easy = liburlx::Easy::new();
let err = easy.url("://bad").unwrap_err();
let msg = err.to_string();
assert!(!msg.is_empty());
assert!(!msg.starts_with("Error("), "Error display should be human-readable, not Debug: {msg}");
}
#[test]
fn error_variant_distinctness() {
let url_err = liburlx::Easy::new().url("://").unwrap_err();
assert!(matches!(url_err, Error::UrlParse(_)));
let msg1 = url_err.to_string();
assert!(!msg1.is_empty());
}
#[test]
fn timeout_error_format() {
let err = Error::Timeout(std::time::Duration::from_secs(30));
let msg = err.to_string();
assert!(msg.contains("30"), "Expected duration in error: {msg}");
assert!(msg.contains("timeout"), "Expected 'timeout' in error: {msg}");
}
#[test]
fn http_error_format() {
let err = Error::Http("bad response".to_string());
let msg = err.to_string();
assert!(msg.contains("bad response"), "Expected message in error: {msg}");
}
#[test]
fn transfer_error_format() {
let err = Error::Transfer { code: 22, message: "HTTP error".to_string() };
let msg = err.to_string();
assert!(msg.contains("22"), "Expected code in error: {msg}");
assert!(msg.contains("HTTP error"), "Expected message in error: {msg}");
}