test(backend): cover stream_part range requests

This commit is contained in:
2026-06-07 20:20:53 +03:00
parent 4699ffe6af
commit 21d65af470
2 changed files with 111 additions and 5 deletions

View File

@@ -146,7 +146,7 @@ async fn list_files(
#[cfg(test)]
mod tests {
use axum::{Router, middleware};
use axum::{Router, http::header, middleware};
use axum_test::{
TestServer,
multipart::{MultipartForm, Part},
@@ -334,6 +334,71 @@ mod tests {
truncate(&file_repository.pool).await;
}
#[tokio::test]
#[serial_test::serial]
async fn test_stream_part_range() {
let user_repository = user_repository().await;
let token = create_test_session(&user_repository).await;
let file_repository = file_repository().await;
truncate(&file_repository.pool).await;
let server = test_server().await;
let post_res = server
.post("/api/files")
.add_header(axum::http::header::COOKIE, format!("auth-token={token}"))
.multipart(MultipartForm::new().add_part(
"file",
Part::bytes(b"0123456789".to_vec()).file_name("a.txt"),
))
.await;
let id = post_res.json::<serde_json::Value>()["id"].as_i64().unwrap();
let res = server
.get(&format!("/api/files/{id}/stream_part"))
.add_header(axum::http::header::COOKIE, format!("auth-token={token}"))
.add_header(axum::http::header::RANGE, "bytes=2-5")
.await;
res.assert_status(axum::http::StatusCode::PARTIAL_CONTENT);
assert_eq!(res.as_bytes(), b"2345".as_ref());
assert_eq!(res.header(header::CONTENT_RANGE), "bytes 2-5/10");
assert_eq!(res.header(header::CONTENT_LENGTH), "4");
assert_eq!(res.header(header::ACCEPT_RANGES), "bytes");
truncate(&file_repository.pool).await;
}
#[tokio::test]
#[serial_test::serial]
async fn test_stream_part_open_ended() {
let user_repository = user_repository().await;
let token = create_test_session(&user_repository).await;
let file_repository = file_repository().await;
truncate(&file_repository.pool).await;
let server = test_server().await;
let post_res = server
.post("/api/files")
.add_header(axum::http::header::COOKIE, format!("auth-token={token}"))
.multipart(MultipartForm::new().add_part(
"file",
Part::bytes(b"0123456789".to_vec()).file_name("a.txt"),
))
.await;
let id = post_res.json::<serde_json::Value>()["id"].as_i64().unwrap();
let res = server
.get(&format!("/api/files/{id}/stream_part"))
.add_header(axum::http::header::COOKIE, format!("auth-token={token}"))
.add_header(axum::http::header::RANGE, "bytes=0-")
.await;
res.assert_status(axum::http::StatusCode::PARTIAL_CONTENT);
assert_eq!(res.as_bytes(), b"0123456789".as_ref());
assert_eq!(res.header(header::CONTENT_RANGE), "bytes 0-9/10");
assert_eq!(res.header(header::CONTENT_LENGTH), "10");
truncate(&file_repository.pool).await;
}
#[tokio::test]
#[serial_test::serial]
async fn test_download_file_not_found() {