2019-02-07 11:45:47 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 16:39:50 -05:00
|
|
|
import { test } from "../testing/mod.ts";
|
2019-03-06 19:42:24 -05: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";
|
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.
|
2019-05-30 08:59:30 -04:00
|
|
|
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();
|
2019-05-30 08:59:30 -04: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"));
|
|
|
|
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
|
|
|
|
|
|
|
test(async function serveFallback(): Promise<void> {
|
|
|
|
await startFileServer();
|
|
|
|
try {
|
|
|
|
const res = await fetch(
|
|
|
|
"http://localhost:4500/http/testdata/test%20file.txt"
|
|
|
|
);
|
|
|
|
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"
|
|
|
|
});
|
|
|
|
const reader = new TextProtoReader(new BufReader(deniedServer.stdout!));
|
|
|
|
const errReader = new TextProtoReader(new BufReader(deniedServer.stderr!));
|
|
|
|
const s = await reader.readLine();
|
|
|
|
assert(s !== Deno.EOF && s.includes("server listening"));
|
|
|
|
|
|
|
|
try {
|
|
|
|
await fetch("http://localhost:4500/");
|
|
|
|
assertEquals(
|
|
|
|
await errReader.readLine(),
|
|
|
|
"run again with the --allow-read flag"
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
deniedServer.close();
|
|
|
|
deniedServer.stdout!.close();
|
|
|
|
deniedServer.stderr!.close();
|
|
|
|
}
|
|
|
|
});
|
2019-12-13 21:01:32 -05:00
|
|
|
|
|
|
|
test(async function printHelp(): Promise<void> {
|
|
|
|
const helpProcess = Deno.run({
|
|
|
|
args: [Deno.execPath(), "run", "http/file_server.ts", "--help"],
|
|
|
|
stdout: "piped"
|
|
|
|
});
|
|
|
|
const r = new TextProtoReader(new BufReader(helpProcess.stdout!));
|
|
|
|
const s = await r.readLine();
|
|
|
|
assert(s !== Deno.EOF && s.includes("Deno File Server"));
|
|
|
|
helpProcess.close();
|
|
|
|
helpProcess.stdout!.close();
|
|
|
|
});
|