diff --git a/tests/end-to-end.rs b/tests/end-to-end.rs index cc22d2f..9734fd4 100644 --- a/tests/end-to-end.rs +++ b/tests/end-to-end.rs @@ -10,8 +10,8 @@ use anyhow::anyhow; use anyhow::bail; use anyhow::Context as _; use anyhow::Result; - -use reqwest::Url; +use reqwest::header::USER_AGENT; +use reqwest::{Method, StatusCode, Url}; use tempfile::tempdir; use tokio::net::TcpListener; @@ -40,6 +40,20 @@ async fn get_listener_in_available_port() -> TcpListener { panic!("No port available"); } +async fn send_download_request(addr: SocketAddr, method: Method, pkg_name: &str, ver: &str)-> StatusCode { + let client = reqwest::Client::new(); + + let base_url = format!("http://{}/api/v1/crates", addr); + + client + .request(method, format!("{}/{}/{}/download", base_url, pkg_name, ver)) + .header(USER_AGENT, "cargo-upload") + .send() + .await + .expect("Failed to send request") + .status() +} + /// Append data to a file. fn append(file: &Path, data: B) -> Result<()> where @@ -147,17 +161,17 @@ where async fn serve_registry() -> (JoinHandle<()>, PathBuf, SocketAddr) { let root = tempdir().unwrap(); let path = root.path(); - let listener = get_listener_in_available_port().await; - let addr = listener.local_addr().unwrap(); + let listener = get_listener_in_available_port().await; + let addr = listener.local_addr().unwrap(); - let server = move || { - let path = path.to_owned(); - let addr = addr.clone(); - async move { serve(&path, listener, addr).await.unwrap() } - }; - let handle = spawn(server()); + let server = move || { + let path = path.to_owned(); + let addr = addr.clone(); + async move { serve(&path, listener, addr).await.unwrap() } + }; + let handle = spawn(server()); - (handle, path.to_owned(), addr) + (handle, path.to_owned(), addr) } /// Check that we can publish a crate. @@ -181,8 +195,51 @@ async fn publish() { my_lib.join("Cargo.toml").to_str().unwrap(), ], ) - .await - .unwrap(); + .await + .unwrap(); +} + +#[tokio::test] +async fn publish_and_consume_download_endpoint() { + let (_handle, _reg_root, addr) = serve_registry().await; + + let src_root = tempdir().unwrap(); + let src_root = src_root.path(); + let home = setup_cargo_home(src_root, Locator::Socket(addr)).unwrap(); + + let my_lib = src_root.join("my-lib"); + cargo_init(&home, ["--lib", my_lib.to_str().unwrap()]) + .await + .unwrap(); + + cargo_publish( + &home, + [ + "--manifest-path", + my_lib.join("Cargo.toml").to_str().unwrap(), + ], + ) + .await + .unwrap(); + + let existing_crate_and_version_status = send_download_request(addr, Method::GET, "my-lib", "0.1.0").await; + assert_eq!(existing_crate_and_version_status, 200); + + let existing_crate_and_version_status = send_download_request(addr, Method::HEAD, "my-lib", "0.1.0").await; + assert_eq!(existing_crate_and_version_status, 200); + + let existing_crate_and_missing_version_status = send_download_request(addr, Method::GET, "my-lib", "99.99.99").await; + assert_eq!(existing_crate_and_missing_version_status, 404); + + let existing_crate_and_missing_version_status = send_download_request(addr, Method::HEAD, "my-lib", "99.99.99").await; + assert_eq!(existing_crate_and_missing_version_status, 404); + + let missing_crate_and_status = send_download_request(addr, Method::GET, "ba93ba78-f47a-4a37-b25b-1c713e5d11f8", "99.99.99").await; + assert_eq!(missing_crate_and_status, 404); + + let missing_crate_and_status = send_download_request(addr, Method::HEAD, "ba93ba78-f47a-4a37-b25b-1c713e5d11f8", "99.99.99").await; + assert_eq!(missing_crate_and_status, 404); + } async fn test_publish_and_consume(registry_locator: Locator) {