use actix_http::{Method, Request};
use actix_service::Service;
use actix_web::body::MessageBody;
use actix_web::dev::ServiceResponse;
use actix_web::http::header;
use actix_web::{test, Error};
mod archive_basic_test;
mod archive_multihost_test;
async fn test_archive_paths(
org_name: &str,
repo_name: &str,
file_paths: Vec<&str>,
branch_name: &str,
app: &impl Service<Request, Response = ServiceResponse<impl MessageBody>, Error = Error>,
is_success: bool,
header_name: &'static str,
header_value: &str,
) {
for file_path in file_paths {
let req = test::TestRequest::get()
.uri(&format!(
"/_archive/{}/{}?commitish={}&path={}",
org_name, repo_name, branch_name, file_path
))
.insert_header((header::HeaderName::from_static(header_name), header_value))
.to_request();
let resp = test::call_service(&app, req).await;
let actual = if is_success {
resp.status().is_success()
} else {
resp.status().is_client_error()
};
let expected = true;
assert_eq!(actual, expected);
if is_success {
let http_x_path = resp.headers().get("X-File-Path").unwrap().to_str().unwrap();
let file_path = file_path.strip_prefix('/').unwrap_or(file_path);
assert!(
http_x_path.to_string().contains(file_path),
"\"{http_x_path}\" doesn't contain {file_path}"
);
}
}
}
async fn test_archive_paths_with_head_method(
org_name: &str,
repo_name: &str,
file_paths: Vec<&str>,
branch_name: &str,
app: &impl Service<Request, Response = ServiceResponse<impl MessageBody>, Error = Error>,
is_success: bool,
) {
for file_path in file_paths {
let req = test::TestRequest::default()
.method(Method::HEAD)
.uri(&format!(
"/_archive/{}/{}?commitish={}&path={}",
org_name, repo_name, branch_name, file_path
))
.to_request();
let resp = test::call_service(&app, req).await;
let actual = if is_success {
resp.status().is_success()
} else {
resp.status().is_client_error()
};
let expected = true;
assert_eq!(actual, expected);
}
}