Skip to content

Commit

Permalink
test(transport): add unit tests for Authorization methods (#1616)
Browse files Browse the repository at this point in the history
test(transport): add unit tests for Authorization methods
  • Loading branch information
tcoratger authored Nov 5, 2024
1 parent e5ef82e commit 0a2b334
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions crates/transport/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,57 @@ impl fmt::Display for Authorization {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use url::Url;

#[test]
fn test_extract_from_url_with_basic_auth() {
let url = Url::parse("http://username:password@domain.com").unwrap();
let auth = Authorization::extract_from_url(&url).unwrap();

// Expected Basic auth encoded in base64
assert_eq!(
auth,
Authorization::Basic(general_purpose::STANDARD.encode("username:password"))
);
}

#[test]
fn test_extract_from_url_no_auth() {
let url = Url::parse("http://domain.com").unwrap();
assert!(Authorization::extract_from_url(&url).is_none());
}

#[test]
fn test_extract_from_url_with_localhost() {
let url = Url::parse("http://localhost:password@domain.com").unwrap();
assert!(Authorization::extract_from_url(&url).is_none());
}

#[test]
fn test_extract_from_url_with_socket_address() {
let url = Url::parse("http://127.0.0.1:8080").unwrap();
assert!(Authorization::extract_from_url(&url).is_none());
}

#[test]
fn test_authority() {
let auth = Authorization::authority("user:pass");
assert_eq!(auth, Authorization::Basic(general_purpose::STANDARD.encode("user:pass")));
}

#[test]
fn test_basic() {
let auth = Authorization::basic("user", "pass");
assert_eq!(auth, Authorization::Basic(general_purpose::STANDARD.encode("user:pass")));
}

#[test]
fn test_raw() {
let auth = Authorization::raw("raw_token");
assert_eq!(auth, Authorization::Raw("raw_token".to_string()));
}
}

0 comments on commit 0a2b334

Please sign in to comment.