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-01-17 13:08:59 -05:00
|
|
|
import { BufReader } from "../io/bufio.ts";
|
|
|
|
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",
|
|
|
|
"--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));
|
|
|
|
const [s, err] = await r.readLine();
|
|
|
|
assert(err == null);
|
|
|
|
assert(s.includes("server listening"));
|
|
|
|
}
|
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-01-17 13:08:59 -05:00
|
|
|
test(async function serveFile() {
|
|
|
|
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-01-17 13:08:59 -05:00
|
|
|
test(async function serveDirectory() {
|
|
|
|
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-01-17 13:08:59 -05:00
|
|
|
} finally {
|
|
|
|
killFileServer();
|
|
|
|
}
|
|
|
|
});
|
2018-12-11 17:56:32 -05:00
|
|
|
|
2019-01-17 13:08:59 -05:00
|
|
|
test(async function serveFallback() {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|