From 02d9eed4d4242eefc7f9d5fbf2d593a103a365c2 Mon Sep 17 00:00:00 2001 From: n4n5 Date: Fri, 17 May 2024 15:19:16 +0200 Subject: [PATCH] add tests --- lychee-lib/src/client.rs | 9 +++++++++ lychee-lib/src/extract/html/html5ever.rs | 23 +++++++++++++++++++++++ lychee-lib/src/extract/html/html5gum.rs | 22 ++++++++++++++++++++++ lychee-lib/src/filter/mod.rs | 10 ++++++++++ lychee-lib/src/types/uri/valid.rs | 8 ++++++++ 5 files changed, 72 insertions(+) diff --git a/lychee-lib/src/client.rs b/lychee-lib/src/client.rs index 896c8da680..ed29e4ff93 100644 --- a/lychee-lib/src/client.rs +++ b/lychee-lib/src/client.rs @@ -378,6 +378,7 @@ impl ClientBuilder { exclude_link_local_ips: self.exclude_all_private || self.exclude_link_local_ips, exclude_loopback_ips: self.exclude_all_private || self.exclude_loopback_ips, include_mail: self.include_mail, + include_tel: false, }; Ok(Client { @@ -916,6 +917,14 @@ mod tests { })); } + #[tokio::test] + async fn test_include_tel() { + let client = ClientBuilder::builder().build().client().unwrap(); + assert!(client.is_excluded(&Uri { + url: "tel:1234567890".try_into().unwrap() + })); + } + #[tokio::test] async fn test_require_https() { let client = ClientBuilder::builder().build().client().unwrap(); diff --git a/lychee-lib/src/extract/html/html5ever.rs b/lychee-lib/src/extract/html/html5ever.rs index 87573b23be..1ee03b836f 100644 --- a/lychee-lib/src/extract/html/html5ever.rs +++ b/lychee-lib/src/extract/html/html5ever.rs @@ -319,6 +319,29 @@ mod tests { let uris = extract_html(input, false); assert_eq!(uris, expected); } + + #[test] + fn test_valid_tel() { + let input = r#" + + + + Test + + + + + "#; + + let expected = vec![RawUri { + text: "tel:1234567890".to_string(), + element: Some("a".to_string()), + attribute: Some("href".to_string()), + }]; + let uris = extract_html(input, false); + assert_eq!(uris, expected); + } + #[test] fn test_exclude_email_without_mailto() { let input = r#" diff --git a/lychee-lib/src/extract/html/html5gum.rs b/lychee-lib/src/extract/html/html5gum.rs index ad133963ce..cf9d88f489 100644 --- a/lychee-lib/src/extract/html/html5gum.rs +++ b/lychee-lib/src/extract/html/html5gum.rs @@ -454,6 +454,28 @@ mod tests { assert_eq!(uris, expected); } + #[test] + fn test_valid_tel() { + let input = r#" + + + + Test + + + + + "#; + + let expected = vec![RawUri { + text: "tel:1234567890".to_string(), + element: Some("a".to_string()), + attribute: Some("href".to_string()), + }]; + let uris = extract_html(input, false); + assert_eq!(uris, expected); + } + #[test] fn test_valid_email() { let input = r#" diff --git a/lychee-lib/src/filter/mod.rs b/lychee-lib/src/filter/mod.rs index f8d6130716..6cfdd21a3f 100644 --- a/lychee-lib/src/filter/mod.rs +++ b/lychee-lib/src/filter/mod.rs @@ -128,6 +128,8 @@ pub struct Filter { pub exclude_loopback_ips: bool, /// Example: octocat@github.com pub include_mail: bool, + /// Example: 1234567890 + pub include_tel: bool, } impl Filter { @@ -138,6 +140,13 @@ impl Filter { uri.is_mail() && !self.include_mail } + #[inline] + #[must_use] + /// Whether tel aren't checked (which is the default) + pub fn is_tel_excluded(&self, uri: &Uri) -> bool { + uri.is_tel() && !self.include_tel + } + #[must_use] /// Whether the IP address is excluded from checking pub fn is_ip_excluded(&self, uri: &Uri) -> bool { @@ -214,6 +223,7 @@ impl Filter { || self.is_host_excluded(uri) || self.is_ip_excluded(uri) || self.is_mail_excluded(uri) + || self.is_tel_excluded(uri) || is_example_domain(uri) || is_unsupported_domain(uri) { diff --git a/lychee-lib/src/types/uri/valid.rs b/lychee-lib/src/types/uri/valid.rs index df7ee9bb09..9e9bb72a72 100644 --- a/lychee-lib/src/types/uri/valid.rs +++ b/lychee-lib/src/types/uri/valid.rs @@ -332,6 +332,14 @@ mod tests { ); } + #[test] + fn test_uri_tel() { + assert_eq!( + Uri::try_from("tel:1234567890"), + Ok(Uri::try_from("tel:1234567890").unwrap()) + ); + } + #[test] fn test_uri_host_ip_v4() { assert_eq!(