2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-10-27 06:48:45 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2020-11-26 16:31:19 -05:00
|
|
|
assertNotEquals,
|
2020-10-27 06:48:45 -04:00
|
|
|
assertStringIncludes,
|
|
|
|
} 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-11-26 16:31:19 -05:00
|
|
|
import { Response, ServerRequest } from "./server.ts";
|
2020-09-27 06:22:32 -04:00
|
|
|
import { FileServerArgs, serveFile } from "./file_server.ts";
|
|
|
|
import { dirname, fromFileUrl, join, resolve } from "../path/mod.ts";
|
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-08-12 11:38:25 -04:00
|
|
|
type FileServerCfg = Omit<FileServerArgs, "_"> & { target?: string };
|
2020-05-21 13:55:18 -04:00
|
|
|
|
2020-09-14 06:58:43 -04:00
|
|
|
const moduleDir = dirname(fromFileUrl(import.meta.url));
|
|
|
|
const testdataDir = resolve(moduleDir, "testdata");
|
|
|
|
|
2020-05-21 13:55:18 -04:00
|
|
|
async function startFileServer({
|
|
|
|
target = ".",
|
|
|
|
port = 4507,
|
2020-08-12 11:38:25 -04:00
|
|
|
"dir-listing": dirListing = true,
|
2020-05-21 13:55:18 -04:00
|
|
|
}: 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",
|
2020-11-20 12:01:58 -05:00
|
|
|
"--quiet",
|
2019-02-09 15:41:05 -05:00
|
|
|
"--allow-read",
|
|
|
|
"--allow-net",
|
2020-09-14 06:58:43 -04:00
|
|
|
"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}`,
|
2020-08-12 11:38:25 -04:00
|
|
|
`${dirListing ? "" : "--no-dir-listing"}`,
|
2019-02-09 15:41:05 -05:00
|
|
|
],
|
2020-09-14 06:58:43 -04:00
|
|
|
cwd: moduleDir,
|
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",
|
2020-11-20 12:01:58 -05:00
|
|
|
"--quiet",
|
2020-06-03 13:48:03 -04:00
|
|
|
"--allow-read",
|
|
|
|
"--allow-net",
|
2020-09-14 06:58:43 -04:00
|
|
|
"testdata/file_server_as_library.ts",
|
2020-06-03 13:48:03 -04:00
|
|
|
],
|
2020-09-14 06:58:43 -04:00
|
|
|
cwd: moduleDir,
|
2020-06-03 13:48:03 -04:00
|
|
|
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-11-26 16:31:19 -05:00
|
|
|
interface StringResponse extends Response {
|
|
|
|
body: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* HTTP GET request allowing arbitrary paths */
|
|
|
|
async function fetchExactPath(
|
|
|
|
hostname: string,
|
|
|
|
port: number,
|
|
|
|
path: string,
|
|
|
|
): Promise<StringResponse> {
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
const request = encoder.encode("GET " + path + " HTTP/1.1\r\n\r\n");
|
|
|
|
let conn: void | Deno.Conn;
|
|
|
|
try {
|
|
|
|
conn = await Deno.connect(
|
|
|
|
{ hostname: hostname, port: port, transport: "tcp" },
|
|
|
|
);
|
|
|
|
await Deno.writeAll(conn, request);
|
|
|
|
let currentResult = "";
|
|
|
|
let contentLength = -1;
|
|
|
|
let startOfBody = -1;
|
|
|
|
for await (const chunk of Deno.iter(conn)) {
|
|
|
|
currentResult += decoder.decode(chunk);
|
|
|
|
if (contentLength === -1) {
|
|
|
|
const match = /^content-length: (.*)$/m.exec(currentResult);
|
|
|
|
if (match && match[1]) {
|
|
|
|
contentLength = Number(match[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (startOfBody === -1) {
|
|
|
|
const ind = currentResult.indexOf("\r\n\r\n");
|
|
|
|
if (ind !== -1) {
|
|
|
|
startOfBody = ind + 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (startOfBody !== -1 && contentLength !== -1) {
|
|
|
|
const byteLen = encoder.encode(currentResult).length;
|
|
|
|
if (byteLen >= contentLength + startOfBody) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const status = /^HTTP\/1.1 (...)/.exec(currentResult);
|
|
|
|
let statusCode = 0;
|
|
|
|
if (status && status[1]) {
|
|
|
|
statusCode = Number(status[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const body = currentResult.slice(startOfBody);
|
|
|
|
const headersStr = currentResult.slice(0, startOfBody);
|
|
|
|
const headersReg = /^(.*): (.*)$/mg;
|
|
|
|
const headersObj: { [i: string]: string } = {};
|
|
|
|
let match = headersReg.exec(headersStr);
|
|
|
|
while (match !== null) {
|
|
|
|
if (match[1] && match[2]) {
|
|
|
|
headersObj[match[1]] = match[2];
|
|
|
|
}
|
|
|
|
match = headersReg.exec(headersStr);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
status: statusCode,
|
|
|
|
headers: new Headers(headersObj),
|
|
|
|
body: body,
|
|
|
|
};
|
|
|
|
} finally {
|
|
|
|
if (conn) {
|
|
|
|
Deno.close(conn.rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
2020-09-14 06:58:43 -04:00
|
|
|
"file_server serveFile",
|
2020-06-12 15:23:38 -04:00
|
|
|
async (): Promise<void> => {
|
|
|
|
await startFileServer();
|
|
|
|
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(
|
2020-09-14 06:58:43 -04:00
|
|
|
await Deno.readFile(join(moduleDir, "README.md")),
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
|
|
|
assertEquals(downloadedFile, localFile);
|
|
|
|
} finally {
|
|
|
|
await killFileServer();
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
2018-12-11 17:56:32 -05:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
2020-09-14 06:58:43 -04:00
|
|
|
"file_server serveFile in testdata",
|
2020-06-12 15:23:38 -04:00
|
|
|
async (): Promise<void> => {
|
2020-09-14 06:58:43 -04:00
|
|
|
await startFileServer({ target: "./testdata" });
|
2020-06-12 15:23:38 -04:00
|
|
|
try {
|
2020-09-14 06:58:43 -04:00
|
|
|
const res = await fetch("http://localhost:4507/hello.html");
|
2020-06-12 15:23:38 -04:00
|
|
|
assert(res.headers.has("access-control-allow-origin"));
|
|
|
|
assert(res.headers.has("access-control-allow-headers"));
|
2020-09-14 06:58:43 -04:00
|
|
|
assertEquals(res.headers.get("content-type"), "text/html");
|
2020-06-12 15:23:38 -04:00
|
|
|
const downloadedFile = await res.text();
|
|
|
|
const localFile = new TextDecoder().decode(
|
2020-09-14 06:58:43 -04:00
|
|
|
await Deno.readFile(join(testdataDir, "hello.html")),
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
|
|
|
assertEquals(downloadedFile, localFile);
|
|
|
|
} finally {
|
|
|
|
await killFileServer();
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
2020-05-21 13:55:18 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.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
|
2021-01-16 18:32:59 -05:00
|
|
|
// TODO(bartlomieju): `mode` should work correctly in the future.
|
2019-06-19 00:22:01 -04:00
|
|
|
// 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-06-12 15:23:38 -04:00
|
|
|
Deno.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-10-27 06:48:45 -04:00
|
|
|
const _ = await res.text();
|
|
|
|
} finally {
|
|
|
|
await killFileServer();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("checkPathTraversal", async function (): Promise<void> {
|
|
|
|
await startFileServer();
|
|
|
|
try {
|
|
|
|
const res = await fetch(
|
|
|
|
"http://localhost:4507/../../../../../../../..",
|
|
|
|
);
|
|
|
|
assert(res.headers.has("access-control-allow-origin"));
|
|
|
|
assert(res.headers.has("access-control-allow-headers"));
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
const listing = await res.text();
|
|
|
|
assertStringIncludes(listing, "README.md");
|
|
|
|
} finally {
|
|
|
|
await killFileServer();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-26 16:31:19 -05:00
|
|
|
Deno.test("checkPathTraversalNoLeadingSlash", async function (): Promise<void> {
|
|
|
|
await startFileServer();
|
|
|
|
try {
|
|
|
|
const res = await fetchExactPath("127.0.0.1", 4507, "../../../..");
|
|
|
|
assertEquals(res.status, 400);
|
|
|
|
} finally {
|
|
|
|
await killFileServer();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("checkPathTraversalAbsoluteURI", async function (): Promise<void> {
|
|
|
|
await startFileServer();
|
|
|
|
try {
|
|
|
|
//allowed per https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
|
|
|
|
const res = await fetchExactPath(
|
|
|
|
"127.0.0.1",
|
|
|
|
4507,
|
|
|
|
"http://localhost/../../../..",
|
|
|
|
);
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
assertStringIncludes(res.body, "README.md");
|
|
|
|
} finally {
|
|
|
|
await killFileServer();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-10-27 06:48:45 -04:00
|
|
|
Deno.test("checkURIEncodedPathTraversal", async function (): Promise<void> {
|
|
|
|
await startFileServer();
|
|
|
|
try {
|
|
|
|
const res = await fetch(
|
|
|
|
"http://localhost:4507/%2F..%2F..%2F..%2F..%2F..%2F..%2F..%2F..",
|
|
|
|
);
|
|
|
|
assert(res.headers.has("access-control-allow-origin"));
|
|
|
|
assert(res.headers.has("access-control-allow-headers"));
|
|
|
|
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-06-12 15:23:38 -04:00
|
|
|
Deno.test("serveWithUnorthodoxFilename", async function (): Promise<void> {
|
2019-12-10 07:11:55 -05:00
|
|
|
await startFileServer();
|
|
|
|
try {
|
2020-09-14 06:58:43 -04:00
|
|
|
let res = await fetch("http://localhost:4507/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-09-14 06:58:43 -04:00
|
|
|
res = await fetch("http://localhost:4507/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-06-12 15:23:38 -04:00
|
|
|
Deno.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-11-20 12:01:58 -05:00
|
|
|
"--quiet",
|
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-09-14 06:58:43 -04:00
|
|
|
"file_server.ts",
|
2020-04-30 11:23:40 -04:00
|
|
|
"--help",
|
|
|
|
],
|
2020-09-14 06:58:43 -04:00
|
|
|
cwd: moduleDir,
|
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
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("contentType", async () => {
|
2020-04-01 12:51:01 -04:00
|
|
|
const request = new ServerRequest();
|
2020-09-14 06:58:43 -04:00
|
|
|
const response = await serveFile(request, join(testdataDir, "hello.html"));
|
2020-04-01 12:51:01 -04:00
|
|
|
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
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("file_server running as library", async function (): Promise<void> {
|
2020-06-03 13:48:03 -04:00
|
|
|
await startFileServerAsLibrary();
|
|
|
|
try {
|
2021-01-24 16:29:36 -05:00
|
|
|
const res = await fetch("http://localhost:4504");
|
2020-06-03 13:48:03 -04:00
|
|
|
assertEquals(res.status, 200);
|
2020-09-19 08:07:54 -04:00
|
|
|
const _ = await res.text();
|
2020-06-03 13:48:03 -04:00
|
|
|
} finally {
|
|
|
|
await killFileServer();
|
|
|
|
}
|
|
|
|
});
|
2020-08-12 06:55:38 -04:00
|
|
|
|
2020-10-26 09:55:26 -04:00
|
|
|
Deno.test("file_server should ignore query params", async () => {
|
|
|
|
await startFileServer();
|
|
|
|
try {
|
|
|
|
const res = await fetch("http://localhost:4507/README.md?key=value");
|
|
|
|
assertEquals(res.status, 200);
|
|
|
|
const downloadedFile = await res.text();
|
|
|
|
const localFile = new TextDecoder().decode(
|
|
|
|
await Deno.readFile(join(moduleDir, "README.md")),
|
|
|
|
);
|
|
|
|
assertEquals(downloadedFile, localFile);
|
|
|
|
} finally {
|
|
|
|
await killFileServer();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-12 06:55:38 -04:00
|
|
|
async function startTlsFileServer({
|
|
|
|
target = ".",
|
|
|
|
port = 4577,
|
|
|
|
}: FileServerCfg = {}): Promise<void> {
|
|
|
|
fileServer = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"run",
|
2020-11-20 12:01:58 -05:00
|
|
|
"--quiet",
|
2020-08-12 06:55:38 -04:00
|
|
|
"--allow-read",
|
|
|
|
"--allow-net",
|
2020-09-14 06:58:43 -04:00
|
|
|
"file_server.ts",
|
2020-08-12 06:55:38 -04:00
|
|
|
target,
|
|
|
|
"--host",
|
|
|
|
"localhost",
|
|
|
|
"--cert",
|
2020-09-14 06:58:43 -04:00
|
|
|
"./testdata/tls/localhost.crt",
|
2020-08-12 06:55:38 -04:00
|
|
|
"--key",
|
2020-09-14 06:58:43 -04:00
|
|
|
"./testdata/tls/localhost.key",
|
2020-08-12 06:55:38 -04:00
|
|
|
"--cors",
|
|
|
|
"-p",
|
|
|
|
`${port}`,
|
|
|
|
],
|
2020-09-14 06:58:43 -04:00
|
|
|
cwd: moduleDir,
|
2020-08-12 06:55:38 -04:00
|
|
|
stdout: "piped",
|
|
|
|
stderr: "null",
|
|
|
|
});
|
|
|
|
// Once fileServer is ready it will write to its stdout.
|
|
|
|
assert(fileServer.stdout != null);
|
|
|
|
const r = new TextProtoReader(new BufReader(fileServer.stdout));
|
|
|
|
const s = await r.readLine();
|
|
|
|
assert(s !== null && s.includes("server listening"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.test("serveDirectory TLS", async function (): Promise<void> {
|
|
|
|
await startTlsFileServer();
|
|
|
|
try {
|
|
|
|
// Valid request after invalid
|
|
|
|
const conn = await Deno.connectTls({
|
|
|
|
hostname: "localhost",
|
|
|
|
port: 4577,
|
2020-09-14 06:58:43 -04:00
|
|
|
certFile: join(testdataDir, "tls/RootCA.pem"),
|
2020-08-12 06:55:38 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
await Deno.writeAll(
|
|
|
|
conn,
|
2020-09-14 06:58:43 -04:00
|
|
|
new TextEncoder().encode("GET / HTTP/1.0\r\n\r\n"),
|
2020-08-12 06:55:38 -04:00
|
|
|
);
|
|
|
|
const res = new Uint8Array(128 * 1024);
|
|
|
|
const nread = await conn.read(res);
|
|
|
|
assert(nread !== null);
|
|
|
|
conn.close();
|
|
|
|
const page = new TextDecoder().decode(res.subarray(0, nread));
|
|
|
|
assert(page.includes("<title>Deno File Server</title>"));
|
|
|
|
} finally {
|
|
|
|
await killFileServer();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("partial TLS arguments fail", async function (): Promise<void> {
|
|
|
|
fileServer = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"run",
|
2020-11-20 12:01:58 -05:00
|
|
|
"--quiet",
|
2020-08-12 06:55:38 -04:00
|
|
|
"--allow-read",
|
|
|
|
"--allow-net",
|
2020-09-14 06:58:43 -04:00
|
|
|
"file_server.ts",
|
2020-08-12 06:55:38 -04:00
|
|
|
".",
|
|
|
|
"--host",
|
|
|
|
"localhost",
|
|
|
|
"--cert",
|
2020-09-14 06:58:43 -04:00
|
|
|
"./testdata/tls/localhost.crt",
|
2020-08-12 06:55:38 -04:00
|
|
|
"-p",
|
|
|
|
`4578`,
|
|
|
|
],
|
2020-09-14 06:58:43 -04:00
|
|
|
cwd: moduleDir,
|
2020-08-12 06:55:38 -04:00
|
|
|
stdout: "piped",
|
|
|
|
stderr: "null",
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
// Once fileServer is ready it will write to its stdout.
|
|
|
|
assert(fileServer.stdout != null);
|
|
|
|
const r = new TextProtoReader(new BufReader(fileServer.stdout));
|
|
|
|
const s = await r.readLine();
|
|
|
|
assert(
|
|
|
|
s !== null && s.includes("--key and --cert are required for TLS"),
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
await killFileServer();
|
|
|
|
}
|
|
|
|
});
|
2020-08-12 11:38:25 -04:00
|
|
|
|
|
|
|
Deno.test("file_server disable dir listings", async function (): Promise<void> {
|
|
|
|
await startFileServer({ "dir-listing": false });
|
|
|
|
try {
|
|
|
|
const res = await fetch("http://localhost:4507/");
|
|
|
|
assert(res.headers.has("access-control-allow-origin"));
|
|
|
|
assert(res.headers.has("access-control-allow-headers"));
|
|
|
|
assertEquals(res.status, 404);
|
|
|
|
const _ = await res.text();
|
|
|
|
} finally {
|
|
|
|
await killFileServer();
|
|
|
|
}
|
|
|
|
});
|