Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ deno.lock
/console/testdata/unicode_width_crate/target
html_cov/
cov.lcov
/http/testdata/%25A.txt
/http/testdata/file#2.txt
/http/testdata/test file.txt
43 changes: 43 additions & 0 deletions http/file_server_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,25 @@ Deno.test("serveDir() with hash symbol in filename", async () => {
await Deno.remove(filePath);
});

Deno.test("serveDir() with space in filename", async () => {
const filePath = join(testdataDir, "test file.txt");
const text = "Plain text";
await Deno.writeTextFile(filePath, text);

const req = new Request("http://localhost/test%20file.txt");
const res = await serveDir(req, serveDirOptions);
const downloadedFile = await res.text();

assertEquals(res.status, 200);
assertEquals(
res.headers.get("content-type"),
"text/plain; charset=UTF-8",
);
assertEquals(downloadedFile, text);
Comment on lines +160 to +171
Copy link
Member

Choose a reason for hiding this comment

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

Can you surround these part with try {} block and call Deno.remove(filePath) in finally {} block? That structure avoids the leak of created file even when the test failed in the middle of the body.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I instead added the file to .gitignore, along with the files from #3879. Let me know if you'd still like me to add in the try/catch blocks.

Copy link
Member

Choose a reason for hiding this comment

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

I think that works too 👍


await Deno.remove(filePath);
});

Deno.test("serveDir() serves directory index", async () => {
const filePath = join(testdataDir, "%25A.txt");
await Deno.writeTextFile(filePath, "25A");
Expand All @@ -178,6 +197,30 @@ Deno.test("serveDir() serves directory index", async () => {
await Deno.remove(filePath);
});

Deno.test("serveDir() serves directory index with file containing space in the filename", async () => {
const filePath = join(testdataDir, "test file.txt");
await Deno.writeTextFile(filePath, "25A");

const req = new Request("http://localhost/");
const res = await serveDir(req, serveDirOptions);
const page = await res.text();

assertEquals(res.status, 200);
assertStringIncludes(page, '<a href="/hello.html">hello.html</a>');
assertStringIncludes(page, '<a href="/tls/">tls/</a>');
assertStringIncludes(page, "test%20file.txt");
// `Deno.FileInfo` is not completely compatible with Windows yet
// TODO(bartlomieju): `mode` should work correctly in the future.
// Correct this test case accordingly.
if (Deno.build.os === "windows") {
assertMatch(page, /<td class="mode">(\s)*\(unknown mode\)(\s)*<\/td>/);
} else {
assertMatch(page, /<td class="mode">(\s)*[a-zA-Z- ]{14}(\s)*<\/td>/);
}

await Deno.remove(filePath);
});

Deno.test("serveDir() returns a response even if fileinfo is inaccessible", async () => {
// Note: Deno.stat for windows system files may be rejected with os error 32.
// Mock Deno.stat to test that the dirlisting page can be generated
Expand Down