summaryrefslogtreecommitdiff
path: root/std/http/file_server_test.ts
diff options
context:
space:
mode:
authormoyinzi <632378816@qq.com>2020-05-22 01:55:18 +0800
committerGitHub <noreply@github.com>2020-05-21 13:55:18 -0400
commit8d8a2f573f32e0b2680eb114739902c5953f7b99 (patch)
treed5f81e839742085be01fa239bf285909750efb31 /std/http/file_server_test.ts
parent65f4e5912293bea5c59e2923cabfa8c0373a05a7 (diff)
fix(std/http): file_server's target directory (#5695)
Diffstat (limited to 'std/http/file_server_test.ts')
-rw-r--r--std/http/file_server_test.ts34
1 files changed, 31 insertions, 3 deletions
diff --git a/std/http/file_server_test.ts b/std/http/file_server_test.ts
index 2ff114906..97c07c250 100644
--- a/std/http/file_server_test.ts
+++ b/std/http/file_server_test.ts
@@ -7,7 +7,15 @@ import { serveFile } from "./file_server.ts";
const { test } = Deno;
let fileServer: Deno.Process;
-async function startFileServer(): Promise<void> {
+type FileServerCfg = {
+ target?: string;
+ port?: number;
+};
+
+async function startFileServer({
+ target = ".",
+ port = 4507,
+}: FileServerCfg = {}): Promise<void> {
fileServer = Deno.run({
cmd: [
Deno.execPath(),
@@ -15,8 +23,10 @@ async function startFileServer(): Promise<void> {
"--allow-read",
"--allow-net",
"http/file_server.ts",
- ".",
+ target,
"--cors",
+ "-p",
+ `${port}`,
],
stdout: "piped",
stderr: "null",
@@ -40,7 +50,7 @@ async function killFileServer(): Promise<void> {
fileServer.stdout!.close();
}
-test("file_server serveFile", async (): Promise<void> => {
+test("file_server serveFile in ./", async (): Promise<void> => {
await startFileServer();
try {
const res = await fetch("http://localhost:4507/README.md");
@@ -57,6 +67,24 @@ test("file_server serveFile", async (): Promise<void> => {
}
});
+test("file_server serveFile in ./http", async (): Promise<void> => {
+ await startFileServer({ target: "./http" });
+ 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(
+ await Deno.readFile("./http/README.md")
+ );
+ console.log(downloadedFile, localFile);
+ assertEquals(downloadedFile, localFile);
+ } finally {
+ await killFileServer();
+ }
+});
+
test("serveDirectory", async function (): Promise<void> {
await startFileServer();
try {