2019-02-07 11:45:47 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-02-26 00:35:50 -05:00
|
|
|
const { readFile, run } = Deno;
|
2018-12-11 17:56:32 -05:00
|
|
|
|
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-05-23 22:04:06 -04:00
|
|
|
import { BufReader, EOF } 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-01-17 13:08:59 -05:00
|
|
|
let fileServer;
|
2019-03-12 01:51:51 -04:00
|
|
|
async function startFileServer(): Promise<void> {
|
2019-01-17 13:08:59 -05:00
|
|
|
fileServer = run({
|
2019-02-09 15:41:05 -05:00
|
|
|
args: [
|
|
|
|
"deno",
|
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.
|
|
|
|
const r = new TextProtoReader(new BufReader(fileServer.stdout));
|
2019-05-23 22:04:06 -04:00
|
|
|
const s = await r.readLine();
|
|
|
|
assert(s !== 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();
|
|
|
|
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-01-03 23:11:40 -05:00
|
|
|
const res = await fetch("http://localhost:4500/azure-pipelines.yml");
|
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.headers.get("content-type"), "text/yaml; 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(
|
|
|
|
await readFile("./azure-pipelines.yml")
|
|
|
|
);
|
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-01-03 23:11:40 -05:00
|
|
|
assert(page.includes("azure-pipelines.yml"));
|
2019-05-22 18:58:20 -04:00
|
|
|
|
|
|
|
// `Deno.FileInfo` is not completely compatible with Windows yet
|
|
|
|
// TODO: `mode` should work correctly in the future. Correct this test case accordingly.
|
|
|
|
Deno.platform.os !== "win" &&
|
|
|
|
assert(/<td class="mode">\([a-zA-Z-]{10}\)<\/td>/.test(page));
|
|
|
|
Deno.platform.os === "win" &&
|
|
|
|
assert(/<td class="mode">\(unknown mode\)<\/td>/.test(page));
|
|
|
|
assert(
|
|
|
|
page.includes(
|
|
|
|
`<td><a href="/azure-pipelines.yml">azure-pipelines.yml</a></td>`
|
|
|
|
)
|
|
|
|
);
|
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();
|
|
|
|
}
|
|
|
|
});
|