use bytes::Bytes;
use std::time::Duration;
use crate::mail::attachment::*;
#[test]
fn names_and_content_types_default_from_the_path_or_url() {
assert_eq!(Attachment::from_storage("invoices/2026/42.pdf").file_name(), "42.pdf");
assert_eq!(
Attachment::from_storage("invoices/2026/42.pdf").mime_type(),
"application/pdf"
);
assert_eq!(
Attachment::from_url("https://cdn.example.com/a/brochure.pdf?v=2#page=1").file_name(),
"brochure.pdf"
);
assert_eq!(
Attachment::from_url("https://cdn.example.com/").file_name(),
"attachment"
);
assert_eq!(Attachment::from_path("resources/terms.pdf").file_name(), "terms.pdf");
let renamed = Attachment::from_bytes("a,b", "data").name("report.csv");
assert_eq!((renamed.file_name(), renamed.mime_type()), ("report.csv", "text/csv"));
assert_eq!(
Attachment::from_bytes("", "blob").mime_type(),
"application/octet-stream"
);
}
#[tokio::test]
async fn downloads_stop_once_the_body_passes_the_limit() {
let chunks = futures_util::stream::repeat_with(|| Ok::<_, std::io::Error>(Bytes::from(vec![0_u8; 64 * 1024])));
let server = axum::Router::new().fallback(move || async move { axum::body::Body::from_stream(chunks) });
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = format!("http://{}/endless.bin", listener.local_addr().unwrap());
tokio::spawn(async move { axum::serve(listener, server).await.unwrap() });
let app = crate::Application::configure()
.replace_env(crate::config::Env::default())
.create();
let download = app.scope(async || Attachment::from_url(url).bytes().await);
let error = tokio::time::timeout(Duration::from_secs(5), download)
.await
.expect("the download stops at the limit")
.unwrap_err();
assert_eq!(error.status(), 413, "{error}");
}