summaryrefslogtreecommitdiff
path: root/std/http
diff options
context:
space:
mode:
Diffstat (limited to 'std/http')
-rw-r--r--std/http/file_server_test.ts28
-rw-r--r--std/http/http_bench.ts3
-rw-r--r--std/http/racing_server.ts2
-rw-r--r--std/http/racing_server_test.ts12
-rw-r--r--std/http/server_test.ts23
-rw-r--r--std/http/test_util.ts20
-rw-r--r--std/http/testdata/simple_https_server.ts3
-rw-r--r--std/http/testdata/simple_server.ts5
8 files changed, 22 insertions, 74 deletions
diff --git a/std/http/file_server_test.ts b/std/http/file_server_test.ts
index d7f939dad..fcf776ea2 100644
--- a/std/http/file_server_test.ts
+++ b/std/http/file_server_test.ts
@@ -2,11 +2,9 @@
import { assert, assertEquals, assertStrContains } from "../testing/asserts.ts";
import { BufReader } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
-import { randomPort } from "./test_util.ts";
const { test } = Deno;
let fileServer: Deno.Process;
-const port = randomPort();
async function startFileServer(): Promise<void> {
fileServer = Deno.run({
cmd: [
@@ -16,9 +14,7 @@ async function startFileServer(): Promise<void> {
"--allow-net",
"http/file_server.ts",
".",
- "--cors",
- "--port",
- `${port}`
+ "--cors"
],
stdout: "piped",
stderr: "null"
@@ -38,7 +34,7 @@ function killFileServer(): void {
test(async function serveFile(): Promise<void> {
await startFileServer();
try {
- const res = await fetch(`http://localhost:${port}/README.md`);
+ const res = await fetch("http://localhost:4500/README.md");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assert(res.headers.has("content-type"));
@@ -56,7 +52,7 @@ test(async function serveFile(): Promise<void> {
test(async function serveDirectory(): Promise<void> {
await startFileServer();
try {
- const res = await fetch(`http://localhost:${port}/`);
+ const res = await fetch("http://localhost:4500/");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
const page = await res.text();
@@ -78,7 +74,7 @@ test(async function serveDirectory(): Promise<void> {
test(async function serveFallback(): Promise<void> {
await startFileServer();
try {
- const res = await fetch(`http://localhost:${port}/badfile.txt`);
+ const res = await fetch("http://localhost:4500/badfile.txt");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assertEquals(res.status, 404);
@@ -91,12 +87,12 @@ test(async function serveFallback(): Promise<void> {
test(async function serveWithUnorthodoxFilename(): Promise<void> {
await startFileServer();
try {
- let res = await fetch(`http://localhost:${port}/http/testdata/%`);
+ let res = await fetch("http://localhost:4500/http/testdata/%");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assertEquals(res.status, 200);
res.body.close();
- res = await fetch(`http://localhost:${port}/http/testdata/test%20file.txt`);
+ res = await fetch("http://localhost:4500/http/testdata/test%20file.txt");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assertEquals(res.status, 200);
@@ -107,16 +103,8 @@ test(async function serveWithUnorthodoxFilename(): Promise<void> {
});
test(async function servePermissionDenied(): Promise<void> {
- const _port = randomPort();
const deniedServer = Deno.run({
- cmd: [
- Deno.execPath(),
- "run",
- "--allow-net",
- "http/file_server.ts",
- "-p",
- `${_port}`
- ],
+ cmd: [Deno.execPath(), "run", "--allow-net", "http/file_server.ts"],
stdout: "piped",
stderr: "piped"
});
@@ -128,7 +116,7 @@ test(async function servePermissionDenied(): Promise<void> {
assert(s !== Deno.EOF && s.includes("server listening"));
try {
- const res = await fetch(`http://localhost:${_port}/`);
+ const res = await fetch("http://localhost:4500/");
res.body.close();
assertStrContains(
(await errReader.readLine()) as string,
diff --git a/std/http/http_bench.ts b/std/http/http_bench.ts
index 060d0ad88..9d1912831 100644
--- a/std/http/http_bench.ts
+++ b/std/http/http_bench.ts
@@ -1,8 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { serve } from "./server.ts";
-import { randomPort } from "./test_util.ts";
-const addr = Deno.args[0] || "127.0.0.1:" + randomPort();
+const addr = Deno.args[0] || "127.0.0.1:4500";
const server = serve(addr);
const body = new TextEncoder().encode("Hello World");
diff --git a/std/http/racing_server.ts b/std/http/racing_server.ts
index 693531a7f..0b0e5a8a5 100644
--- a/std/http/racing_server.ts
+++ b/std/http/racing_server.ts
@@ -2,7 +2,7 @@
import { serve, ServerRequest } from "./server.ts";
import { delay } from "../util/async.ts";
-const addr = Deno.args[0] || "127.0.0.1:4501";
+const addr = Deno.args[1] || "127.0.0.1:4501";
const server = serve(addr);
function body(i: number): string {
diff --git a/std/http/racing_server_test.ts b/std/http/racing_server_test.ts
index 76b0e872d..865777599 100644
--- a/std/http/racing_server_test.ts
+++ b/std/http/racing_server_test.ts
@@ -1,20 +1,12 @@
import { assert, assertEquals } from "../testing/asserts.ts";
import { BufReader, BufWriter } from "../io/bufio.ts";
import { TextProtoReader } from "../textproto/mod.ts";
-import { randomPort } from "./test_util.ts";
-const port = randomPort();
const { connect, run, test } = Deno;
let server: Deno.Process;
async function startServer(): Promise<void> {
server = run({
- cmd: [
- Deno.execPath(),
- "run",
- "-A",
- "http/racing_server.ts",
- "127.0.0.1:" + port
- ],
+ cmd: [Deno.execPath(), "run", "-A", "http/racing_server.ts"],
stdout: "piped"
});
// Once racing server is ready it will write to its stdout.
@@ -69,7 +61,7 @@ Step7
test(async function serverPipelineRace(): Promise<void> {
await startServer();
- const conn = await connect({ port });
+ const conn = await connect({ port: 4501 });
const r = new TextProtoReader(new BufReader(conn));
const w = new BufWriter(conn);
await w.write(new TextEncoder().encode(input));
diff --git a/std/http/server_test.ts b/std/http/server_test.ts
index d9ce3ba97..f66b190b2 100644
--- a/std/http/server_test.ts
+++ b/std/http/server_test.ts
@@ -18,7 +18,6 @@ import { BufReader, BufWriter } from "../io/bufio.ts";
import { delay } from "../util/async.ts";
import { encode, decode } from "../strings/mod.ts";
import { mockConn } from "./mock.ts";
-import { randomPort } from "./test_util.ts";
const { Buffer, test } = Deno;
@@ -356,14 +355,8 @@ test({
ignore: true,
fn: async (): Promise<void> => {
// Runs a simple server as another process
- const port = randomPort();
const p = Deno.run({
- cmd: [
- Deno.execPath(),
- "--allow-net",
- "http/testdata/simple_server.ts",
- `${port}`
- ],
+ cmd: [Deno.execPath(), "--allow-net", "http/testdata/simple_server.ts"],
stdout: "piped"
});
@@ -402,15 +395,13 @@ test({
// FIXME(bartlomieju): hangs on windows, cause can't do `Deno.kill`
ignore: true,
fn: async (): Promise<void> => {
- const port = randomPort();
// Runs a simple server as another process
const p = Deno.run({
cmd: [
Deno.execPath(),
"--allow-net",
"--allow-read",
- "http/testdata/simple_https_server.ts",
- `${port}`
+ "http/testdata/simple_https_server.ts"
],
stdout: "piped"
});
@@ -422,6 +413,7 @@ test({
serverIsRunning = false;
})
.catch((_): void => {}); // Ignores the error when closing the process.
+
try {
const r = new TextProtoReader(new BufReader(p.stdout!));
const s = await r.readLine();
@@ -432,7 +424,7 @@ test({
// Requests to the server and immediately closes the connection
const conn = await Deno.connectTLS({
hostname: "localhost",
- port,
+ port: 4503,
certFile: "http/testdata/tls/RootCA.pem"
});
await Deno.writeAll(
@@ -456,7 +448,7 @@ test({
});
test("close server while iterating", async (): Promise<void> => {
- const server = serve({ port: randomPort() });
+ const server = serve(":8123");
const nextWhileClosing = server[Symbol.asyncIterator]().next();
server.close();
assertEquals(await nextWhileClosing, { value: undefined, done: true });
@@ -499,9 +491,8 @@ test({
test({
name: "respond error closes connection",
async fn(): Promise<void> {
- const port = randomPort();
const serverRoutine = async (): Promise<void> => {
- const server = serve(":" + port);
+ const server = serve(":8124");
// @ts-ignore
for await (const req of server) {
await assertThrowsAsync(async () => {
@@ -518,7 +509,7 @@ test({
const p = serverRoutine();
const conn = await Deno.connect({
hostname: "127.0.0.1",
- port
+ port: 8124
});
await Deno.writeAll(
conn,
diff --git a/std/http/test_util.ts b/std/http/test_util.ts
deleted file mode 100644
index c86a339a7..000000000
--- a/std/http/test_util.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-import { assert } from "../testing/asserts.ts";
-
-function* portIterator(): IterableIterator<number> {
- // use 55001 ~ 65535 (rest (49152~55000) are for cli/js)
- let i = 55001;
- while (true) {
- yield i;
- i++;
- if (i > 65535) {
- i = 55001;
- }
- }
-}
-const it = portIterator();
-/** Obtain (maybe) safe port number for net tests */
-export function randomPort(): number {
- const { value } = it.next();
- assert(value != null);
- return value;
-}
diff --git a/std/http/testdata/simple_https_server.ts b/std/http/testdata/simple_https_server.ts
index 9e3045609..9330b4172 100644
--- a/std/http/testdata/simple_https_server.ts
+++ b/std/http/testdata/simple_https_server.ts
@@ -2,10 +2,9 @@
// This is an example of a https server
import { serveTLS } from "../server.ts";
-const port = parseInt(Deno.args[0] || "4503");
const tlsOptions = {
hostname: "localhost",
- port,
+ port: 4503,
certFile: "./http/testdata/tls/localhost.crt",
keyFile: "./http/testdata/tls/localhost.key"
};
diff --git a/std/http/testdata/simple_server.ts b/std/http/testdata/simple_server.ts
index 05f169705..d8ca4cc97 100644
--- a/std/http/testdata/simple_server.ts
+++ b/std/http/testdata/simple_server.ts
@@ -2,9 +2,8 @@
// This is an example of a server that responds with an empty body
import { serve } from "../server.ts";
-const port = parseInt(Deno.args[0] || "4502");
-const addr: Deno.ListenOptions = { port };
-console.log(`Simple server listening on ${port}`);
+const addr = "0.0.0.0:4502";
+console.log(`Simple server listening on ${addr}`);
for await (const req of serve(addr)) {
req.respond({});
}