Skip to main content

orion_accessor/addr/
http.rs

1use crate::prelude::*;
2
3use getset::{Getters, Setters, WithSetters};
4use url::Url;
5
6#[derive(Getters, Clone, Debug, Serialize, Deserialize, WithSetters, Setters)]
7#[getset(get = "pub", set = "pub")]
8#[serde(rename = "http")]
9pub struct HttpResource {
10    url: String,
11    #[serde(skip_serializing_if = "Option::is_none")]
12    username: Option<String>,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    password: Option<String>,
15}
16
17impl PartialEq for HttpResource {
18    fn eq(&self, other: &Self) -> bool {
19        self.url == other.url && self.username == other.username && self.password == other.password
20    }
21}
22
23impl Eq for HttpResource {}
24
25impl EnvEvalable<HttpResource> for HttpResource {
26    fn env_eval(self, dict: &EnvDict) -> HttpResource {
27        Self {
28            url: self.url.env_eval(dict),
29            username: self.username.env_eval(dict),
30            password: self.password.env_eval(dict),
31        }
32    }
33}
34
35impl HttpResource {
36    pub fn from<S: Into<String>>(url: S) -> Self {
37        Self {
38            url: url.into(),
39            username: None,
40            password: None,
41        }
42    }
43
44    pub fn with_credentials<S: Into<String>>(mut self, username: S, password: S) -> Self {
45        self.username = Some(username.into());
46        self.password = Some(password.into());
47        self
48    }
49}
50
51pub fn filename_of_url(url: &str) -> Option<String> {
52    let parsed_url = Url::parse(url).ok()?;
53    parsed_url.path_segments()?.next_back().and_then(|s| {
54        if s.is_empty() {
55            None
56        } else {
57            Some(s.to_string())
58        }
59    })
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_get_filename_with_regular_url() {
68        assert_eq!(
69            filename_of_url("http://example.com/file.txt"),
70            Some("file.txt".to_string())
71        );
72    }
73
74    #[test]
75    fn test_get_filename_with_query_params() {
76        assert_eq!(
77            filename_of_url("http://example.com/file.txt?version=1.0"),
78            Some("file.txt".to_string())
79        );
80    }
81
82    #[test]
83    fn test_get_filename_with_fragment() {
84        assert_eq!(
85            filename_of_url("http://example.com/file.txt#section1"),
86            Some("file.txt".to_string())
87        );
88    }
89
90    #[test]
91    fn test_get_filename_with_multiple_path_segments() {
92        assert_eq!(
93            filename_of_url("http://example.com/path/to/file.txt"),
94            Some("file.txt".to_string())
95        );
96    }
97
98    #[test]
99    fn test_get_filename_with_trailing_slash() {
100        assert_eq!(filename_of_url("http://example.com/path/"), None);
101    }
102
103    #[test]
104    fn test_get_filename_with_empty_path() {
105        assert_eq!(filename_of_url("http://example.com"), None);
106    }
107
108    #[test]
109    fn test_get_filename_with_invalid_url() {
110        assert_eq!(filename_of_url("not a valid url"), None);
111    }
112
113    #[test]
114    fn test_get_filename_with_encoded_characters() {
115        assert_eq!(
116            filename_of_url("http://example.com/file%20name.txt"),
117            Some("file%20name.txt".to_string())
118        );
119    }
120}