Skip to content

Commit

Permalink
Fix MemoryFs root request not resolving index
Browse files Browse the repository at this point in the history
  • Loading branch information
stephank committed Dec 12, 2022
1 parent de2262a commit fb277a5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
20 changes: 19 additions & 1 deletion src/vfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,29 @@ impl FileAccess for Cursor<Bytes> {
/// An in-memory virtual filesystem.
///
/// This type implements `FileOpener`, and can be directly used in `Static::with_opener`, for example.
#[derive(Default)]
pub struct MemoryFs {
files: MemoryFileMap,
}

impl Default for MemoryFs {
fn default() -> Self {
let mut files = MemoryFileMap::new();

// Create a top-level directory entry.
files.insert(
PathBuf::new(),
FileWithMetadata {
handle: Bytes::new(),
size: 0,
modified: None,
is_dir: true,
},
);

Self { files }
}
}

impl MemoryFs {
/// Initialize a `MemoryFs` from a directory.
///
Expand Down
19 changes: 15 additions & 4 deletions tests/static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,19 +465,30 @@ async fn serves_br() {

#[tokio::test]
async fn test_memory_fs() {
let dir = Harness::create_temp_dir(vec![("foobar/file1.html", "this is file1")]);
let dir = Harness::create_temp_dir(vec![
("index.html", "root index"),
("nested/index.html", "nested index"),
]);
let fs = MemoryFs::from_dir(dir.path())
.await
.expect("MemoryFs failed");
dir.close().expect("tempdir cleanup failed");

let static_ = Static::with_opener(fs);

let req = Request::builder()
.uri("/foobar/file1.html")
.uri("/")
.body(())
.expect("unable to build request");
let res = static_.clone().serve(req).await.unwrap();
assert_eq!(read_body(res).await, "root index");

let res = Static::with_opener(fs).serve(req).await.unwrap();
assert_eq!(read_body(res).await, "this is file1");
let req = Request::builder()
.uri("/nested/")
.body(())
.expect("unable to build request");
let res = static_.serve(req).await.unwrap();
assert_eq!(read_body(res).await, "nested index");
}

#[cfg(target_os = "windows")]
Expand Down

0 comments on commit fb277a5

Please sign in to comment.