4065: Use url crate port_or_known_default to allow parsing default ports (#4087)

This commit is contained in:
LivingLimes 2024-05-28 20:05:48 +10:00 committed by GitHub
parent bf702b0d67
commit 2b223d45ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -92,7 +92,7 @@ pub mod url {
pub fn port((string,): (String,)) -> Result<Value, Error> { pub fn port((string,): (String,)) -> Result<Value, Error> {
// Parse the URL // Parse the URL
match Url::parse(&string) { match Url::parse(&string) {
Ok(v) => match v.port() { Ok(v) => match v.port_or_known_default() {
Some(v) => Ok(v.into()), Some(v) => Ok(v.into()),
None => Ok(Value::None), None => Ok(Value::None),
}, },
@ -118,4 +118,33 @@ pub mod url {
Err(_) => Ok(Value::None), Err(_) => Ok(Value::None),
} }
} }
#[cfg(test)]
mod tests {
use crate::sql::value::Value;
#[test]
fn port_default_port_specified() {
let value = super::port(("http://www.google.com:80".to_string(),)).unwrap();
assert_eq!(value, 80.into());
}
#[test]
fn port_nondefault_port_specified() {
let value = super::port(("http://www.google.com:8080".to_string(),)).unwrap();
assert_eq!(value, 8080.into());
}
#[test]
fn port_no_port_specified() {
let value = super::port(("http://www.google.com".to_string(),)).unwrap();
assert_eq!(value, 80.into());
}
#[test]
fn port_no_scheme_no_port_specified() {
let value = super::port(("www.google.com".to_string(),)).unwrap();
assert_eq!(value, Value::None);
}
}
} }