Skip to content

Commit

Permalink
feat: add method to check if no_proxy is empty
Browse files Browse the repository at this point in the history
Signed-off-by: Jérémie Drouet <jeremie.drouet@datadoghq.com>
  • Loading branch information
jdrouet committed Jul 28, 2021
1 parent 06f0fa3 commit 8b91374
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ pub struct NoProxy {
impl NoProxy {
fn from_iterator<V: AsRef<str>, I: Iterator<Item = V>>(iterator: I) -> Self {
let content: HashSet<_> = iterator
.map(|item| NoProxyItem::from(item.as_ref().trim().to_string()))
.map(|item| item.as_ref().trim().to_string())
.filter(|item| !item.is_empty())
.map(NoProxyItem::from)
.collect();
let has_wildcard = content.contains(&NoProxyItem::Wildcard);
Self {
Expand Down Expand Up @@ -136,6 +138,10 @@ impl Extend<NoProxyItem> for NoProxy {
}

impl NoProxy {
pub fn is_empty(&self) -> bool {
self.content.is_empty()
}

pub fn matches(&self, input: &str) -> bool {
if self.has_wildcard {
return true;
Expand Down Expand Up @@ -183,6 +189,12 @@ mod tests {
);
}

#[test]
fn filter_empty() {
let no_proxy = NoProxy::from("");
assert!(no_proxy.is_empty());
}

#[test]
fn wildcard() {
should_match("*", "www.wikipedia.org");
Expand Down
2 changes: 2 additions & 0 deletions src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ mod tests {
// parsing a comma separated string
let result: NoProxy = serde_json::from_str(&json).unwrap();
assert_eq!(proxy, result);
assert_eq!(result.content.len(), 2);
// parsing an array of strings
let result: NoProxy = serde_json::from_str(r#"["foo.bar", "1.2.3.4"]"#).unwrap();
assert_eq!(proxy, result);
assert_eq!(result.content.len(), 2);
}
}

0 comments on commit 8b91374

Please sign in to comment.