2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-01-27 21:13:17 -05:00
|
|
|
import { assert, assertEquals, assertStrContains } 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-02-11 11:24:27 -05:00
|
|
|
const { test } = Deno;
|
2018-12-11 17:56:32 -05:00
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
let fileServer: Deno.Process;
|
|
|
|
|
2019-03-12 01:51:51 -04:00
|
|
|
async function startFileServer(): Promise<void> {
|
2019-10-09 17:22:22 -04:00
|
|
|
fileServer = Deno.run({
|
2019-02-09 15:41:05 -05:00
|
|
|
args: [
|
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",
|
|
|
|
".",
|
|
|
|
"--cors"
|
|
|
|
],
|
2019-01-17 13:08:59 -05:00
|
|
|
stdout: "piped"
|
|
|
|
});
|
|
|
|
// 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();
|
2019-07-07 15:20:41 -04:00
|
|
|
assert(s !== Deno.EOF && s.includes("server listening"));
|
2019-01-17 13:08:59 -05:00
|
|
|
}
|
2019-05-23 22:04:06 -04:00
|
|
|
|
2019-03-12 01:51:51 -04:00
|
|
|
function killFileServer(): void {
|
2019-01-17 13:08:59 -05:00
|
|
|
fileServer.close();
|
2020-02-07 02:23:38 -05:00
|
|
|
fileServer.stdout?.close();
|
2018-12-11 17:56:32 -05:00
|
|
|
}
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function serveFile(): Promise<void> {
|
2019-01-17 13:08:59 -05:00
|
|
|
await startFileServer();
|
|
|
|
try {
|
2019-11-04 18:13:28 -05:00
|
|
|
const res = await fetch("http://localhost:4500/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"));
|
2019-12-14 03:03:30 -05:00
|
|
|
assert(res.headers.has("content-type"));
|
2020-02-19 15:36:18 -05:00
|
|
|
assert(res.headers.get("content-type")!.includes("charset=utf-8"));
|
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 {
|
|
|
|
killFileServer();
|
|
|
|
}
|
|
|
|
});
|
2018-12-11 17:56:32 -05:00
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function serveDirectory(): Promise<void> {
|
2019-01-17 13:08:59 -05:00
|
|
|
await startFileServer();
|
|
|
|
try {
|
2018-12-11 17:56:32 -05:00
|
|
|
const res = await fetch("http://localhost:4500/");
|
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.
|
2019-09-09 13:58:36 -04:00
|
|
|
Deno.build.os !== "win" &&
|
2019-12-02 19:14:25 -05:00
|
|
|
assert(/<td class="mode">(\s)*\([a-zA-Z-]{10}\)(\s)*<\/td>/.test(page));
|
2019-09-09 13:58:36 -04:00
|
|
|
Deno.build.os === "win" &&
|
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 {
|
|
|
|
killFileServer();
|
|
|
|
}
|
|
|
|
});
|
2018-12-11 17:56:32 -05:00
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function serveFallback(): Promise<void> {
|
2019-01-17 13:08:59 -05:00
|
|
|
await startFileServer();
|
|
|
|
try {
|
2018-12-11 17:56:32 -05:00
|
|
|
const res = await fetch("http://localhost:4500/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);
|
2019-01-17 13:08:59 -05:00
|
|
|
} finally {
|
|
|
|
killFileServer();
|
|
|
|
}
|
|
|
|
});
|
2019-12-10 07:11:55 -05:00
|
|
|
|
2020-02-11 15:53:09 -05:00
|
|
|
test(async function serveWithUnorthodoxFilename(): Promise<void> {
|
2019-12-10 07:11:55 -05:00
|
|
|
await startFileServer();
|
|
|
|
try {
|
2020-02-11 15:53:09 -05:00
|
|
|
let res = await fetch("http://localhost:4500/http/testdata/%");
|
|
|
|
assert(res.headers.has("access-control-allow-origin"));
|
|
|
|
assert(res.headers.has("access-control-allow-headers"));
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
|
|
|
|
res = await fetch("http://localhost:4500/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);
|
|
|
|
} finally {
|
|
|
|
killFileServer();
|
|
|
|
}
|
|
|
|
});
|
2019-12-12 00:05:26 -05:00
|
|
|
|
|
|
|
test(async function servePermissionDenied(): Promise<void> {
|
|
|
|
const deniedServer = Deno.run({
|
|
|
|
args: [Deno.execPath(), "run", "--allow-net", "http/file_server.ts"],
|
|
|
|
stdout: "piped",
|
|
|
|
stderr: "piped"
|
|
|
|
});
|
2020-02-07 02:23:38 -05:00
|
|
|
assert(deniedServer.stdout != null);
|
|
|
|
const reader = new TextProtoReader(new BufReader(deniedServer.stdout));
|
|
|
|
assert(deniedServer.stderr != null);
|
|
|
|
const errReader = new TextProtoReader(new BufReader(deniedServer.stderr));
|
2019-12-12 00:05:26 -05:00
|
|
|
const s = await reader.readLine();
|
|
|
|
assert(s !== Deno.EOF && s.includes("server listening"));
|
|
|
|
|
|
|
|
try {
|
|
|
|
await fetch("http://localhost:4500/");
|
2020-01-27 21:13:17 -05:00
|
|
|
assertStrContains(
|
|
|
|
(await errReader.readLine()) as string,
|
2019-12-12 00:05:26 -05:00
|
|
|
"run again with the --allow-read flag"
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
deniedServer.close();
|
2020-02-07 02:23:38 -05:00
|
|
|
deniedServer.stdout.close();
|
|
|
|
deniedServer.stderr.close();
|
2019-12-12 00:05:26 -05:00
|
|
|
}
|
|
|
|
});
|
2019-12-13 21:01:32 -05:00
|
|
|
|
|
|
|
test(async function printHelp(): Promise<void> {
|
|
|
|
const helpProcess = Deno.run({
|
2020-01-15 19:21:35 -05:00
|
|
|
args: [Deno.execPath(), "run", "http/file_server.ts", "--help"],
|
2019-12-13 21:01:32 -05:00
|
|
|
stdout: "piped"
|
|
|
|
});
|
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();
|
|
|
|
assert(s !== Deno.EOF && s.includes("Deno File Server"));
|
|
|
|
helpProcess.close();
|
2020-02-07 02:23:38 -05:00
|
|
|
helpProcess.stdout.close();
|
2019-12-13 21:01:32 -05:00
|
|
|
});
|