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-10-19 16:55:20 -04:00
|
|
|
const res = await fetch("http://localhost:4500/tsconfig.json");
|
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-10-19 16:55:20 -04:00
|
|
|
assertEquals(
|
|
|
|
res.headers.get("content-type"),
|
|
|
|
"application/json; 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-10-19 16:55:20 -04:00
|
|
|
await Deno.readFile("./tsconfig.json")
|
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-10-19 16:55:20 -04:00
|
|
|
assert(page.includes("tsconfig.json"));
|
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-05-22 18:58:20 -04:00
|
|
|
assert(/<td class="mode">\([a-zA-Z-]{10}\)<\/td>/.test(page));
|
2019-09-09 13:58:36 -04:00
|
|
|
Deno.build.os === "win" &&
|
2019-05-22 18:58:20 -04:00
|
|
|
assert(/<td class="mode">\(unknown mode\)<\/td>/.test(page));
|
|
|
|
assert(
|
2019-10-19 16:55:20 -04:00
|
|
|
page.includes(`<td><a href="/tsconfig.json">tsconfig.json</a></td>`)
|
2019-05-22 18:58:20 -04:00
|
|
|
);
|
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();
|
|
|
|
}
|
|
|
|
});
|