chore(http): add test for file containing space in name (#3892)

* chore: add test for file containing space in name

* tweak

* tweak
This commit is contained in:
Asher Gomez 2023-12-04 17:00:44 +11:00 committed by GitHub
parent 1dc2369865
commit ea64542e54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 0 deletions

3
.gitignore vendored
View File

@ -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

View File

@ -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);
await Deno.remove(filePath);
});
Deno.test("serveDir() serves directory index", async () => {
const filePath = join(testdataDir, "%25A.txt");
await Deno.writeTextFile(filePath, "25A");
@ -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