fix: regression: redirect folders not ending with a slash

This commit is contained in:
Matteo Settenvini 2025-08-21 23:21:42 +02:00
parent 3bda00a7ae
commit 8137566988
Signed by: matteo
GPG key ID: 1C1B12600D81DE05
2 changed files with 63 additions and 71 deletions

View file

@ -8,10 +8,7 @@ use {
scraper::{Html, Selector},
};
#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn serves_files() -> anyhow::Result<()> {
let test = common::Test::new().await?;
async fn create_sample_files(test: &common::Test) -> anyhow::Result<()> {
test.bucket
.put(
&ObjectStorePath::from("file.txt"),
@ -26,6 +23,14 @@ async fn serves_files() -> anyhow::Result<()> {
)
.await?;
Ok(())
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn serves_files() -> anyhow::Result<()> {
let test = common::Test::new().await?;
create_sample_files(&test).await?;
let resp = reqwest::get(test.base_url.join("file.txt")?).await?;
assert_eq!(resp.bytes().await?, "I am a file");
@ -38,19 +43,7 @@ async fn serves_files() -> anyhow::Result<()> {
#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn serves_top_level_folder() -> anyhow::Result<()> {
let test = common::Test::new().await?;
test.bucket
.put(
&ObjectStorePath::from("file.txt"),
PutPayload::from_static("I am a file".as_bytes()),
)
.await?;
test.bucket
.put(
&ObjectStorePath::from("folder/file.txt"),
PutPayload::from_static("I am a file in a folder".as_bytes()),
)
.await?;
create_sample_files(&test).await?;
// Check that a file in the toplevel is listed:
let resp = reqwest::get(test.base_url.clone()).await?;
@ -72,8 +65,11 @@ async fn serves_top_level_folder() -> anyhow::Result<()> {
let selector =
Selector::parse(r#"table > tbody > tr:nth-child(1) > td:first-child > a"#).unwrap();
for item in document.select(&selector) {
assert_eq!(item.attr("href"), Some("folder"));
assert_eq!(item.text().next(), Some("folder"));
// Folders should be listed ending with a slash,
// or HTTP gets confused. This is also due to the
// normalization we do on the path in the main program.
assert_eq!(item.attr("href"), Some("folder/"));
assert_eq!(item.text().next(), Some("folder/"));
}
let selector =
@ -89,20 +85,7 @@ async fn serves_top_level_folder() -> anyhow::Result<()> {
#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn serves_second_level_folder() -> anyhow::Result<()> {
let test = common::Test::new().await?;
test.bucket
.put(
&ObjectStorePath::from("file.txt"),
PutPayload::from_static("I am a file".as_bytes()),
)
.await?;
test.bucket
.put(
&ObjectStorePath::from("folder/file.txt"),
PutPayload::from_static("I am a file in a folder".as_bytes()),
)
.await?;
create_sample_files(&test).await?;
// Check that a file in the second level is listed:
let resp = reqwest::get(test.base_url.join("folder/")?).await?;
@ -140,19 +123,7 @@ async fn serves_second_level_folder() -> anyhow::Result<()> {
#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn serves_second_level_folder_without_ending_slash() -> anyhow::Result<()> {
let test = common::Test::new().await?;
test.bucket
.put(
&ObjectStorePath::from("file.txt"),
PutPayload::from_static("I am a file".as_bytes()),
)
.await?;
test.bucket
.put(
&ObjectStorePath::from("folder/file.txt"),
PutPayload::from_static("I am a file in a folder".as_bytes()),
)
.await?;
create_sample_files(&test).await?;
// Check that a file in the second level is listed even without an ending slash:
let resp = reqwest::get(test.base_url.join("folder")?).await?;
@ -161,6 +132,10 @@ async fn serves_second_level_folder_without_ending_slash() -> anyhow::Result<()>
"Request failed with {}",
resp.status()
);
// Ensure we were redirected to a URL ending with a slash
assert!(resp.url().path().ends_with("/"));
let text = resp.text().await?;
println!("{}", &text);
let document = Html::parse_document(&text);