summaryrefslogtreecommitdiff
path: root/std/http/file_server_test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'std/http/file_server_test.ts')
-rw-r--r--std/http/file_server_test.ts49
1 files changed, 29 insertions, 20 deletions
diff --git a/std/http/file_server_test.ts b/std/http/file_server_test.ts
index 818fd5ecb..13205bb01 100644
--- a/std/http/file_server_test.ts
+++ b/std/http/file_server_test.ts
@@ -4,10 +4,14 @@ import { BufReader } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
import { ServerRequest } from "./server.ts";
import { serveFile, FileServerArgs } from "./file_server.ts";
+import { resolve, dirname, join, fromFileUrl } from "../path/mod.ts";
let fileServer: Deno.Process<Deno.RunOptions & { stdout: "piped" }>;
type FileServerCfg = Omit<FileServerArgs, "_"> & { target?: string };
+const moduleDir = dirname(fromFileUrl(import.meta.url));
+const testdataDir = resolve(moduleDir, "testdata");
+
async function startFileServer({
target = ".",
port = 4507,
@@ -19,13 +23,14 @@ async function startFileServer({
"run",
"--allow-read",
"--allow-net",
- "http/file_server.ts",
+ "file_server.ts",
target,
"--cors",
"-p",
`${port}`,
`${dirListing ? "" : "--no-dir-listing"}`,
],
+ cwd: moduleDir,
stdout: "piped",
stderr: "null",
});
@@ -43,8 +48,9 @@ async function startFileServerAsLibrary({}: FileServerCfg = {}): Promise<void> {
"run",
"--allow-read",
"--allow-net",
- "http/testdata/file_server_as_library.ts",
+ "testdata/file_server_as_library.ts",
],
+ cwd: moduleDir,
stdout: "piped",
stderr: "null",
});
@@ -67,7 +73,7 @@ async function killFileServer(): Promise<void> {
}
Deno.test(
- "file_server serveFile in ./",
+ "file_server serveFile",
async (): Promise<void> => {
await startFileServer();
try {
@@ -77,7 +83,7 @@ Deno.test(
assertEquals(res.headers.get("content-type"), "text/markdown");
const downloadedFile = await res.text();
const localFile = new TextDecoder().decode(
- await Deno.readFile("README.md"),
+ await Deno.readFile(join(moduleDir, "README.md")),
);
assertEquals(downloadedFile, localFile);
} finally {
@@ -87,17 +93,17 @@ Deno.test(
);
Deno.test(
- "file_server serveFile in ./http",
+ "file_server serveFile in testdata",
async (): Promise<void> => {
- await startFileServer({ target: "./http" });
+ await startFileServer({ target: "./testdata" });
try {
- const res = await fetch("http://localhost:4507/README.md");
+ const res = await fetch("http://localhost:4507/hello.html");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
- assertEquals(res.headers.get("content-type"), "text/markdown");
+ assertEquals(res.headers.get("content-type"), "text/html");
const downloadedFile = await res.text();
const localFile = new TextDecoder().decode(
- await Deno.readFile("./http/README.md"),
+ await Deno.readFile(join(testdataDir, "hello.html")),
);
assertEquals(downloadedFile, localFile);
} finally {
@@ -144,12 +150,12 @@ Deno.test("serveFallback", async function (): Promise<void> {
Deno.test("serveWithUnorthodoxFilename", async function (): Promise<void> {
await startFileServer();
try {
- let res = await fetch("http://localhost:4507/http/testdata/%");
+ let res = await fetch("http://localhost:4507/testdata/%");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assertEquals(res.status, 200);
let _ = await res.text();
- res = await fetch("http://localhost:4507/http/testdata/test%20file.txt");
+ res = await fetch("http://localhost:4507/testdata/test%20file.txt");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assertEquals(res.status, 200);
@@ -167,9 +173,10 @@ Deno.test("printHelp", async function (): Promise<void> {
// TODO(ry) It ought to be possible to get the help output without
// --allow-read.
"--allow-read",
- "http/file_server.ts",
+ "file_server.ts",
"--help",
],
+ cwd: moduleDir,
stdout: "piped",
});
assert(helpProcess.stdout != null);
@@ -182,7 +189,7 @@ Deno.test("printHelp", async function (): Promise<void> {
Deno.test("contentType", async () => {
const request = new ServerRequest();
- const response = await serveFile(request, "http/testdata/hello.html");
+ const response = await serveFile(request, join(testdataDir, "hello.html"));
const contentType = response.headers!.get("content-type");
assertEquals(contentType, "text/html");
(response.body as Deno.File).close();
@@ -208,18 +215,19 @@ async function startTlsFileServer({
"run",
"--allow-read",
"--allow-net",
- "http/file_server.ts",
+ "file_server.ts",
target,
"--host",
"localhost",
"--cert",
- "./http/testdata/tls/localhost.crt",
+ "./testdata/tls/localhost.crt",
"--key",
- "./http/testdata/tls/localhost.key",
+ "./testdata/tls/localhost.key",
"--cors",
"-p",
`${port}`,
],
+ cwd: moduleDir,
stdout: "piped",
stderr: "null",
});
@@ -237,12 +245,12 @@ Deno.test("serveDirectory TLS", async function (): Promise<void> {
const conn = await Deno.connectTls({
hostname: "localhost",
port: 4577,
- certFile: "./http/testdata/tls/RootCA.pem",
+ certFile: join(testdataDir, "tls/RootCA.pem"),
});
await Deno.writeAll(
conn,
- new TextEncoder().encode("GET /http HTTP/1.0\r\n\r\n"),
+ new TextEncoder().encode("GET / HTTP/1.0\r\n\r\n"),
);
const res = new Uint8Array(128 * 1024);
const nread = await conn.read(res);
@@ -262,15 +270,16 @@ Deno.test("partial TLS arguments fail", async function (): Promise<void> {
"run",
"--allow-read",
"--allow-net",
- "http/file_server.ts",
+ "file_server.ts",
".",
"--host",
"localhost",
"--cert",
- "./http/testdata/tls/localhost.crt",
+ "./testdata/tls/localhost.crt",
"-p",
`4578`,
],
+ cwd: moduleDir,
stdout: "piped",
stderr: "null",
});