Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions axum-extra/src/response/file_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ where
let metadata = file.metadata().await?;
let total_size = metadata.len();

if total_size == 0 {
return Ok((StatusCode::RANGE_NOT_SATISFIABLE, "Range Not Satisfiable").into_response());
Copy link
Contributor

@darth-raijin darth-raijin Nov 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question:
I noticed that the error body "Range Not Satisfiable" is hard-coded directly into the response. Would it make sense to extract this into a shared constant so tests can assert on the exact same value?

Having the literal inline makes tests fragile, since they need to duplicate the exact string. If the message ever changes, that could also unintentionally break consumers relying on the current wording or cause test drift.

In particular, there are two branches in this PR that both return StatusCode::RANGE_NOT_SATISFIABLE. It might be useful if the test also asserted on the error body, to ensure the StatusCode::RANGE_NOT_SATISFIABLE comes from the branch in file_stream.rs with the expected response body, and not the test branch.

}

if end == 0 {
end = total_size - 1;
}
Expand Down Expand Up @@ -596,4 +600,61 @@ mod tests {
}
Some((start, end))
}

#[tokio::test]
async fn response_range_empty_file() -> Result<(), Box<dyn std::error::Error>> {
struct TempFile(&'static str);

impl Drop for TempFile {
fn drop(&mut self) {
let _ = std::fs::remove_file(self.0);
}
}

let filename = "test_empty_file.txt";
std::fs::write(filename, []).unwrap();
let _cleanup = TempFile(filename);

let app = Router::new().route(
"/range_empty",
get(move |headers: HeaderMap| async move {
let range_header = headers
.get(header::RANGE)
.and_then(|value| value.to_str().ok());

let (start, end) = if let Some(range) = range_header {
if let Some(range) = parse_range_header(range) {
range
} else {
return (StatusCode::RANGE_NOT_SATISFIABLE, "Invalid Range")
.into_response();
}
} else {
(0, 0)
};

FileStream::<ReaderStream<File>>::try_range_response(
Path::new("test_empty_file.txt"),
start,
end,
)
.await
.unwrap_or_else(|_| StatusCode::INTERNAL_SERVER_ERROR.into_response())
}),
);

let response = app
.oneshot(
Request::builder()
.uri("/range_empty")
.header(header::RANGE, "bytes=0-")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();

assert_eq!(response.status(), StatusCode::RANGE_NOT_SATISFIABLE);
Ok(())
}
}
Loading