2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-05-04 14:23:06 -04:00
|
|
|
import { assert, assertEquals } from "../testing/asserts.ts";
|
2019-07-07 15:20:41 -04:00
|
|
|
import { BufReader } from "../io/bufio.ts";
|
2019-01-17 13:08:59 -05:00
|
|
|
import { TextProtoReader } from "../textproto/mod.ts";
|
2020-04-01 12:51:01 -04:00
|
|
|
import { ServerRequest } from "./server.ts";
|
|
|
|
import { serveFile } from "./file_server.ts";
|
2020-02-11 11:24:27 -05:00
|
|
|
const { test } = Deno;
|
2020-06-09 07:18:18 -04:00
|
|
|
let fileServer: Deno.Process<Deno.RunOptions & { stdout: "piped" }>;
|
2019-05-30 08:59:30 -04:00
|
|
|
|
2020-05-21 13:55:18 -04:00
|
|
|
type FileServerCfg = {
|
|
|
|
target?: string;
|
|
|
|
port?: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
async function startFileServer({
|
|
|
|
target = ".",
|
|
|
|
port = 4507,
|
|
|
|
}: FileServerCfg = {}): Promise<void> {
|
2019-10-09 17:22:22 -04:00
|
|
|
fileServer = Deno.run({
|
2020-03-21 17:44:18 -04:00
|
|
|
cmd: [
|
2019-08-13 20:03:29 -04:00
|
|
|
Deno.execPath(),
|
2019-05-04 11:33:50 -04:00
|
|
|
"run",
|
2019-02-09 15:41:05 -05:00
|
|
|
"--allow-read",
|
|
|
|
"--allow-net",
|
|
|
|
"http/file_server.ts",
|
2020-05-21 13:55:18 -04:00
|
|
|
target,
|
2020-03-28 13:03:49 -04:00
|
|
|
"--cors",
|
2020-05-21 13:55:18 -04:00
|
|
|
"-p",
|
|
|
|
`${port}`,
|
2019-02-09 15:41:05 -05:00
|
|
|
],
|
2020-02-27 15:12:04 -05:00
|
|
|
stdout: "piped",
|
2020-03-28 13:03:49 -04:00
|
|
|
stderr: "null",
|
2019-01-17 13:08:59 -05:00
|
|
|
});
|
|
|
|
// Once fileServer is ready it will write to its stdout.
|
2020-02-07 02:23:38 -05:00
|
|
|
assert(fileServer.stdout != null);
|
|
|
|
const r = new TextProtoReader(new BufReader(fileServer.stdout));
|
2019-05-23 22:04:06 -04:00
|
|
|
const s = await r.readLine();
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(s !== null && s.includes("server listening"));
|
2019-01-17 13:08:59 -05:00
|
|
|
}
|
2019-05-23 22:04:06 -04:00
|
|
|
|
2020-06-03 13:48:03 -04:00
|
|
|
async function startFileServerAsLibrary({}: FileServerCfg = {}): Promise<void> {
|
|
|
|
fileServer = await Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"run",
|
|
|
|
"--allow-read",
|
|
|
|
"--allow-net",
|
|
|
|
"http/testdata/file_server_as_library.ts",
|
|
|
|
],
|
|
|
|
stdout: "piped",
|
|
|
|
stderr: "null",
|
|
|
|
});
|
|
|
|
assert(fileServer.stdout != null);
|
|
|
|
const r = new TextProtoReader(new BufReader(fileServer.stdout));
|
|
|
|
const s = await r.readLine();
|
|
|
|
assert(s !== null && s.includes("Server running..."));
|
|
|
|
}
|
|
|
|
|
2020-05-14 00:49:45 -04:00
|
|
|
async function killFileServer(): Promise<void> {
|
2019-01-17 13:08:59 -05:00
|
|
|
fileServer.close();
|
2020-05-14 00:49:45 -04:00
|
|
|
// Process.close() kills the file server process. However this termination
|
|
|
|
// happens asynchronously, and since we've just closed the process resource,
|
|
|
|
// we can't use `await fileServer.status()` to wait for the process to have
|
|
|
|
// exited. As a workaround, wait for its stdout to close instead.
|
|
|
|
// TODO(piscisaureus): when `Process.kill()` is stable and works on Windows,
|
|
|
|
// switch to calling `kill()` followed by `await fileServer.status()`.
|
|
|
|
await Deno.readAll(fileServer.stdout!);
|
|
|
|
fileServer.stdout!.close();
|
2018-12-11 17:56:32 -05:00
|
|
|
}
|
|
|
|
|
2020-05-21 13:55:18 -04:00
|
|
|
test("file_server serveFile in ./", async (): Promise<void> => {
|
2019-01-17 13:08:59 -05:00
|
|
|
await startFileServer();
|
|
|
|
try {
|
2020-05-11 17:33:36 -04:00
|
|
|
const res = await fetch("http://localhost:4507/README.md");
|
2018-12-23 22:50:49 -05:00
|
|
|
assert(res.headers.has("access-control-allow-origin"));
|
|
|
|
assert(res.headers.has("access-control-allow-headers"));
|
2020-04-03 12:11:52 -04:00
|
|
|
assertEquals(res.headers.get("content-type"), "text/markdown");
|
2018-12-11 17:56:32 -05:00
|
|
|
const downloadedFile = await res.text();
|
2019-01-06 14:19:15 -05:00
|
|
|
const localFile = new TextDecoder().decode(
|
2019-11-04 18:13:28 -05:00
|
|
|
await Deno.readFile("README.md")
|
2019-01-06 14:19:15 -05:00
|
|
|
);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(downloadedFile, localFile);
|
2019-01-17 13:08:59 -05:00
|
|
|
} finally {
|
2020-05-14 00:49:45 -04:00
|
|
|
await killFileServer();
|
2019-01-17 13:08:59 -05:00
|
|
|
}
|
|
|
|
});
|
2018-12-11 17:56:32 -05:00
|
|
|
|
2020-05-21 13:55:18 -04:00
|
|
|
test("file_server serveFile in ./http", async (): Promise<void> => {
|
|
|
|
await startFileServer({ target: "./http" });
|
|
|
|
try {
|
|
|
|
const res = await fetch("http://localhost:4507/README.md");
|
|
|
|
assert(res.headers.has("access-control-allow-origin"));
|
|
|
|
assert(res.headers.has("access-control-allow-headers"));
|
|
|
|
assertEquals(res.headers.get("content-type"), "text/markdown");
|
|
|
|
const downloadedFile = await res.text();
|
|
|
|
const localFile = new TextDecoder().decode(
|
|
|
|
await Deno.readFile("./http/README.md")
|
|
|
|
);
|
|
|
|
console.log(downloadedFile, localFile);
|
|
|
|
assertEquals(downloadedFile, localFile);
|
|
|
|
} finally {
|
|
|
|
await killFileServer();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
test("serveDirectory", async function (): Promise<void> {
|
2019-01-17 13:08:59 -05:00
|
|
|
await startFileServer();
|
|
|
|
try {
|
2020-05-11 17:33:36 -04:00
|
|
|
const res = await fetch("http://localhost:4507/");
|
2018-12-23 22:50:49 -05:00
|
|
|
assert(res.headers.has("access-control-allow-origin"));
|
|
|
|
assert(res.headers.has("access-control-allow-headers"));
|
2018-12-11 17:56:32 -05:00
|
|
|
const page = await res.text();
|
2019-11-04 18:13:28 -05:00
|
|
|
assert(page.includes("README.md"));
|
2019-05-22 18:58:20 -04:00
|
|
|
|
|
|
|
// `Deno.FileInfo` is not completely compatible with Windows yet
|
2019-06-19 00:22:01 -04:00
|
|
|
// TODO: `mode` should work correctly in the future.
|
|
|
|
// Correct this test case accordingly.
|
2020-04-28 12:35:23 -04:00
|
|
|
Deno.build.os !== "windows" &&
|
2019-12-02 19:14:25 -05:00
|
|
|
assert(/<td class="mode">(\s)*\([a-zA-Z-]{10}\)(\s)*<\/td>/.test(page));
|
2020-04-28 12:35:23 -04:00
|
|
|
Deno.build.os === "windows" &&
|
2019-12-02 19:14:25 -05:00
|
|
|
assert(/<td class="mode">(\s)*\(unknown mode\)(\s)*<\/td>/.test(page));
|
|
|
|
assert(page.includes(`<a href="/README.md">README.md</a>`));
|
2019-01-17 13:08:59 -05:00
|
|
|
} finally {
|
2020-05-14 00:49:45 -04:00
|
|
|
await killFileServer();
|
2019-01-17 13:08:59 -05:00
|
|
|
}
|
|
|
|
});
|
2018-12-11 17:56:32 -05:00
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
test("serveFallback", async function (): Promise<void> {
|
2019-01-17 13:08:59 -05:00
|
|
|
await startFileServer();
|
|
|
|
try {
|
2020-05-11 17:33:36 -04:00
|
|
|
const res = await fetch("http://localhost:4507/badfile.txt");
|
2018-12-23 22:50:49 -05:00
|
|
|
assert(res.headers.has("access-control-allow-origin"));
|
|
|
|
assert(res.headers.has("access-control-allow-headers"));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(res.status, 404);
|
2020-04-10 09:51:17 -04:00
|
|
|
const _ = await res.text();
|
2019-01-17 13:08:59 -05:00
|
|
|
} finally {
|
2020-05-14 00:49:45 -04:00
|
|
|
await killFileServer();
|
2019-01-17 13:08:59 -05:00
|
|
|
}
|
|
|
|
});
|
2019-12-10 07:11:55 -05:00
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
test("serveWithUnorthodoxFilename", async function (): Promise<void> {
|
2019-12-10 07:11:55 -05:00
|
|
|
await startFileServer();
|
|
|
|
try {
|
2020-05-11 17:33:36 -04:00
|
|
|
let res = await fetch("http://localhost:4507/http/testdata/%");
|
2020-02-11 15:53:09 -05:00
|
|
|
assert(res.headers.has("access-control-allow-origin"));
|
|
|
|
assert(res.headers.has("access-control-allow-headers"));
|
|
|
|
assertEquals(res.status, 200);
|
2020-04-10 09:51:17 -04:00
|
|
|
let _ = await res.text();
|
2020-05-11 17:33:36 -04:00
|
|
|
res = await fetch("http://localhost:4507/http/testdata/test%20file.txt");
|
2019-12-10 07:11:55 -05:00
|
|
|
assert(res.headers.has("access-control-allow-origin"));
|
|
|
|
assert(res.headers.has("access-control-allow-headers"));
|
|
|
|
assertEquals(res.status, 200);
|
2020-04-10 09:51:17 -04:00
|
|
|
_ = await res.text();
|
2019-12-10 07:11:55 -05:00
|
|
|
} finally {
|
2020-05-14 00:49:45 -04:00
|
|
|
await killFileServer();
|
2019-12-10 07:11:55 -05:00
|
|
|
}
|
|
|
|
});
|
2019-12-12 00:05:26 -05:00
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
test("printHelp", async function (): Promise<void> {
|
2019-12-13 21:01:32 -05:00
|
|
|
const helpProcess = Deno.run({
|
2020-04-30 11:23:40 -04:00
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"run",
|
2020-05-04 14:23:06 -04:00
|
|
|
// TODO(ry) It ought to be possible to get the help output without
|
|
|
|
// --allow-read.
|
|
|
|
"--allow-read",
|
2020-04-30 11:23:40 -04:00
|
|
|
"http/file_server.ts",
|
|
|
|
"--help",
|
|
|
|
],
|
2020-03-28 13:03:49 -04:00
|
|
|
stdout: "piped",
|
2019-12-13 21:01:32 -05:00
|
|
|
});
|
2020-02-07 02:23:38 -05:00
|
|
|
assert(helpProcess.stdout != null);
|
|
|
|
const r = new TextProtoReader(new BufReader(helpProcess.stdout));
|
2019-12-13 21:01:32 -05:00
|
|
|
const s = await r.readLine();
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(s !== null && s.includes("Deno File Server"));
|
2019-12-13 21:01:32 -05:00
|
|
|
helpProcess.close();
|
2020-02-07 02:23:38 -05:00
|
|
|
helpProcess.stdout.close();
|
2019-12-13 21:01:32 -05:00
|
|
|
});
|
2020-04-01 12:51:01 -04:00
|
|
|
|
|
|
|
test("contentType", async () => {
|
|
|
|
const request = new ServerRequest();
|
|
|
|
const response = await serveFile(request, "http/testdata/hello.html");
|
|
|
|
const contentType = response.headers!.get("content-type");
|
2020-04-03 12:11:52 -04:00
|
|
|
assertEquals(contentType, "text/html");
|
2020-04-01 12:51:01 -04:00
|
|
|
(response.body as Deno.File).close();
|
|
|
|
});
|
2020-06-03 13:48:03 -04:00
|
|
|
|
|
|
|
test("file_server running as library", async function (): Promise<void> {
|
|
|
|
await startFileServerAsLibrary();
|
|
|
|
try {
|
|
|
|
const res = await fetch("http://localhost:8000");
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
} finally {
|
|
|
|
await killFileServer();
|
|
|
|
}
|
|
|
|
});
|