implement domain matching function and adjust tests

pull/270/head
Sunshine 2 years ago
parent 9356161a89
commit b13be1e0ce
No known key found for this signature in database
GPG Key ID: B80CA68703CD8AB1

@ -93,33 +93,60 @@ pub fn detect_media_type_by_file_name(filename: &str) -> String {
} }
pub fn domain_is_within_domain(domain: &str, domain_to_match_against: &str) -> bool { pub fn domain_is_within_domain(domain: &str, domain_to_match_against: &str) -> bool {
let domain_partials: Vec<&str> = domain.split(".").collect(); if domain_to_match_against.len() == 0 {
return false;
}
if domain_to_match_against == "." {
return true;
}
let domain_partials: Vec<&str> = domain.trim_end_matches(".").rsplit(".").collect();
let domain_to_match_against_partials: Vec<&str> = domain_to_match_against let domain_to_match_against_partials: Vec<&str> = domain_to_match_against
.trim_start_matches(".") .trim_end_matches(".")
.split(".") .rsplit(".")
.collect(); .collect();
let domain_to_match_against_starts_with_a_dot = domain_to_match_against.starts_with(".");
let mut i: usize = 0;
let l: usize = std::cmp::max(
domain_partials.len(),
domain_to_match_against_partials.len(),
);
let mut ok: bool = true;
while i < l {
// Exit and return false if went out of bounds of domain to match against, and it didn't start with a dot
if domain_to_match_against_partials.len() < i + 1
&& !domain_to_match_against_starts_with_a_dot
{
ok = false;
break;
}
let mut i: usize = domain_partials.len(); let domain_partial = if domain_partials.len() < i + 1 {
let mut j: usize = domain_to_match_against_partials.len(); ""
} else {
if i >= j { domain_partials.get(i).unwrap()
while j > 0 { };
if !domain_partials let domain_to_match_against_partial = if domain_to_match_against_partials.len() < i + 1 {
.get(i - 1) ""
.unwrap() } else {
.eq_ignore_ascii_case(&domain_to_match_against_partials.get(j - 1).unwrap()) domain_to_match_against_partials.get(i).unwrap()
{ };
break;
} let parts_match = domain_to_match_against_starts_with_a_dot
|| domain_to_match_against_partial.eq_ignore_ascii_case(domain_partial);
i -= 1; if !parts_match {
j -= 1; ok = false;
break;
} }
j == 0 i += 1;
} else {
false
} }
ok
} }
pub fn indent(level: u32) -> String { pub fn indent(level: u32) -> String {

@ -25,14 +25,6 @@ mod passing {
)); ));
} }
#[test]
fn dotted_domain_is_within_domain() {
assert!(utils::domain_is_within_domain(
".ycombinator.com",
"ycombinator.com"
));
}
#[test] #[test]
fn sub_domain_is_within_dotted_domain() { fn sub_domain_is_within_dotted_domain() {
assert!(utils::domain_is_within_domain( assert!(utils::domain_is_within_domain(
@ -67,18 +59,12 @@ mod passing {
#[test] #[test]
fn domain_with_trailing_dot_is_within_single_dot() { fn domain_with_trailing_dot_is_within_single_dot() {
assert!(utils::domain_is_within_domain( assert!(utils::domain_is_within_domain("ycombinator.com.", "."));
"ycombinator.com.",
"."
));
} }
#[test] #[test]
fn domain_matches_single_dot() { fn domain_matches_single_dot() {
assert!(utils::domain_is_within_domain( assert!(utils::domain_is_within_domain("ycombinator.com", "."));
"ycombinator.com",
"."
));
} }
#[test] #[test]
@ -140,6 +126,14 @@ mod failing {
)); ));
} }
#[test]
fn dotted_domain_is_not_within_domain() {
assert!(!utils::domain_is_within_domain(
".ycombinator.com",
"ycombinator.com"
));
}
#[test] #[test]
fn no_domain_can_be_within_empty_domain() { fn no_domain_can_be_within_empty_domain() {
assert!(!utils::domain_is_within_domain("ycombinator.com", "")); assert!(!utils::domain_is_within_domain("ycombinator.com", ""));

Loading…
Cancel
Save