use crate::Error;
use crate::Error::UserInfoNotSupported;
use crate::parse::is_authority_end;
pub fn check_no_user_info(s: &str) -> Result<(), Error> {
let end: usize = s
.as_bytes()
.iter()
.position(|c| is_authority_end(*c))
.unwrap_or(s.len());
if s.as_bytes()[..end].contains(&b'@') {
Err(UserInfoNotSupported)
} else {
Ok(())
}
}
#[cfg(test)]
mod tests {
use crate::Error;
use crate::Error::UserInfoNotSupported;
use crate::parse::check_no_user_info;
#[test]
fn fn_check_no_user_info() {
let test_cases: &[(&str, Result<(), Error>)] = &[
("", Ok(())),
("host", Ok(())),
("host/", Ok(())),
("host:80/path", Ok(())),
("[::1]:80/path", Ok(())),
("host/a@b", Ok(())),
("host/p?a@b", Ok(())),
("host/p#a@b", Ok(())),
("host?a@b", Ok(())),
("host#a@b", Ok(())),
("user@host", Err(UserInfoNotSupported)),
("user:pass@host", Err(UserInfoNotSupported)),
("user:@host", Err(UserInfoNotSupported)),
(":pass@host", Err(UserInfoNotSupported)),
("@host", Err(UserInfoNotSupported)),
("user@host/path", Err(UserInfoNotSupported)),
("user:pass@host:8080/path", Err(UserInfoNotSupported)),
("user@host?query", Err(UserInfoNotSupported)),
("user@host#frag", Err(UserInfoNotSupported)),
("a@b@host", Err(UserInfoNotSupported)),
("user@[::1]:80/p", Err(UserInfoNotSupported)),
];
for (s, expected) in test_cases {
let result: Result<(), Error> = check_no_user_info(s);
assert_eq!(result, *expected, "s={}", s);
}
}
}