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-03-19 19:15:21 -04:00
|
|
|
assertStrContains,
|
2020-03-28 13:03:49 -04:00
|
|
|
assertThrowsAsync,
|
2020-03-19 11:04:26 -04:00
|
|
|
} from "../testing/asserts.ts";
|
|
|
|
import { Response, ServerRequest, Server, serve } from "./server.ts";
|
2020-02-26 10:48:35 -05:00
|
|
|
import { BufReader, BufWriter } from "../io/bufio.ts";
|
2020-03-19 19:15:21 -04:00
|
|
|
import { delay } from "../util/async.ts";
|
2020-04-01 15:23:39 -04:00
|
|
|
import { encode, decode } from "../encoding/utf8.ts";
|
2020-02-26 10:48:35 -05:00
|
|
|
import { mockConn } from "./mock.ts";
|
|
|
|
|
|
|
|
const { Buffer, test } = Deno;
|
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-03-28 13:03:49 -04:00
|
|
|
body: new Buffer(new TextEncoder().encode("abcdef")),
|
2018-12-17 11:49:10 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
raw:
|
|
|
|
"HTTP/1.1 200 OK\r\n" +
|
|
|
|
"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-04-28 06:33:09 -04:00
|
|
|
test("responseWrite", async function (): Promise<void> {
|
2019-02-19 12:38:19 -05:00
|
|
|
for (const testCase of responseTests) {
|
2018-12-17 11:49:10 -05:00
|
|
|
const buf = new 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-04-28 06:33:09 -04:00
|
|
|
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-02-26 10:48:35 -05:00
|
|
|
const buf = new 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);
|
|
|
|
chunksData += `${chunkSize.toString(16)}\r\n${shortText.substr(
|
|
|
|
chunkOffset,
|
|
|
|
chunkSize
|
|
|
|
)}\r\n`;
|
|
|
|
chunkOffset += chunkSize;
|
|
|
|
}
|
|
|
|
chunksData += "0\r\n\r\n";
|
2020-02-26 10:48:35 -05:00
|
|
|
const buf = new 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-04-28 06:33:09 -04:00
|
|
|
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-02-26 10:48:35 -05:00
|
|
|
const buf = new 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-02-26 10:48:35 -05:00
|
|
|
const buf = new 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
|
|
|
|
});
|
|
|
|
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 Buffer(encode(text)));
|
|
|
|
req.r = new BufReader(tr);
|
|
|
|
req.w = new BufWriter(new Buffer());
|
|
|
|
await req.respond({ status: 200, body: "ok" });
|
|
|
|
assertEquals(tr.total, 0);
|
|
|
|
await req.finalize();
|
|
|
|
assertEquals(tr.total, text.length);
|
|
|
|
});
|
|
|
|
test("ServerRequest.finalize() should consume unread body / chunked, trailers", async () => {
|
|
|
|
const text = [
|
|
|
|
"5",
|
|
|
|
"Hello",
|
|
|
|
"4",
|
|
|
|
"Deno",
|
|
|
|
"0",
|
|
|
|
"",
|
|
|
|
"deno: land",
|
|
|
|
"node: js",
|
|
|
|
"",
|
2020-03-28 13:03:49 -04:00
|
|
|
"",
|
2020-02-24 22:49:39 -05:00
|
|
|
].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 Buffer(body));
|
|
|
|
req.r = new BufReader(tr);
|
|
|
|
req.w = new BufWriter(new 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");
|
2019-02-18 18:32:31 -05:00
|
|
|
});
|
2020-04-28 06:33:09 -04:00
|
|
|
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);
|
|
|
|
chunksData += `${chunkSize.toString(16)}\r\n${shortText.substr(
|
|
|
|
chunkOffset,
|
|
|
|
chunkSize
|
|
|
|
)}\r\n`;
|
|
|
|
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-02-26 10:48:35 -05:00
|
|
|
const buf = new 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);
|
|
|
|
chunksData += `${chunkSize.toString(16)}\r\n${longText.substr(
|
|
|
|
chunkOffset,
|
|
|
|
chunkSize
|
|
|
|
)}\r\n`;
|
|
|
|
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-02-26 10:48:35 -05:00
|
|
|
const buf = new 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-04-28 06:33:09 -04:00
|
|
|
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-02-26 10:48:35 -05:00
|
|
|
const buf = new 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-02-26 10:48:35 -05:00
|
|
|
const buf = new 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-04-28 06:33:09 -04:00
|
|
|
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);
|
|
|
|
chunksData += `${chunkSize.toString(16)}\r\n${shortText.substr(
|
|
|
|
chunkOffset,
|
|
|
|
chunkSize
|
|
|
|
)}\r\n`;
|
|
|
|
chunkOffset += chunkSize;
|
|
|
|
}
|
|
|
|
chunksData += "0\r\n\r\n";
|
2020-02-26 10:48:35 -05:00
|
|
|
const buf = new 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);
|
|
|
|
chunksData += `${chunkSize.toString(16)}\r\n${longText.substr(
|
|
|
|
chunkOffset,
|
|
|
|
chunkSize
|
|
|
|
)}\r\n`;
|
|
|
|
chunkOffset += chunkSize;
|
|
|
|
}
|
|
|
|
chunksData += "0\r\n\r\n";
|
2020-02-26 10:48:35 -05:00
|
|
|
const buf = new 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-03-18 19:25:55 -04:00
|
|
|
test({
|
|
|
|
name: "destroyed connection",
|
|
|
|
// FIXME(bartlomieju): hangs on windows, cause can't do `Deno.kill`
|
2020-03-19 05:58:12 -04:00
|
|
|
ignore: true,
|
2020-03-18 19:25:55 -04:00
|
|
|
fn: async (): Promise<void> => {
|
|
|
|
// Runs a simple server as another process
|
|
|
|
const p = Deno.run({
|
2020-03-24 12:24:58 -04:00
|
|
|
cmd: [Deno.execPath(), "--allow-net", "http/testdata/simple_server.ts"],
|
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 {
|
|
|
|
const r = new TextProtoReader(new BufReader(p.stdout!));
|
|
|
|
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;
|
|
|
|
p.stdout!.close();
|
|
|
|
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-03-18 19:25:55 -04:00
|
|
|
test({
|
|
|
|
name: "serveTLS",
|
|
|
|
// FIXME(bartlomieju): hangs on windows, cause can't do `Deno.kill`
|
2020-03-19 05:58:12 -04:00
|
|
|
ignore: true,
|
2020-03-18 19:25:55 -04:00
|
|
|
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(),
|
|
|
|
"--allow-net",
|
|
|
|
"--allow-read",
|
2020-03-28 13:03:49 -04:00
|
|
|
"http/testdata/simple_https_server.ts",
|
2020-03-18 19:25:55 -04:00
|
|
|
],
|
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 {
|
|
|
|
const r = new TextProtoReader(new BufReader(p.stdout!));
|
|
|
|
const s = await r.readLine();
|
|
|
|
assert(
|
2020-04-28 12:40:43 -04:00
|
|
|
s !== null && s.includes("server listening"),
|
2020-03-18 19:25:55 -04:00
|
|
|
"server must be started"
|
|
|
|
);
|
|
|
|
// 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-03-28 13:03:49 -04:00
|
|
|
certFile: "http/testdata/tls/RootCA.pem",
|
2020-03-18 19:25:55 -04:00
|
|
|
});
|
|
|
|
await Deno.writeAll(
|
|
|
|
conn,
|
|
|
|
new TextEncoder().encode("GET / HTTP/1.0\r\n\r\n")
|
|
|
|
);
|
|
|
|
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;
|
|
|
|
p.stdout!.close();
|
|
|
|
p.close();
|
|
|
|
}
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-11-04 13:45:29 -05:00
|
|
|
});
|
|
|
|
|
2020-02-26 10:48:35 -05:00
|
|
|
test("close server while iterating", async (): Promise<void> => {
|
2020-03-24 12:24:58 -04:00
|
|
|
const server = serve(":8123");
|
2020-02-26 10:48:35 -05:00
|
|
|
const nextWhileClosing = server[Symbol.asyncIterator]().next();
|
|
|
|
server.close();
|
|
|
|
assertEquals(await nextWhileClosing, { value: undefined, done: true });
|
2019-11-09 14:40:22 -05:00
|
|
|
|
2020-02-26 10:48:35 -05:00
|
|
|
const nextAfterClosing = server[Symbol.asyncIterator]().next();
|
|
|
|
assertEquals(await nextAfterClosing, { value: undefined, done: true });
|
2019-11-09 14:40:22 -05:00
|
|
|
});
|
|
|
|
|
2020-03-19 11:04:26 -04:00
|
|
|
test({
|
|
|
|
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,
|
|
|
|
new TextEncoder().encode("GET /hello HTTP/1.1\r\n\r\n")
|
|
|
|
);
|
|
|
|
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));
|
|
|
|
assertStrContains(resStr, "/hello");
|
|
|
|
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-03-15 08:03:25 -04:00
|
|
|
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
|
|
|
// @ts-ignore
|
|
|
|
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,
|
|
|
|
new TextEncoder().encode("GET / HTTP/1.1\r\n\r\n")
|
|
|
|
);
|
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
|
|
|
|
|
|
|
test({
|
|
|
|
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,
|
|
|
|
encode("GET / HTTP/1.1\r\nmalformedHeader\r\n\r\n\r\n\r\n")
|
|
|
|
);
|
|
|
|
const responseString = decode(await Deno.readAll(conn));
|
|
|
|
assertMatch(
|
|
|
|
responseString,
|
|
|
|
/^HTTP\/1\.1 400 Bad Request\r\ncontent-length: \d+\r\n\r\n.*\r\n\r\n$/ms
|
|
|
|
);
|
|
|
|
conn.close();
|
|
|
|
server.close();
|
|
|
|
assert((await entry).done);
|
|
|
|
},
|
|
|
|
});
|