2018-12-17 11:49:10 -05:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Ported from
|
|
|
|
// https://github.com/golang/go/blob/master/src/net/http/responsewrite_test.go
|
|
|
|
|
2020-02-08 15:24:09 -05:00
|
|
|
import { TextProtoReader } from "../textproto/mod.ts";
|
2020-03-19 11:04:26 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2020-04-07 06:34:18 -04:00
|
|
|
assertMatch,
|
2020-10-26 11:03:30 -04:00
|
|
|
assertStringIncludes,
|
2020-03-28 13:03:49 -04:00
|
|
|
assertThrowsAsync,
|
2020-03-19 11:04:26 -04:00
|
|
|
} from "../testing/asserts.ts";
|
2020-06-29 10:39:17 -04:00
|
|
|
import {
|
2020-09-27 06:22:32 -04:00
|
|
|
_parseAddrFromStr,
|
2020-06-29 10:39:17 -04:00
|
|
|
Response,
|
|
|
|
serve,
|
2020-09-27 06:22:32 -04:00
|
|
|
Server,
|
|
|
|
ServerRequest,
|
2020-06-29 10:39:17 -04:00
|
|
|
serveTLS,
|
|
|
|
} from "./server.ts";
|
2020-02-26 10:48:35 -05:00
|
|
|
import { BufReader, BufWriter } from "../io/bufio.ts";
|
2020-05-09 08:34:47 -04:00
|
|
|
import { delay } from "../async/delay.ts";
|
2020-09-27 06:22:32 -04:00
|
|
|
import { decode, encode } from "../encoding/utf8.ts";
|
2020-05-09 08:34:47 -04:00
|
|
|
import { mockConn } from "./_mock_conn.ts";
|
2020-09-27 06:22:32 -04:00
|
|
|
import { dirname, fromFileUrl, join, resolve } from "../path/mod.ts";
|
2020-09-14 06:58:43 -04:00
|
|
|
|
|
|
|
const moduleDir = dirname(fromFileUrl(import.meta.url));
|
|
|
|
const testdataDir = resolve(moduleDir, "testdata");
|
2020-02-26 10:48:35 -05:00
|
|
|
|
2018-12-17 11:49:10 -05:00
|
|
|
interface ResponseTest {
|
2019-02-19 12:38:19 -05:00
|
|
|
response: Response;
|
2018-12-17 11:49:10 -05:00
|
|
|
raw: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const responseTests: ResponseTest[] = [
|
|
|
|
// Default response
|
|
|
|
{
|
|
|
|
response: {},
|
2020-03-28 13:03:49 -04:00
|
|
|
raw: "HTTP/1.1 200 OK\r\n" + "content-length: 0" + "\r\n\r\n",
|
2019-05-22 22:33:17 -04:00
|
|
|
},
|
|
|
|
// Empty body with status
|
|
|
|
{
|
|
|
|
response: {
|
2020-03-28 13:03:49 -04:00
|
|
|
status: 404,
|
2019-05-22 22:33:17 -04:00
|
|
|
},
|
2020-03-28 13:03:49 -04:00
|
|
|
raw: "HTTP/1.1 404 Not Found\r\n" + "content-length: 0" + "\r\n\r\n",
|
2018-12-17 11:49:10 -05:00
|
|
|
},
|
|
|
|
// HTTP/1.1, chunked coding; empty trailer; close
|
|
|
|
{
|
|
|
|
response: {
|
|
|
|
status: 200,
|
2020-06-12 15:23:38 -04:00
|
|
|
body: new Deno.Buffer(new TextEncoder().encode("abcdef")),
|
2018-12-17 11:49:10 -05:00
|
|
|
},
|
|
|
|
|
2020-07-14 15:24:17 -04:00
|
|
|
raw: "HTTP/1.1 200 OK\r\n" +
|
2018-12-17 11:49:10 -05:00
|
|
|
"transfer-encoding: chunked\r\n\r\n" +
|
2020-03-28 13:03:49 -04:00
|
|
|
"6\r\nabcdef\r\n0\r\n\r\n",
|
|
|
|
},
|
2018-12-17 11:49:10 -05:00
|
|
|
];
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("responseWrite", async function (): Promise<void> {
|
2019-02-19 12:38:19 -05:00
|
|
|
for (const testCase of responseTests) {
|
2020-06-12 15:23:38 -04:00
|
|
|
const buf = new Deno.Buffer();
|
2019-02-19 12:38:19 -05:00
|
|
|
const bufw = new BufWriter(buf);
|
|
|
|
const request = new ServerRequest();
|
|
|
|
request.w = bufw;
|
2019-05-20 09:17:26 -04:00
|
|
|
|
2020-02-24 22:49:39 -05:00
|
|
|
request.conn = mockConn();
|
2019-08-05 18:03:55 -04:00
|
|
|
|
2019-02-19 12:38:19 -05:00
|
|
|
await request.respond(testCase.response);
|
2020-04-29 16:38:10 -04:00
|
|
|
assertEquals(new TextDecoder().decode(buf.bytes()), testCase.raw);
|
2019-05-20 09:17:26 -04:00
|
|
|
await request.done;
|
2019-02-19 12:38:19 -05:00
|
|
|
}
|
2019-02-15 11:03:57 -05:00
|
|
|
});
|
2018-12-18 20:48:05 -05:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("requestContentLength", function (): void {
|
2020-01-02 12:34:33 -05:00
|
|
|
// Has content length
|
|
|
|
{
|
|
|
|
const req = new ServerRequest();
|
|
|
|
req.headers = new Headers();
|
|
|
|
req.headers.set("content-length", "5");
|
2020-06-12 15:23:38 -04:00
|
|
|
const buf = new Deno.Buffer(encode("Hello"));
|
2020-01-02 12:34:33 -05:00
|
|
|
req.r = new BufReader(buf);
|
|
|
|
assertEquals(req.contentLength, 5);
|
|
|
|
}
|
|
|
|
// No content length
|
|
|
|
{
|
|
|
|
const shortText = "Hello";
|
|
|
|
const req = new ServerRequest();
|
|
|
|
req.headers = new Headers();
|
|
|
|
req.headers.set("transfer-encoding", "chunked");
|
|
|
|
let chunksData = "";
|
|
|
|
let chunkOffset = 0;
|
|
|
|
const maxChunkSize = 70;
|
|
|
|
while (chunkOffset < shortText.length) {
|
|
|
|
const chunkSize = Math.min(maxChunkSize, shortText.length - chunkOffset);
|
2020-07-14 15:24:17 -04:00
|
|
|
chunksData += `${chunkSize.toString(16)}\r\n${
|
|
|
|
shortText.substr(chunkOffset, chunkSize)
|
|
|
|
}\r\n`;
|
2020-01-02 12:34:33 -05:00
|
|
|
chunkOffset += chunkSize;
|
|
|
|
}
|
|
|
|
chunksData += "0\r\n\r\n";
|
2020-06-12 15:23:38 -04:00
|
|
|
const buf = new Deno.Buffer(encode(chunksData));
|
2020-01-02 12:34:33 -05:00
|
|
|
req.r = new BufReader(buf);
|
|
|
|
assertEquals(req.contentLength, null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-02-24 22:49:39 -05:00
|
|
|
interface TotalReader extends Deno.Reader {
|
|
|
|
total: number;
|
|
|
|
}
|
|
|
|
function totalReader(r: Deno.Reader): TotalReader {
|
|
|
|
let _total = 0;
|
2020-04-28 12:40:43 -04:00
|
|
|
async function read(p: Uint8Array): Promise<number | null> {
|
2020-02-24 22:49:39 -05:00
|
|
|
const result = await r.read(p);
|
|
|
|
if (typeof result === "number") {
|
|
|
|
_total += result;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
read,
|
|
|
|
get total(): number {
|
|
|
|
return _total;
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-02-24 22:49:39 -05:00
|
|
|
};
|
|
|
|
}
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("requestBodyWithContentLength", async function (): Promise<void> {
|
2019-02-19 12:38:19 -05:00
|
|
|
{
|
|
|
|
const req = new ServerRequest();
|
|
|
|
req.headers = new Headers();
|
|
|
|
req.headers.set("content-length", "5");
|
2020-06-12 15:23:38 -04:00
|
|
|
const buf = new Deno.Buffer(encode("Hello"));
|
2019-02-19 12:38:19 -05:00
|
|
|
req.r = new BufReader(buf);
|
2020-02-26 10:48:35 -05:00
|
|
|
const body = decode(await Deno.readAll(req.body));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(body, "Hello");
|
2019-02-19 12:38:19 -05:00
|
|
|
}
|
2018-12-18 20:48:05 -05:00
|
|
|
|
2019-02-19 12:38:19 -05:00
|
|
|
// Larger than internal buf
|
|
|
|
{
|
|
|
|
const longText = "1234\n".repeat(1000);
|
|
|
|
const req = new ServerRequest();
|
|
|
|
req.headers = new Headers();
|
|
|
|
req.headers.set("Content-Length", "5000");
|
2020-06-12 15:23:38 -04:00
|
|
|
const buf = new Deno.Buffer(encode(longText));
|
2019-02-19 12:38:19 -05:00
|
|
|
req.r = new BufReader(buf);
|
2020-02-26 10:48:35 -05:00
|
|
|
const body = decode(await Deno.readAll(req.body));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(body, longText);
|
2019-02-19 12:38:19 -05:00
|
|
|
}
|
2020-02-24 22:49:39 -05:00
|
|
|
// Handler ignored to consume body
|
|
|
|
});
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
|
|
|
"ServerRequest.finalize() should consume unread body / content-length",
|
|
|
|
async () => {
|
|
|
|
const text = "deno.land";
|
|
|
|
const req = new ServerRequest();
|
|
|
|
req.headers = new Headers();
|
|
|
|
req.headers.set("content-length", "" + text.length);
|
|
|
|
const tr = totalReader(new Deno.Buffer(encode(text)));
|
|
|
|
req.r = new BufReader(tr);
|
|
|
|
req.w = new BufWriter(new Deno.Buffer());
|
|
|
|
await req.respond({ status: 200, body: "ok" });
|
|
|
|
assertEquals(tr.total, 0);
|
|
|
|
await req.finalize();
|
|
|
|
assertEquals(tr.total, text.length);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
|
|
|
Deno.test(
|
|
|
|
"ServerRequest.finalize() should consume unread body / chunked, trailers",
|
|
|
|
async () => {
|
|
|
|
const text = [
|
|
|
|
"5",
|
|
|
|
"Hello",
|
|
|
|
"4",
|
|
|
|
"Deno",
|
|
|
|
"0",
|
|
|
|
"",
|
|
|
|
"deno: land",
|
|
|
|
"node: js",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
].join("\r\n");
|
|
|
|
const req = new ServerRequest();
|
|
|
|
req.headers = new Headers();
|
|
|
|
req.headers.set("transfer-encoding", "chunked");
|
|
|
|
req.headers.set("trailer", "deno,node");
|
|
|
|
const body = encode(text);
|
|
|
|
const tr = totalReader(new Deno.Buffer(body));
|
|
|
|
req.r = new BufReader(tr);
|
|
|
|
req.w = new BufWriter(new Deno.Buffer());
|
|
|
|
await req.respond({ status: 200, body: "ok" });
|
|
|
|
assertEquals(tr.total, 0);
|
|
|
|
assertEquals(req.headers.has("trailer"), true);
|
|
|
|
assertEquals(req.headers.has("deno"), false);
|
|
|
|
assertEquals(req.headers.has("node"), false);
|
|
|
|
await req.finalize();
|
|
|
|
assertEquals(tr.total, body.byteLength);
|
|
|
|
assertEquals(req.headers.has("trailer"), false);
|
|
|
|
assertEquals(req.headers.get("deno"), "land");
|
|
|
|
assertEquals(req.headers.get("node"), "js");
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
|
|
|
Deno.test("requestBodyWithTransferEncoding", async function (): Promise<void> {
|
2019-02-19 12:38:19 -05:00
|
|
|
{
|
|
|
|
const shortText = "Hello";
|
|
|
|
const req = new ServerRequest();
|
|
|
|
req.headers = new Headers();
|
|
|
|
req.headers.set("transfer-encoding", "chunked");
|
|
|
|
let chunksData = "";
|
|
|
|
let chunkOffset = 0;
|
|
|
|
const maxChunkSize = 70;
|
|
|
|
while (chunkOffset < shortText.length) {
|
|
|
|
const chunkSize = Math.min(maxChunkSize, shortText.length - chunkOffset);
|
2020-07-14 15:24:17 -04:00
|
|
|
chunksData += `${chunkSize.toString(16)}\r\n${
|
|
|
|
shortText.substr(chunkOffset, chunkSize)
|
|
|
|
}\r\n`;
|
2019-02-19 12:38:19 -05:00
|
|
|
chunkOffset += chunkSize;
|
2018-12-18 20:48:05 -05:00
|
|
|
}
|
2019-02-19 12:38:19 -05:00
|
|
|
chunksData += "0\r\n\r\n";
|
2020-06-12 15:23:38 -04:00
|
|
|
const buf = new Deno.Buffer(encode(chunksData));
|
2019-02-19 12:38:19 -05:00
|
|
|
req.r = new BufReader(buf);
|
2020-02-26 10:48:35 -05:00
|
|
|
const body = decode(await Deno.readAll(req.body));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(body, shortText);
|
2018-12-18 20:48:05 -05:00
|
|
|
}
|
|
|
|
|
2019-02-19 12:38:19 -05:00
|
|
|
// Larger than internal buf
|
|
|
|
{
|
|
|
|
const longText = "1234\n".repeat(1000);
|
|
|
|
const req = new ServerRequest();
|
|
|
|
req.headers = new Headers();
|
|
|
|
req.headers.set("transfer-encoding", "chunked");
|
|
|
|
let chunksData = "";
|
|
|
|
let chunkOffset = 0;
|
|
|
|
const maxChunkSize = 70;
|
|
|
|
while (chunkOffset < longText.length) {
|
|
|
|
const chunkSize = Math.min(maxChunkSize, longText.length - chunkOffset);
|
2020-07-14 15:24:17 -04:00
|
|
|
chunksData += `${chunkSize.toString(16)}\r\n${
|
|
|
|
longText.substr(chunkOffset, chunkSize)
|
|
|
|
}\r\n`;
|
2019-02-19 12:38:19 -05:00
|
|
|
chunkOffset += chunkSize;
|
2018-12-18 20:48:05 -05:00
|
|
|
}
|
2019-02-19 12:38:19 -05:00
|
|
|
chunksData += "0\r\n\r\n";
|
2020-06-12 15:23:38 -04:00
|
|
|
const buf = new Deno.Buffer(encode(chunksData));
|
2019-02-19 12:38:19 -05:00
|
|
|
req.r = new BufReader(buf);
|
2020-02-26 10:48:35 -05:00
|
|
|
const body = decode(await Deno.readAll(req.body));
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(body, longText);
|
2019-02-19 12:38:19 -05:00
|
|
|
}
|
2019-02-15 11:03:57 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("requestBodyReaderWithContentLength", async function (): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2019-02-19 12:38:19 -05:00
|
|
|
{
|
|
|
|
const shortText = "Hello";
|
|
|
|
const req = new ServerRequest();
|
|
|
|
req.headers = new Headers();
|
|
|
|
req.headers.set("content-length", "" + shortText.length);
|
2020-06-12 15:23:38 -04:00
|
|
|
const buf = new Deno.Buffer(encode(shortText));
|
2019-02-19 12:38:19 -05:00
|
|
|
req.r = new BufReader(buf);
|
2020-01-02 12:34:33 -05:00
|
|
|
const readBuf = new Uint8Array(6);
|
2019-02-19 12:38:19 -05:00
|
|
|
let offset = 0;
|
2020-01-02 12:34:33 -05:00
|
|
|
while (offset < shortText.length) {
|
|
|
|
const nread = await req.body.read(readBuf);
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(nread !== null);
|
2020-02-26 10:48:35 -05:00
|
|
|
const s = decode(readBuf.subarray(0, nread as number));
|
2020-01-02 12:34:33 -05:00
|
|
|
assertEquals(shortText.substr(offset, nread as number), s);
|
|
|
|
offset += nread as number;
|
2019-02-19 12:38:19 -05:00
|
|
|
}
|
2020-01-02 12:34:33 -05:00
|
|
|
const nread = await req.body.read(readBuf);
|
2020-04-28 12:40:43 -04:00
|
|
|
assertEquals(nread, null);
|
2019-02-19 12:38:19 -05:00
|
|
|
}
|
2018-12-18 20:48:05 -05:00
|
|
|
|
2020-01-02 12:34:33 -05:00
|
|
|
// Larger than given buf
|
2018-12-18 20:48:05 -05:00
|
|
|
{
|
2019-02-19 12:38:19 -05:00
|
|
|
const longText = "1234\n".repeat(1000);
|
|
|
|
const req = new ServerRequest();
|
|
|
|
req.headers = new Headers();
|
|
|
|
req.headers.set("Content-Length", "5000");
|
2020-06-12 15:23:38 -04:00
|
|
|
const buf = new Deno.Buffer(encode(longText));
|
2019-02-19 12:38:19 -05:00
|
|
|
req.r = new BufReader(buf);
|
2020-01-02 12:34:33 -05:00
|
|
|
const readBuf = new Uint8Array(1000);
|
2019-02-19 12:38:19 -05:00
|
|
|
let offset = 0;
|
2020-01-02 12:34:33 -05:00
|
|
|
while (offset < longText.length) {
|
|
|
|
const nread = await req.body.read(readBuf);
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(nread !== null);
|
2020-02-26 10:48:35 -05:00
|
|
|
const s = decode(readBuf.subarray(0, nread as number));
|
2020-01-02 12:34:33 -05:00
|
|
|
assertEquals(longText.substr(offset, nread as number), s);
|
|
|
|
offset += nread as number;
|
2019-02-19 12:38:19 -05:00
|
|
|
}
|
2020-01-02 12:34:33 -05:00
|
|
|
const nread = await req.body.read(readBuf);
|
2020-04-28 12:40:43 -04:00
|
|
|
assertEquals(nread, null);
|
2018-12-18 20:48:05 -05:00
|
|
|
}
|
2019-02-19 12:38:19 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("requestBodyReaderWithTransferEncoding", async function (): Promise<
|
|
|
|
void
|
|
|
|
> {
|
2018-12-18 20:48:05 -05:00
|
|
|
{
|
2019-02-19 12:38:19 -05:00
|
|
|
const shortText = "Hello";
|
|
|
|
const req = new ServerRequest();
|
|
|
|
req.headers = new Headers();
|
|
|
|
req.headers.set("transfer-encoding", "chunked");
|
|
|
|
let chunksData = "";
|
|
|
|
let chunkOffset = 0;
|
|
|
|
const maxChunkSize = 70;
|
|
|
|
while (chunkOffset < shortText.length) {
|
|
|
|
const chunkSize = Math.min(maxChunkSize, shortText.length - chunkOffset);
|
2020-07-14 15:24:17 -04:00
|
|
|
chunksData += `${chunkSize.toString(16)}\r\n${
|
|
|
|
shortText.substr(chunkOffset, chunkSize)
|
|
|
|
}\r\n`;
|
2019-02-19 12:38:19 -05:00
|
|
|
chunkOffset += chunkSize;
|
|
|
|
}
|
|
|
|
chunksData += "0\r\n\r\n";
|
2020-06-12 15:23:38 -04:00
|
|
|
const buf = new Deno.Buffer(encode(chunksData));
|
2019-02-19 12:38:19 -05:00
|
|
|
req.r = new BufReader(buf);
|
2020-01-02 12:34:33 -05:00
|
|
|
const readBuf = new Uint8Array(6);
|
2019-02-19 12:38:19 -05:00
|
|
|
let offset = 0;
|
2020-01-02 12:34:33 -05:00
|
|
|
while (offset < shortText.length) {
|
|
|
|
const nread = await req.body.read(readBuf);
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(nread !== null);
|
2020-02-26 10:48:35 -05:00
|
|
|
const s = decode(readBuf.subarray(0, nread as number));
|
2020-01-02 12:34:33 -05:00
|
|
|
assertEquals(shortText.substr(offset, nread as number), s);
|
|
|
|
offset += nread as number;
|
2019-02-19 12:38:19 -05:00
|
|
|
}
|
2020-01-02 12:34:33 -05:00
|
|
|
const nread = await req.body.read(readBuf);
|
2020-04-28 12:40:43 -04:00
|
|
|
assertEquals(nread, null);
|
2019-02-15 11:03:57 -05:00
|
|
|
}
|
2019-02-19 12:38:19 -05:00
|
|
|
|
|
|
|
// Larger than internal buf
|
2019-02-15 11:03:57 -05:00
|
|
|
{
|
2019-02-19 12:38:19 -05:00
|
|
|
const longText = "1234\n".repeat(1000);
|
|
|
|
const req = new ServerRequest();
|
|
|
|
req.headers = new Headers();
|
|
|
|
req.headers.set("transfer-encoding", "chunked");
|
|
|
|
let chunksData = "";
|
|
|
|
let chunkOffset = 0;
|
|
|
|
const maxChunkSize = 70;
|
|
|
|
while (chunkOffset < longText.length) {
|
|
|
|
const chunkSize = Math.min(maxChunkSize, longText.length - chunkOffset);
|
2020-07-14 15:24:17 -04:00
|
|
|
chunksData += `${chunkSize.toString(16)}\r\n${
|
|
|
|
longText.substr(chunkOffset, chunkSize)
|
|
|
|
}\r\n`;
|
2019-02-19 12:38:19 -05:00
|
|
|
chunkOffset += chunkSize;
|
|
|
|
}
|
|
|
|
chunksData += "0\r\n\r\n";
|
2020-06-12 15:23:38 -04:00
|
|
|
const buf = new Deno.Buffer(encode(chunksData));
|
2019-02-19 12:38:19 -05:00
|
|
|
req.r = new BufReader(buf);
|
2020-01-02 12:34:33 -05:00
|
|
|
const readBuf = new Uint8Array(1000);
|
2019-02-19 12:38:19 -05:00
|
|
|
let offset = 0;
|
2020-01-02 12:34:33 -05:00
|
|
|
while (offset < longText.length) {
|
|
|
|
const nread = await req.body.read(readBuf);
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(nread !== null);
|
2020-02-26 10:48:35 -05:00
|
|
|
const s = decode(readBuf.subarray(0, nread as number));
|
2020-01-02 12:34:33 -05:00
|
|
|
assertEquals(longText.substr(offset, nread as number), s);
|
|
|
|
offset += nread as number;
|
2019-02-19 12:38:19 -05:00
|
|
|
}
|
2020-01-02 12:34:33 -05:00
|
|
|
const nread = await req.body.read(readBuf);
|
2020-04-28 12:40:43 -04:00
|
|
|
assertEquals(nread, null);
|
2018-12-18 20:48:05 -05:00
|
|
|
}
|
|
|
|
});
|
2019-05-10 12:55:18 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-18 19:25:55 -04:00
|
|
|
name: "destroyed connection",
|
|
|
|
fn: async (): Promise<void> => {
|
|
|
|
// Runs a simple server as another process
|
|
|
|
const p = Deno.run({
|
2020-05-17 13:11:24 -04:00
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"run",
|
|
|
|
"--allow-net",
|
2020-09-14 06:58:43 -04:00
|
|
|
"testdata/simple_server.ts",
|
2020-05-17 13:11:24 -04:00
|
|
|
],
|
2020-09-14 06:58:43 -04:00
|
|
|
cwd: moduleDir,
|
2020-03-28 13:03:49 -04:00
|
|
|
stdout: "piped",
|
2020-03-18 19:25:55 -04:00
|
|
|
});
|
2020-02-26 10:48:35 -05:00
|
|
|
|
|
|
|
let serverIsRunning = true;
|
2020-03-18 19:25:55 -04:00
|
|
|
const statusPromise = p
|
|
|
|
.status()
|
2020-02-26 10:48:35 -05:00
|
|
|
.then((): void => {
|
|
|
|
serverIsRunning = false;
|
|
|
|
})
|
|
|
|
.catch((_): void => {}); // Ignores the error when closing the process.
|
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
try {
|
2020-06-09 07:18:18 -04:00
|
|
|
const r = new TextProtoReader(new BufReader(p.stdout));
|
2020-03-18 19:25:55 -04:00
|
|
|
const s = await r.readLine();
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(s !== null && s.includes("server listening"));
|
2020-03-18 19:25:55 -04:00
|
|
|
await delay(100);
|
|
|
|
// Reqeusts to the server and immediately closes the connection
|
|
|
|
const conn = await Deno.connect({ port: 4502 });
|
|
|
|
await conn.write(new TextEncoder().encode("GET / HTTP/1.0\n\n"));
|
|
|
|
conn.close();
|
|
|
|
// Waits for the server to handle the above (broken) request
|
|
|
|
await delay(100);
|
|
|
|
assert(serverIsRunning);
|
|
|
|
} finally {
|
|
|
|
// Stops the sever and allows `p.status()` promise to resolve
|
|
|
|
Deno.kill(p.pid, Deno.Signal.SIGKILL);
|
|
|
|
await statusPromise;
|
2020-06-09 07:18:18 -04:00
|
|
|
p.stdout.close();
|
2020-03-18 19:25:55 -04:00
|
|
|
p.close();
|
|
|
|
}
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-05-22 19:28:03 -04:00
|
|
|
});
|
2019-05-25 12:22:30 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-18 19:25:55 -04:00
|
|
|
name: "serveTLS",
|
|
|
|
fn: async (): Promise<void> => {
|
|
|
|
// Runs a simple server as another process
|
|
|
|
const p = Deno.run({
|
2020-03-21 17:44:18 -04:00
|
|
|
cmd: [
|
2020-03-18 19:25:55 -04:00
|
|
|
Deno.execPath(),
|
2020-05-17 13:11:24 -04:00
|
|
|
"run",
|
2020-03-18 19:25:55 -04:00
|
|
|
"--allow-net",
|
|
|
|
"--allow-read",
|
2020-09-14 06:58:43 -04:00
|
|
|
"testdata/simple_https_server.ts",
|
2020-03-18 19:25:55 -04:00
|
|
|
],
|
2020-09-14 06:58:43 -04:00
|
|
|
cwd: moduleDir,
|
2020-03-28 13:03:49 -04:00
|
|
|
stdout: "piped",
|
2020-03-18 19:25:55 -04:00
|
|
|
});
|
2020-02-26 10:48:35 -05:00
|
|
|
|
|
|
|
let serverIsRunning = true;
|
2020-03-18 19:25:55 -04:00
|
|
|
const statusPromise = p
|
|
|
|
.status()
|
2020-02-26 10:48:35 -05:00
|
|
|
.then((): void => {
|
|
|
|
serverIsRunning = false;
|
|
|
|
})
|
|
|
|
.catch((_): void => {}); // Ignores the error when closing the process.
|
2020-03-24 12:24:58 -04:00
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
try {
|
2020-06-09 07:18:18 -04:00
|
|
|
const r = new TextProtoReader(new BufReader(p.stdout));
|
2020-03-18 19:25:55 -04:00
|
|
|
const s = await r.readLine();
|
|
|
|
assert(
|
2020-04-28 12:40:43 -04:00
|
|
|
s !== null && s.includes("server listening"),
|
2020-07-14 15:24:17 -04:00
|
|
|
"server must be started",
|
2020-03-18 19:25:55 -04:00
|
|
|
);
|
|
|
|
// Requests to the server and immediately closes the connection
|
2020-04-24 17:29:14 -04:00
|
|
|
const conn = await Deno.connectTls({
|
2020-03-18 19:25:55 -04:00
|
|
|
hostname: "localhost",
|
2020-03-24 12:24:58 -04:00
|
|
|
port: 4503,
|
2020-09-14 06:58:43 -04:00
|
|
|
certFile: join(testdataDir, "tls/RootCA.pem"),
|
2020-03-18 19:25:55 -04:00
|
|
|
});
|
|
|
|
await Deno.writeAll(
|
|
|
|
conn,
|
2020-07-14 15:24:17 -04:00
|
|
|
new TextEncoder().encode("GET / HTTP/1.0\r\n\r\n"),
|
2020-03-18 19:25:55 -04:00
|
|
|
);
|
|
|
|
const res = new Uint8Array(100);
|
2020-04-28 12:40:43 -04:00
|
|
|
const nread = await conn.read(res);
|
|
|
|
assert(nread !== null);
|
2020-03-18 19:25:55 -04:00
|
|
|
conn.close();
|
|
|
|
const resStr = new TextDecoder().decode(res.subarray(0, nread));
|
|
|
|
assert(resStr.includes("Hello HTTPS"));
|
|
|
|
assert(serverIsRunning);
|
|
|
|
} finally {
|
|
|
|
// Stops the sever and allows `p.status()` promise to resolve
|
|
|
|
Deno.kill(p.pid, Deno.Signal.SIGKILL);
|
|
|
|
await statusPromise;
|
2020-06-09 07:18:18 -04:00
|
|
|
p.stdout.close();
|
2020-03-18 19:25:55 -04:00
|
|
|
p.close();
|
|
|
|
}
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-11-04 13:45:29 -05:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test(
|
|
|
|
"close server while iterating",
|
|
|
|
async (): Promise<void> => {
|
|
|
|
const server = serve(":8123");
|
|
|
|
const nextWhileClosing = server[Symbol.asyncIterator]().next();
|
|
|
|
server.close();
|
|
|
|
assertEquals(await nextWhileClosing, { value: undefined, done: true });
|
2019-11-09 14:40:22 -05:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
const nextAfterClosing = server[Symbol.asyncIterator]().next();
|
|
|
|
assertEquals(await nextAfterClosing, { value: undefined, done: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-12 15:23:38 -04:00
|
|
|
);
|
2019-11-09 14:40:22 -05:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-19 11:04:26 -04:00
|
|
|
name: "[http] close server while connection is open",
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
async function iteratorReq(server: Server): Promise<void> {
|
|
|
|
for await (const req of server) {
|
2020-03-20 04:46:48 -04:00
|
|
|
await req.respond({ body: new TextEncoder().encode(req.url) });
|
2020-03-19 11:04:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const server = serve(":8123");
|
2020-03-20 04:46:48 -04:00
|
|
|
const p = iteratorReq(server);
|
2020-03-19 11:04:26 -04:00
|
|
|
const conn = await Deno.connect({ hostname: "127.0.0.1", port: 8123 });
|
|
|
|
await Deno.writeAll(
|
|
|
|
conn,
|
2020-07-14 15:24:17 -04:00
|
|
|
new TextEncoder().encode("GET /hello HTTP/1.1\r\n\r\n"),
|
2020-03-19 11:04:26 -04:00
|
|
|
);
|
|
|
|
const res = new Uint8Array(100);
|
|
|
|
const nread = await conn.read(res);
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(nread !== null);
|
2020-03-19 11:04:26 -04:00
|
|
|
const resStr = new TextDecoder().decode(res.subarray(0, nread));
|
2020-10-26 11:03:30 -04:00
|
|
|
assertStringIncludes(resStr, "/hello");
|
2020-03-19 11:04:26 -04:00
|
|
|
server.close();
|
2020-03-20 04:46:48 -04:00
|
|
|
await p;
|
2020-03-19 11:04:26 -04:00
|
|
|
// Client connection should still be open, verify that
|
|
|
|
// it's visible in resource table.
|
|
|
|
const resources = Deno.resources();
|
|
|
|
assertEquals(resources[conn.rid], "tcpStream");
|
|
|
|
conn.close();
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-19 11:04:26 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-19 19:15:21 -04:00
|
|
|
name: "respond error closes connection",
|
2020-03-15 08:03:25 -04:00
|
|
|
async fn(): Promise<void> {
|
2020-02-26 10:48:35 -05:00
|
|
|
const serverRoutine = async (): Promise<void> => {
|
2020-03-24 12:24:58 -04:00
|
|
|
const server = serve(":8124");
|
2020-02-26 10:48:35 -05:00
|
|
|
for await (const req of server) {
|
2020-03-19 19:15:21 -04:00
|
|
|
await assertThrowsAsync(async () => {
|
2020-02-26 10:48:35 -05:00
|
|
|
await req.respond({
|
2020-03-19 19:15:21 -04:00
|
|
|
status: 12345,
|
2020-03-28 13:03:49 -04:00
|
|
|
body: new TextEncoder().encode("Hello World"),
|
2020-02-26 10:48:35 -05:00
|
|
|
});
|
2020-03-19 19:15:21 -04:00
|
|
|
}, Deno.errors.InvalidData);
|
|
|
|
// The connection should be destroyed
|
|
|
|
assert(!(req.conn.rid in Deno.resources()));
|
|
|
|
server.close();
|
2020-02-26 10:48:35 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
const p = serverRoutine();
|
|
|
|
const conn = await Deno.connect({
|
|
|
|
hostname: "127.0.0.1",
|
2020-03-28 13:03:49 -04:00
|
|
|
port: 8124,
|
2020-02-26 10:48:35 -05:00
|
|
|
});
|
|
|
|
await Deno.writeAll(
|
|
|
|
conn,
|
2020-07-14 15:24:17 -04:00
|
|
|
new TextEncoder().encode("GET / HTTP/1.1\r\n\r\n"),
|
2020-02-26 10:48:35 -05:00
|
|
|
);
|
2020-03-19 19:15:21 -04:00
|
|
|
conn.close();
|
2020-02-26 10:48:35 -05:00
|
|
|
await p;
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-15 08:03:25 -04:00
|
|
|
});
|
2020-04-07 06:34:18 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-04-07 06:34:18 -04:00
|
|
|
name: "[http] request error gets 400 response",
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
const server = serve(":8124");
|
|
|
|
const entry = server[Symbol.asyncIterator]().next();
|
|
|
|
const conn = await Deno.connect({
|
|
|
|
hostname: "127.0.0.1",
|
|
|
|
port: 8124,
|
|
|
|
});
|
|
|
|
await Deno.writeAll(
|
|
|
|
conn,
|
2020-07-14 15:24:17 -04:00
|
|
|
encode("GET / HTTP/1.1\r\nmalformedHeader\r\n\r\n\r\n\r\n"),
|
2020-04-07 06:34:18 -04:00
|
|
|
);
|
|
|
|
const responseString = decode(await Deno.readAll(conn));
|
|
|
|
assertMatch(
|
|
|
|
responseString,
|
2020-07-14 15:24:17 -04:00
|
|
|
/^HTTP\/1\.1 400 Bad Request\r\ncontent-length: \d+\r\n\r\n.*\r\n\r\n$/ms,
|
2020-04-07 06:34:18 -04:00
|
|
|
);
|
|
|
|
conn.close();
|
|
|
|
server.close();
|
|
|
|
assert((await entry).done);
|
|
|
|
},
|
|
|
|
});
|
2020-06-08 11:58:52 -04:00
|
|
|
|
2020-11-18 11:47:47 -05:00
|
|
|
Deno.test({
|
|
|
|
name: "[http] finalizing invalid chunked data closes connection",
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
const serverRoutine = async (): Promise<void> => {
|
|
|
|
const server = serve(":8124");
|
|
|
|
for await (const req of server) {
|
|
|
|
await req.respond({ status: 200, body: "Hello, world!" });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
server.close();
|
|
|
|
};
|
|
|
|
const p = serverRoutine();
|
|
|
|
const conn = await Deno.connect({
|
|
|
|
hostname: "127.0.0.1",
|
|
|
|
port: 8124,
|
|
|
|
});
|
|
|
|
await Deno.writeAll(
|
|
|
|
conn,
|
|
|
|
encode(
|
|
|
|
"PUT / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\nzzzzzzz\r\nhello",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
await conn.closeWrite();
|
|
|
|
const responseString = decode(await Deno.readAll(conn));
|
|
|
|
assertEquals(
|
|
|
|
responseString,
|
|
|
|
"HTTP/1.1 200 OK\r\ncontent-length: 13\r\n\r\nHello, world!",
|
|
|
|
);
|
|
|
|
conn.close();
|
|
|
|
await p;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "[http] finalizing chunked unexpected EOF closes connection",
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
const serverRoutine = async (): Promise<void> => {
|
|
|
|
const server = serve(":8124");
|
|
|
|
for await (const req of server) {
|
|
|
|
await req.respond({ status: 200, body: "Hello, world!" });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
server.close();
|
|
|
|
};
|
|
|
|
const p = serverRoutine();
|
|
|
|
const conn = await Deno.connect({
|
|
|
|
hostname: "127.0.0.1",
|
|
|
|
port: 8124,
|
|
|
|
});
|
|
|
|
await Deno.writeAll(
|
|
|
|
conn,
|
|
|
|
encode("PUT / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nHello"),
|
|
|
|
);
|
|
|
|
conn.closeWrite();
|
|
|
|
const responseString = decode(await Deno.readAll(conn));
|
|
|
|
assertEquals(
|
|
|
|
responseString,
|
|
|
|
"HTTP/1.1 200 OK\r\ncontent-length: 13\r\n\r\nHello, world!",
|
|
|
|
);
|
|
|
|
conn.close();
|
|
|
|
await p;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name:
|
|
|
|
"[http] receiving bad request from a closed connection should not throw",
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
const server = serve(":8124");
|
|
|
|
const serverRoutine = async (): Promise<void> => {
|
|
|
|
for await (const req of server) {
|
|
|
|
await req.respond({ status: 200, body: "Hello, world!" });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const p = serverRoutine();
|
|
|
|
const conn = await Deno.connect({
|
|
|
|
hostname: "127.0.0.1",
|
|
|
|
port: 8124,
|
|
|
|
});
|
|
|
|
await Deno.writeAll(
|
|
|
|
conn,
|
|
|
|
encode([
|
|
|
|
// A normal request is required:
|
|
|
|
"GET / HTTP/1.1",
|
|
|
|
"Host: localhost",
|
|
|
|
"",
|
|
|
|
// The bad request:
|
|
|
|
"GET / HTTP/1.1",
|
|
|
|
"Host: localhost",
|
|
|
|
"INVALID!HEADER!",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
].join("\r\n")),
|
|
|
|
);
|
|
|
|
// After sending the two requests, don't receive the reponses.
|
|
|
|
|
|
|
|
// Closing the connection now.
|
|
|
|
conn.close();
|
|
|
|
|
|
|
|
// The server will write responses to the closed connection,
|
|
|
|
// the first few `write()` calls will not throws, until the server received
|
|
|
|
// the TCP RST. So we need the normal request before the bad request to
|
|
|
|
// make the server do a few writes before it writes that `400` response.
|
|
|
|
|
|
|
|
// Wait for server to handle requests.
|
|
|
|
await delay(10);
|
|
|
|
|
|
|
|
server.close();
|
|
|
|
await p;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-06-08 11:58:52 -04:00
|
|
|
name: "serveTLS Invalid Cert",
|
|
|
|
fn: async (): Promise<void> => {
|
2020-06-08 18:10:29 -04:00
|
|
|
async function iteratorReq(server: Server): Promise<void> {
|
|
|
|
for await (const req of server) {
|
|
|
|
await req.respond({ body: new TextEncoder().encode("Hello HTTPS") });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const port = 9122;
|
|
|
|
const tlsOptions = {
|
|
|
|
hostname: "localhost",
|
|
|
|
port,
|
2020-09-14 06:58:43 -04:00
|
|
|
certFile: join(testdataDir, "tls/localhost.crt"),
|
|
|
|
keyFile: join(testdataDir, "tls/localhost.key"),
|
2020-06-08 18:10:29 -04:00
|
|
|
};
|
|
|
|
const server = serveTLS(tlsOptions);
|
|
|
|
const p = iteratorReq(server);
|
2020-06-08 11:58:52 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
// Invalid certificate, connection should throw
|
|
|
|
// but should not crash the server
|
|
|
|
assertThrowsAsync(
|
|
|
|
() =>
|
|
|
|
Deno.connectTls({
|
|
|
|
hostname: "localhost",
|
2020-06-08 18:10:29 -04:00
|
|
|
port,
|
2020-06-08 11:58:52 -04:00
|
|
|
// certFile
|
|
|
|
}),
|
2020-07-14 15:24:17 -04:00
|
|
|
Deno.errors.InvalidData,
|
2020-06-08 11:58:52 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// Valid request after invalid
|
|
|
|
const conn = await Deno.connectTls({
|
|
|
|
hostname: "localhost",
|
2020-06-08 18:10:29 -04:00
|
|
|
port,
|
2020-09-14 06:58:43 -04:00
|
|
|
certFile: join(testdataDir, "tls/RootCA.pem"),
|
2020-06-08 11:58:52 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
await Deno.writeAll(
|
|
|
|
conn,
|
2020-07-14 15:24:17 -04:00
|
|
|
new TextEncoder().encode("GET / HTTP/1.0\r\n\r\n"),
|
2020-06-08 11:58:52 -04:00
|
|
|
);
|
|
|
|
const res = new Uint8Array(100);
|
|
|
|
const nread = await conn.read(res);
|
|
|
|
assert(nread !== null);
|
|
|
|
conn.close();
|
|
|
|
const resStr = new TextDecoder().decode(res.subarray(0, nread));
|
|
|
|
assert(resStr.includes("Hello HTTPS"));
|
|
|
|
} finally {
|
|
|
|
// Stops the sever and allows `p.status()` promise to resolve
|
2020-06-08 18:10:29 -04:00
|
|
|
server.close();
|
|
|
|
await p;
|
2020-06-08 11:58:52 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2020-06-29 10:39:17 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "server.serve() should be able to parse IPV4 address",
|
|
|
|
fn: (): void => {
|
|
|
|
const server = serve("127.0.0.1:8124");
|
|
|
|
const expected = {
|
|
|
|
hostname: "127.0.0.1",
|
|
|
|
port: 8124,
|
|
|
|
transport: "tcp",
|
|
|
|
};
|
2020-07-09 03:37:50 -04:00
|
|
|
assertEquals(server.listener.addr, expected);
|
2020-06-29 10:39:17 -04:00
|
|
|
server.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2020-07-09 03:37:50 -04:00
|
|
|
name: "server._parseAddrFromStr() should be able to parse IPV6 address",
|
2020-06-29 10:39:17 -04:00
|
|
|
fn: (): void => {
|
|
|
|
const addr = _parseAddrFromStr("[::1]:8124");
|
|
|
|
const expected = {
|
|
|
|
hostname: "[::1]",
|
|
|
|
port: 8124,
|
|
|
|
};
|
2020-07-09 03:37:50 -04:00
|
|
|
assertEquals(addr, expected);
|
2020-06-29 10:39:17 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "server.serve() should be able to parse IPV6 address",
|
|
|
|
fn: (): void => {
|
|
|
|
const server = serve("[::1]:8124");
|
|
|
|
const expected = {
|
|
|
|
hostname: "::1",
|
|
|
|
port: 8124,
|
|
|
|
transport: "tcp",
|
|
|
|
};
|
2020-07-09 03:37:50 -04:00
|
|
|
assertEquals(server.listener.addr, expected);
|
2020-06-29 10:39:17 -04:00
|
|
|
server.close();
|
|
|
|
},
|
|
|
|
});
|
2020-07-09 03:37:50 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "server._parseAddrFromStr() port 80",
|
|
|
|
fn: (): void => {
|
|
|
|
const addr = _parseAddrFromStr(":80");
|
|
|
|
assertEquals(addr.port, 80);
|
2020-07-10 22:37:20 -04:00
|
|
|
assertEquals(addr.hostname, "0.0.0.0");
|
2020-07-09 03:37:50 -04:00
|
|
|
},
|
|
|
|
});
|