Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-Just-Nans committed May 17, 2024
1 parent 9ba7a37 commit 02d9eed
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lychee-lib/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
23 changes: 23 additions & 0 deletions lychee-lib/src/extract/html/html5ever.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,29 @@ mod tests {
let uris = extract_html(input, false);
assert_eq!(uris, expected);
}

#[test]
fn test_valid_tel() {
let input = r#"<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<a href="tel:1234567890">
</body>
</html>"#;

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#"<!DOCTYPE html>
Expand Down
22 changes: 22 additions & 0 deletions lychee-lib/src/extract/html/html5gum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,28 @@ mod tests {
assert_eq!(uris, expected);
}

#[test]
fn test_valid_tel() {
let input = r#"<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<a href="tel:1234567890">
</body>
</html>"#;

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#"<!DOCTYPE html>
Expand Down
10 changes: 10 additions & 0 deletions lychee-lib/src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ pub struct Filter {
pub exclude_loopback_ips: bool,
/// Example: [email protected]
pub include_mail: bool,
/// Example: 1234567890
pub include_tel: bool,
}

impl Filter {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
{
Expand Down
8 changes: 8 additions & 0 deletions lychee-lib/src/types/uri/valid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down

0 comments on commit 02d9eed

Please sign in to comment.