|
| 1 | +use std::str::FromStr; |
| 2 | + |
| 3 | +use thiserror::Error; |
| 4 | +use url::Url; |
| 5 | + |
| 6 | +#[derive(Clone)] |
| 7 | +pub struct ConnectionInfo { |
| 8 | + pub origin: Origin, |
| 9 | + pub api_token: Option<String>, |
| 10 | +} |
| 11 | + |
| 12 | +impl ConnectionInfo { |
| 13 | + #[must_use] |
| 14 | + pub fn authenticated(origin: Origin, api_token: &str) -> Self { |
| 15 | + Self { |
| 16 | + origin, |
| 17 | + api_token: Some(api_token.to_string()), |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + #[must_use] |
| 22 | + pub fn anonymous(origin: Origin) -> Self { |
| 23 | + Self { origin, api_token: None } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/// Represents the origin of a HTTP request. |
| 28 | +/// |
| 29 | +/// The format of the origin is a URL, but only the scheme, host, and port are used. |
| 30 | +/// |
| 31 | +/// Pattern: `scheme://host:port/` |
| 32 | +#[derive(Debug, Clone)] |
| 33 | +pub struct Origin { |
| 34 | + url: Url, |
| 35 | +} |
| 36 | + |
| 37 | +#[derive(Debug, Error)] |
| 38 | +pub enum OriginError { |
| 39 | + #[error("Invalid URL: {0}")] |
| 40 | + InvalidUrl(#[from] url::ParseError), |
| 41 | + |
| 42 | + #[error("URL is missing scheme or host")] |
| 43 | + InvalidOrigin, |
| 44 | + |
| 45 | + #[error("Invalid URL scheme, only http and https are supported")] |
| 46 | + InvalidScheme, |
| 47 | +} |
| 48 | + |
| 49 | +impl FromStr for Origin { |
| 50 | + type Err = OriginError; |
| 51 | + |
| 52 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 53 | + let mut url = Url::parse(s).map_err(OriginError::InvalidUrl)?; |
| 54 | + |
| 55 | + // Ensure the URL has a scheme and host |
| 56 | + if url.scheme().is_empty() || url.host().is_none() { |
| 57 | + return Err(OriginError::InvalidOrigin); |
| 58 | + } |
| 59 | + |
| 60 | + if url.scheme() != "http" && url.scheme() != "https" { |
| 61 | + return Err(OriginError::InvalidScheme); |
| 62 | + } |
| 63 | + |
| 64 | + // Retain only the origin components |
| 65 | + url.set_path("/"); |
| 66 | + url.set_query(None); |
| 67 | + url.set_fragment(None); |
| 68 | + |
| 69 | + Ok(Origin { url }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl std::fmt::Display for Origin { |
| 74 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 75 | + write!(f, "{}", self.url) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl Origin { |
| 80 | + /// # Errors |
| 81 | + /// |
| 82 | + /// Will return an error if the string is not a valid URL containing a |
| 83 | + /// scheme and host. |
| 84 | + pub fn new(s: &str) -> Result<Self, OriginError> { |
| 85 | + s.parse() |
| 86 | + } |
| 87 | + |
| 88 | + #[must_use] |
| 89 | + pub fn url(&self) -> &Url { |
| 90 | + &self.url |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +#[cfg(test)] |
| 95 | +mod tests { |
| 96 | + mod origin { |
| 97 | + use crate::connection_info::Origin; |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn should_be_parsed_from_a_string_representing_a_url() { |
| 101 | + let origin = Origin::new("https://example.com:8080/path?query#fragment").unwrap(); |
| 102 | + |
| 103 | + assert_eq!(origin.to_string(), "https://example.com:8080/"); |
| 104 | + } |
| 105 | + |
| 106 | + mod when_parsing_from_url_string { |
| 107 | + use crate::connection_info::Origin; |
| 108 | + |
| 109 | + #[test] |
| 110 | + fn should_ignore_default_ports() { |
| 111 | + let origin = Origin::new("http://example.com:80").unwrap(); // DevSkim: ignore DS137138 |
| 112 | + assert_eq!(origin.to_string(), "http://example.com/"); // DevSkim: ignore DS137138 |
| 113 | + |
| 114 | + let origin = Origin::new("https://example.com:443").unwrap(); |
| 115 | + assert_eq!(origin.to_string(), "https://example.com/"); |
| 116 | + } |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn should_add_the_slash_after_the_host() { |
| 120 | + let origin = Origin::new("https://example.com:1212").unwrap(); |
| 121 | + |
| 122 | + assert_eq!(origin.to_string(), "https://example.com:1212/"); |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn should_remove_extra_path_and_query_parameters() { |
| 127 | + let origin = Origin::new("https://example.com:1212/path/to/resource?query=1#fragment").unwrap(); |
| 128 | + |
| 129 | + assert_eq!(origin.to_string(), "https://example.com:1212/"); |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn should_fail_when_the_scheme_is_missing() { |
| 134 | + let result = Origin::new("example.com"); |
| 135 | + |
| 136 | + assert!(result.is_err()); |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn should_fail_when_the_scheme_is_not_supported() { |
| 141 | + let result = Origin::new("udp://example.com"); |
| 142 | + |
| 143 | + assert!(result.is_err()); |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn should_fail_when_the_host_is_missing() { |
| 148 | + let result = Origin::new("http://"); |
| 149 | + |
| 150 | + assert!(result.is_err()); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments