test: add ut for client-util (#826)
This commit is contained in:
parent
07591fa862
commit
344271a842
|
|
@ -79,3 +79,100 @@ impl Credentials {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use http::header::HeaderValue;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_verify_no_auth_header() {
|
||||||
|
let credentials = Credentials::new("user", "pass");
|
||||||
|
let header = HeaderMap::new();
|
||||||
|
|
||||||
|
let result = credentials.verify(&header);
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert!(matches!(result.unwrap_err(), Error::Unauthorized));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_verify_invalid_auth_type() {
|
||||||
|
let credentials = Credentials::new("user", "pass");
|
||||||
|
let mut header = HeaderMap::new();
|
||||||
|
header.insert(
|
||||||
|
header::AUTHORIZATION,
|
||||||
|
HeaderValue::from_static("Bearer some_token"),
|
||||||
|
);
|
||||||
|
|
||||||
|
let result = credentials.verify(&header);
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert!(matches!(result.unwrap_err(), Error::Unauthorized));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_verify_invalid_base64() {
|
||||||
|
let credentials = Credentials::new("user", "pass");
|
||||||
|
let mut header = HeaderMap::new();
|
||||||
|
header.insert(
|
||||||
|
header::AUTHORIZATION,
|
||||||
|
HeaderValue::from_static("Basic invalid_base64"),
|
||||||
|
);
|
||||||
|
|
||||||
|
let result = credentials.verify(&header);
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(
|
||||||
|
format!("{}", result.err().unwrap()),
|
||||||
|
format!(
|
||||||
|
"{:?} cause: Invalid symbol 95, offset 7.",
|
||||||
|
ErrorType::ParseError
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_verify_invalid_format() {
|
||||||
|
let credentials = Credentials::new("user", "pass");
|
||||||
|
let mut header = HeaderMap::new();
|
||||||
|
header.insert(
|
||||||
|
header::AUTHORIZATION,
|
||||||
|
HeaderValue::from_static("Basic dXNlcg=="), // "user" in Base64
|
||||||
|
);
|
||||||
|
|
||||||
|
let result = credentials.verify(&header);
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert!(matches!(result.unwrap_err(), Error::Unauthorized));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_verify_incorrect_credentials() {
|
||||||
|
let credentials = Credentials::new("user", "pass");
|
||||||
|
let mut header = HeaderMap::new();
|
||||||
|
header.insert(
|
||||||
|
header::AUTHORIZATION,
|
||||||
|
HeaderValue::from_static("Basic dXNlcjpwYXNzX2Vycm9y"), // "user:pass_error" in Base64
|
||||||
|
);
|
||||||
|
|
||||||
|
let result = credentials.verify(&header);
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert!(matches!(result.unwrap_err(), Error::Unauthorized));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_verify_correct_credentials() {
|
||||||
|
let credentials = Credentials::new("user", "pass");
|
||||||
|
let mut header = HeaderMap::new();
|
||||||
|
header.insert(
|
||||||
|
header::AUTHORIZATION,
|
||||||
|
HeaderValue::from_static("Basic dXNlcjpwYXNz"), // "user:pass" in Base64
|
||||||
|
);
|
||||||
|
|
||||||
|
let result = credentials.verify(&header);
|
||||||
|
|
||||||
|
assert!(result.is_ok());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,10 +51,8 @@ pub fn hashmap_to_reqwest_headermap(
|
||||||
|
|
||||||
/// hashmap_to_hyper_header_map converts a hashmap to a hyper header map.
|
/// hashmap_to_hyper_header_map converts a hashmap to a hyper header map.
|
||||||
#[instrument(skip_all)]
|
#[instrument(skip_all)]
|
||||||
pub fn hashmap_to_hyper_header_map(
|
pub fn hashmap_to_hyper_header_map(header: &HashMap<String, String>) -> Result<HeaderMap> {
|
||||||
header: &HashMap<String, String>,
|
let header: HeaderMap = (header).try_into().or_err(ErrorType::ParseError)?;
|
||||||
) -> Result<hyper::header::HeaderMap> {
|
|
||||||
let header: hyper::header::HeaderMap = (header).try_into().or_err(ErrorType::ParseError)?;
|
|
||||||
Ok(header)
|
Ok(header)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,10 +61,8 @@ pub fn hashmap_to_hyper_header_map(
|
||||||
/// version is different. Reqwest header depends on the http crate
|
/// version is different. Reqwest header depends on the http crate
|
||||||
/// version 0.2, but the Hyper header depends on the http crate version 0.1.
|
/// version 0.2, but the Hyper header depends on the http crate version 0.1.
|
||||||
#[instrument(skip_all)]
|
#[instrument(skip_all)]
|
||||||
pub fn hyper_headermap_to_reqwest_headermap(
|
pub fn hyper_headermap_to_reqwest_headermap(hyper_header: &HeaderMap) -> HeaderMap {
|
||||||
hyper_header: &hyper::header::HeaderMap,
|
let mut reqwest_header = HeaderMap::new();
|
||||||
) -> reqwest::header::HeaderMap {
|
|
||||||
let mut reqwest_header = reqwest::header::HeaderMap::new();
|
|
||||||
for (hyper_header_key, hyper_header_value) in hyper_header.iter() {
|
for (hyper_header_key, hyper_header_value) in hyper_header.iter() {
|
||||||
let reqwest_header_name: reqwest::header::HeaderName =
|
let reqwest_header_name: reqwest::header::HeaderName =
|
||||||
match hyper_header_key.to_string().parse() {
|
match hyper_header_key.to_string().parse() {
|
||||||
|
|
@ -113,9 +109,7 @@ pub fn header_vec_to_hashmap(raw_header: Vec<String>) -> Result<HashMap<String,
|
||||||
|
|
||||||
/// header_vec_to_reqwest_headermap converts a vector of header string to a reqwest headermap.
|
/// header_vec_to_reqwest_headermap converts a vector of header string to a reqwest headermap.
|
||||||
#[instrument(skip_all)]
|
#[instrument(skip_all)]
|
||||||
pub fn header_vec_to_reqwest_headermap(
|
pub fn header_vec_to_reqwest_headermap(raw_header: Vec<String>) -> Result<HeaderMap> {
|
||||||
raw_header: Vec<String>,
|
|
||||||
) -> Result<reqwest::header::HeaderMap> {
|
|
||||||
hashmap_to_reqwest_headermap(&header_vec_to_hashmap(raw_header)?)
|
hashmap_to_reqwest_headermap(&header_vec_to_hashmap(raw_header)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -151,3 +145,109 @@ pub fn parse_range_header(range_header_value: &str, content_length: u64) -> Resu
|
||||||
let length = valid_range.end() - start + 1;
|
let length = valid_range.end() - start + 1;
|
||||||
Ok(Range { start, length })
|
Ok(Range { start, length })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use reqwest::header::{HeaderMap, HeaderValue};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_reqwest_headermap_to_hashmap() {
|
||||||
|
let mut header = HeaderMap::new();
|
||||||
|
header.insert("Content-Type", HeaderValue::from_static("application/json"));
|
||||||
|
header.insert("Authorization", HeaderValue::from_static("Bearer token"));
|
||||||
|
|
||||||
|
let hashmap = reqwest_headermap_to_hashmap(&header);
|
||||||
|
|
||||||
|
assert_eq!(hashmap.get("content-type").unwrap(), "application/json");
|
||||||
|
assert_eq!(hashmap.get("authorization").unwrap(), "Bearer token");
|
||||||
|
assert_eq!(hashmap.get("foo"), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_hashmap_to_reqwest_headermap() {
|
||||||
|
let mut hashmap = HashMap::new();
|
||||||
|
hashmap.insert("Content-Type".to_string(), "application/json".to_string());
|
||||||
|
hashmap.insert("Authorization".to_string(), "Bearer token".to_string());
|
||||||
|
|
||||||
|
let header = hashmap_to_reqwest_headermap(&hashmap).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(header.get("Content-Type").unwrap(), "application/json");
|
||||||
|
assert_eq!(header.get("Authorization").unwrap(), "Bearer token");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_hashmap_to_hyper_header_map() {
|
||||||
|
let mut hashmap = HashMap::new();
|
||||||
|
hashmap.insert("Content-Type".to_string(), "application/json".to_string());
|
||||||
|
hashmap.insert("Authorization".to_string(), "Bearer token".to_string());
|
||||||
|
|
||||||
|
let header = hashmap_to_hyper_header_map(&hashmap).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(header.get("Content-Type").unwrap(), "application/json");
|
||||||
|
assert_eq!(header.get("Authorization").unwrap(), "Bearer token");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_hyper_headermap_to_reqwest_headermap() {
|
||||||
|
let mut hyper_header = HeaderMap::new();
|
||||||
|
hyper_header.insert("Content-Type", HeaderValue::from_static("application/json"));
|
||||||
|
hyper_header.insert("Authorization", HeaderValue::from_static("Bearer token"));
|
||||||
|
|
||||||
|
let reqwest_header = hyper_headermap_to_reqwest_headermap(&hyper_header);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
reqwest_header.get("Content-Type").unwrap(),
|
||||||
|
"application/json"
|
||||||
|
);
|
||||||
|
assert_eq!(reqwest_header.get("Authorization").unwrap(), "Bearer token");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_header_vec_to_hashmap() {
|
||||||
|
let raw_header = vec![
|
||||||
|
"Content-Type: application/json".to_string(),
|
||||||
|
"Authorization: Bearer token".to_string(),
|
||||||
|
];
|
||||||
|
|
||||||
|
let hashmap = header_vec_to_hashmap(raw_header).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(hashmap.get("Content-Type").unwrap(), "application/json");
|
||||||
|
assert_eq!(hashmap.get("Authorization").unwrap(), "Bearer token");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_header_vec_to_reqwest_headermap() {
|
||||||
|
let raw_header = vec![
|
||||||
|
"Content-Type: application/json".to_string(),
|
||||||
|
"Authorization: Bearer token".to_string(),
|
||||||
|
];
|
||||||
|
|
||||||
|
let header = header_vec_to_reqwest_headermap(raw_header).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(header.get("Content-Type").unwrap(), "application/json");
|
||||||
|
assert_eq!(header.get("Authorization").unwrap(), "Bearer token");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_range() {
|
||||||
|
let mut header = HeaderMap::new();
|
||||||
|
header.insert(
|
||||||
|
reqwest::header::RANGE,
|
||||||
|
HeaderValue::from_static("bytes=0-100"),
|
||||||
|
);
|
||||||
|
|
||||||
|
let range = get_range(&header, 200).unwrap().unwrap();
|
||||||
|
|
||||||
|
assert_eq!(range.start, 0);
|
||||||
|
assert_eq!(range.length, 101);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_range_header() {
|
||||||
|
let range = parse_range_header("bytes=0-100", 200).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(range.start, 0);
|
||||||
|
assert_eq!(range.length, 101);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue