Skip to content

Commit

Permalink
fix: IP - Regex lazy matchin
Browse files Browse the repository at this point in the history
Fixes #42
  • Loading branch information
mihaigalos committed May 12, 2024
1 parent 4c4f6bc commit 8e24c17
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/core/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Parser {

/// Mixes out the ip v4 into a Domain structure.
fn domain_ipv4<'a>(&self, input: &'a str) -> Option<Domain<'a>> {
let re = Regex::new(r"([0-9]+?)\.([0-9]+?)\.([0-9]+?)\.([0-9]+?)").unwrap();
let re = Regex::new(r"([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)").unwrap();
let caps = re.captures(input);
caps.as_ref()?;
return Some(Domain {
Expand Down Expand Up @@ -95,6 +95,19 @@ mod tests {

#[test]
fn test_domain_ipv4_when_typical() {
let input = "https://192.168.178.242/dir";
let expected = Domain {
subdomain: None,
domain: Some("192.168.178.242"),
top_level_domain: None,
};
let result = Parser::new(None).domain_ipv4(input).unwrap();

assert_eq!(result, expected);
}

#[test]
fn test_domain_ipv4_when_port() {
let input = "https://1.2.3.4:443/blog/article/search?docid=720&hl=en#dayone";
let expected = Domain {
subdomain: None,
Expand Down
22 changes: 22 additions & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,26 @@ mod tests {
}
);
}

#[test]
fn test_parse_works_when_ip() {
let input = "ftp://192.168.178.242/dir";
let result = Parser::new(None).parse(input).unwrap();
assert_eq!(
result,
Url {
scheme: Some("ftp".to_string()),
user_pass: (None, None),
subdomain: None,
domain: Some("192.168.178.242".to_string()),
top_level_domain: None,
port: Some(21),
path: Some(vec![
"dir".to_string(),
]),
query: None,
anchor: None,
}
);
}
}

0 comments on commit 8e24c17

Please sign in to comment.