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
|
|
|
|
|
2019-02-26 00:35:50 -05:00
|
|
|
const { Buffer } = Deno;
|
2019-07-28 07:35:47 -04:00
|
|
|
import { TextProtoReader } from "../textproto/mod.ts";
|
2019-05-10 12:55:18 -04:00
|
|
|
import { test, runIfMain } from "../testing/mod.ts";
|
2019-05-23 22:04:06 -04:00
|
|
|
import { assert, assertEquals, assertNotEquals } from "../testing/asserts.ts";
|
2019-05-22 19:28:03 -04:00
|
|
|
import {
|
|
|
|
Response,
|
|
|
|
ServerRequest,
|
2019-11-16 19:17:47 -05:00
|
|
|
serve,
|
2019-05-22 19:28:03 -04:00
|
|
|
writeResponse,
|
2019-05-25 12:22:30 -04:00
|
|
|
readRequest,
|
|
|
|
parseHTTPVersion
|
2019-05-22 19:28:03 -04:00
|
|
|
} from "./server.ts";
|
2019-12-11 19:46:03 -05:00
|
|
|
import { delay, deferred } from "../util/async.ts";
|
2019-05-23 22:04:06 -04:00
|
|
|
import {
|
|
|
|
BufReader,
|
|
|
|
BufWriter,
|
|
|
|
ReadLineResult,
|
|
|
|
UnexpectedEOFError
|
|
|
|
} from "../io/bufio.ts";
|
2019-05-10 12:55:18 -04:00
|
|
|
import { StringReader } from "../io/readers.ts";
|
2018-12-17 11:49:10 -05:00
|
|
|
|
2019-07-07 15:20:41 -04:00
|
|
|
function assertNotEOF<T extends {}>(val: T | Deno.EOF): T {
|
|
|
|
assertNotEquals(val, Deno.EOF);
|
2019-05-23 22:04:06 -04:00
|
|
|
return val as T;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-02-19 12:38:19 -05:00
|
|
|
const enc = new TextEncoder();
|
|
|
|
const dec = new TextDecoder();
|
|
|
|
|
2019-04-19 11:25:44 -04:00
|
|
|
type Handler = () => void;
|
|
|
|
|
2018-12-17 11:49:10 -05:00
|
|
|
const responseTests: ResponseTest[] = [
|
|
|
|
// Default response
|
|
|
|
{
|
|
|
|
response: {},
|
2019-05-22 22:33:17 -04:00
|
|
|
raw: "HTTP/1.1 200 OK\r\n" + "content-length: 0" + "\r\n\r\n"
|
|
|
|
},
|
|
|
|
// Empty body with status
|
|
|
|
{
|
|
|
|
response: {
|
|
|
|
status: 404
|
|
|
|
},
|
|
|
|
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,
|
2019-02-19 12:38:19 -05: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" +
|
|
|
|
"6\r\nabcdef\r\n0\r\n\r\n"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function responseWrite(): 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
|
|
|
|
2019-08-05 18:03:55 -04:00
|
|
|
request.conn = {
|
|
|
|
localAddr: "",
|
|
|
|
remoteAddr: "",
|
|
|
|
rid: -1,
|
|
|
|
closeRead: (): void => {},
|
|
|
|
closeWrite: (): void => {},
|
|
|
|
read: async (): Promise<number | Deno.EOF> => {
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
write: async (): Promise<number> => {
|
|
|
|
return -1;
|
|
|
|
},
|
|
|
|
close: (): void => {}
|
|
|
|
};
|
|
|
|
|
2019-02-19 12:38:19 -05:00
|
|
|
await request.respond(testCase.response);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(buf.toString(), 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-01-02 12:34:33 -05:00
|
|
|
test(async function requestContentLength(): Promise<void> {
|
|
|
|
// Has content length
|
|
|
|
{
|
|
|
|
const req = new ServerRequest();
|
|
|
|
req.headers = new Headers();
|
|
|
|
req.headers.set("content-length", "5");
|
|
|
|
const buf = new Buffer(enc.encode("Hello"));
|
|
|
|
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";
|
|
|
|
const buf = new Buffer(enc.encode(chunksData));
|
|
|
|
req.r = new BufReader(buf);
|
|
|
|
assertEquals(req.contentLength, null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function requestBodyWithContentLength(): Promise<void> {
|
2019-02-19 12:38:19 -05:00
|
|
|
{
|
|
|
|
const req = new ServerRequest();
|
|
|
|
req.headers = new Headers();
|
|
|
|
req.headers.set("content-length", "5");
|
|
|
|
const buf = new Buffer(enc.encode("Hello"));
|
|
|
|
req.r = new BufReader(buf);
|
2020-01-02 12:34:33 -05:00
|
|
|
const body = dec.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");
|
|
|
|
const buf = new Buffer(enc.encode(longText));
|
|
|
|
req.r = new BufReader(buf);
|
2020-01-02 12:34:33 -05:00
|
|
|
const body = dec.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-18 18:32:31 -05:00
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function requestBodyWithTransferEncoding(): 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";
|
|
|
|
const buf = new Buffer(enc.encode(chunksData));
|
|
|
|
req.r = new BufReader(buf);
|
2020-01-02 12:34:33 -05:00
|
|
|
const body = dec.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";
|
|
|
|
const buf = new Buffer(enc.encode(chunksData));
|
|
|
|
req.r = new BufReader(buf);
|
2020-01-02 12:34:33 -05:00
|
|
|
const body = dec.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-01-02 12:34:33 -05:00
|
|
|
test(async function requestBodyReaderWithContentLength(): 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);
|
|
|
|
const buf = new Buffer(enc.encode(shortText));
|
|
|
|
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);
|
|
|
|
assertNotEOF(nread);
|
|
|
|
const s = dec.decode(readBuf.subarray(0, nread as number));
|
|
|
|
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);
|
|
|
|
assertEquals(nread, Deno.EOF);
|
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");
|
|
|
|
const buf = new Buffer(enc.encode(longText));
|
|
|
|
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);
|
|
|
|
assertNotEOF(nread);
|
|
|
|
const s = dec.decode(readBuf.subarray(0, nread as number));
|
|
|
|
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);
|
|
|
|
assertEquals(nread, Deno.EOF);
|
2018-12-18 20:48:05 -05:00
|
|
|
}
|
2019-02-19 12:38:19 -05:00
|
|
|
});
|
|
|
|
|
2020-01-02 12:34:33 -05:00
|
|
|
test(async function requestBodyReaderWithTransferEncoding(): 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";
|
|
|
|
const buf = new Buffer(enc.encode(chunksData));
|
|
|
|
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);
|
|
|
|
assertNotEOF(nread);
|
|
|
|
const s = dec.decode(readBuf.subarray(0, nread as number));
|
|
|
|
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);
|
|
|
|
assertEquals(nread, Deno.EOF);
|
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";
|
|
|
|
const buf = new Buffer(enc.encode(chunksData));
|
|
|
|
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);
|
|
|
|
assertNotEOF(nread);
|
|
|
|
const s = dec.decode(readBuf.subarray(0, nread as number));
|
|
|
|
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);
|
|
|
|
assertEquals(nread, Deno.EOF);
|
2018-12-18 20:48:05 -05:00
|
|
|
}
|
|
|
|
});
|
2019-05-10 12:55:18 -04:00
|
|
|
|
|
|
|
test(async function writeUint8ArrayResponse(): Promise<void> {
|
|
|
|
const shortText = "Hello";
|
|
|
|
|
|
|
|
const body = new TextEncoder().encode(shortText);
|
|
|
|
const res: Response = { body };
|
|
|
|
|
|
|
|
const buf = new Deno.Buffer();
|
|
|
|
await writeResponse(buf, res);
|
|
|
|
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
const reader = new BufReader(buf);
|
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
let r: ReadLineResult;
|
|
|
|
r = assertNotEOF(await reader.readLine());
|
|
|
|
assertEquals(decoder.decode(r.line), "HTTP/1.1 200 OK");
|
|
|
|
assertEquals(r.more, false);
|
2019-05-10 12:55:18 -04:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
r = assertNotEOF(await reader.readLine());
|
|
|
|
assertEquals(decoder.decode(r.line), `content-length: ${shortText.length}`);
|
|
|
|
assertEquals(r.more, false);
|
2019-05-10 12:55:18 -04:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
r = assertNotEOF(await reader.readLine());
|
|
|
|
assertEquals(r.line.byteLength, 0);
|
|
|
|
assertEquals(r.more, false);
|
2019-05-10 12:55:18 -04:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
r = assertNotEOF(await reader.readLine());
|
|
|
|
assertEquals(decoder.decode(r.line), shortText);
|
|
|
|
assertEquals(r.more, false);
|
2019-05-10 12:55:18 -04:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
const eof = await reader.readLine();
|
2019-07-07 15:20:41 -04:00
|
|
|
assertEquals(eof, Deno.EOF);
|
2019-05-10 12:55:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test(async function writeStringReaderResponse(): Promise<void> {
|
|
|
|
const shortText = "Hello";
|
|
|
|
|
|
|
|
const body = new StringReader(shortText);
|
|
|
|
const res: Response = { body };
|
|
|
|
|
|
|
|
const buf = new Deno.Buffer();
|
|
|
|
await writeResponse(buf, res);
|
|
|
|
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
const reader = new BufReader(buf);
|
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
let r: ReadLineResult;
|
|
|
|
r = assertNotEOF(await reader.readLine());
|
|
|
|
assertEquals(decoder.decode(r.line), "HTTP/1.1 200 OK");
|
|
|
|
assertEquals(r.more, false);
|
2019-05-10 12:55:18 -04:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
r = assertNotEOF(await reader.readLine());
|
|
|
|
assertEquals(decoder.decode(r.line), "transfer-encoding: chunked");
|
|
|
|
assertEquals(r.more, false);
|
2019-05-10 12:55:18 -04:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
r = assertNotEOF(await reader.readLine());
|
|
|
|
assertEquals(r.line.byteLength, 0);
|
|
|
|
assertEquals(r.more, false);
|
2019-05-10 12:55:18 -04:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
r = assertNotEOF(await reader.readLine());
|
|
|
|
assertEquals(decoder.decode(r.line), shortText.length.toString());
|
|
|
|
assertEquals(r.more, false);
|
2019-05-10 12:55:18 -04:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
r = assertNotEOF(await reader.readLine());
|
|
|
|
assertEquals(decoder.decode(r.line), shortText);
|
|
|
|
assertEquals(r.more, false);
|
2019-05-10 12:55:18 -04:00
|
|
|
|
2019-05-23 22:04:06 -04:00
|
|
|
r = assertNotEOF(await reader.readLine());
|
|
|
|
assertEquals(decoder.decode(r.line), "0");
|
|
|
|
assertEquals(r.more, false);
|
2019-05-10 12:55:18 -04:00
|
|
|
});
|
|
|
|
|
2019-08-05 18:03:55 -04:00
|
|
|
const mockConn = {
|
|
|
|
localAddr: "",
|
|
|
|
remoteAddr: "",
|
|
|
|
rid: -1,
|
|
|
|
closeRead: (): void => {},
|
|
|
|
closeWrite: (): void => {},
|
|
|
|
read: async (): Promise<number | Deno.EOF> => {
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
write: async (): Promise<number> => {
|
|
|
|
return -1;
|
|
|
|
},
|
|
|
|
close: (): void => {}
|
|
|
|
};
|
|
|
|
|
2019-05-22 19:28:03 -04:00
|
|
|
test(async function readRequestError(): Promise<void> {
|
2019-10-05 12:02:34 -04:00
|
|
|
const input = `GET / HTTP/1.1
|
2019-05-22 19:28:03 -04:00
|
|
|
malformedHeader
|
|
|
|
`;
|
|
|
|
const reader = new BufReader(new StringReader(input));
|
|
|
|
let err;
|
|
|
|
try {
|
2019-08-05 18:03:55 -04:00
|
|
|
await readRequest(mockConn, reader);
|
2019-05-22 19:28:03 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(err instanceof Error);
|
|
|
|
assertEquals(err.message, "malformed MIME header line: malformedHeader");
|
|
|
|
});
|
|
|
|
|
|
|
|
// Ported from Go
|
|
|
|
// https://github.com/golang/go/blob/go1.12.5/src/net/http/request_test.go#L377-L443
|
|
|
|
// TODO(zekth) fix tests
|
|
|
|
test(async function testReadRequestError(): Promise<void> {
|
2019-05-30 08:59:30 -04:00
|
|
|
const testCases = [
|
|
|
|
{
|
2019-05-22 19:28:03 -04:00
|
|
|
in: "GET / HTTP/1.1\r\nheader: foo\r\n\r\n",
|
2019-05-23 22:04:06 -04:00
|
|
|
headers: [{ key: "header", value: "foo" }]
|
2019-05-22 19:28:03 -04:00
|
|
|
},
|
2019-05-30 08:59:30 -04:00
|
|
|
{
|
2019-05-23 22:04:06 -04:00
|
|
|
in: "GET / HTTP/1.1\r\nheader:foo\r\n",
|
|
|
|
err: UnexpectedEOFError
|
|
|
|
},
|
2019-07-07 15:20:41 -04:00
|
|
|
{ in: "", err: Deno.EOF },
|
2019-05-30 08:59:30 -04:00
|
|
|
{
|
2019-05-23 11:59:34 -04:00
|
|
|
in: "HEAD / HTTP/1.1\r\nContent-Length:4\r\n\r\n",
|
|
|
|
err: "http: method cannot contain a Content-Length"
|
|
|
|
},
|
2019-05-30 08:59:30 -04:00
|
|
|
{
|
2019-05-22 19:28:03 -04:00
|
|
|
in: "HEAD / HTTP/1.1\r\n\r\n",
|
2019-05-23 22:04:06 -04:00
|
|
|
headers: []
|
2019-05-23 11:59:34 -04:00
|
|
|
},
|
2019-05-22 19:28:03 -04:00
|
|
|
// Multiple Content-Length values should either be
|
|
|
|
// deduplicated if same or reject otherwise
|
|
|
|
// See Issue 16490.
|
2019-05-30 08:59:30 -04:00
|
|
|
{
|
2019-05-23 11:59:34 -04:00
|
|
|
in:
|
2019-06-19 00:22:01 -04:00
|
|
|
"POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 0\r\n\r\n" +
|
|
|
|
"Gopher hey\r\n",
|
2019-05-23 11:59:34 -04:00
|
|
|
err: "cannot contain multiple Content-Length headers"
|
|
|
|
},
|
2019-05-30 08:59:30 -04:00
|
|
|
{
|
2019-05-23 11:59:34 -04:00
|
|
|
in:
|
2019-06-19 00:22:01 -04:00
|
|
|
"POST / HTTP/1.1\r\nContent-Length: 10\r\nContent-Length: 6\r\n\r\n" +
|
|
|
|
"Gopher\r\n",
|
2019-05-23 11:59:34 -04:00
|
|
|
err: "cannot contain multiple Content-Length headers"
|
|
|
|
},
|
2019-05-30 08:59:30 -04:00
|
|
|
{
|
2019-05-23 11:59:34 -04:00
|
|
|
in:
|
2019-06-19 00:22:01 -04:00
|
|
|
"PUT / HTTP/1.1\r\nContent-Length: 6 \r\nContent-Length: 6\r\n" +
|
|
|
|
"Content-Length:6\r\n\r\nGopher\r\n",
|
2019-05-23 11:59:34 -04:00
|
|
|
headers: [{ key: "Content-Length", value: "6" }]
|
|
|
|
},
|
2019-05-30 08:59:30 -04:00
|
|
|
{
|
2019-05-23 11:59:34 -04:00
|
|
|
in: "PUT / HTTP/1.1\r\nContent-Length: 1\r\nContent-Length: 6 \r\n\r\n",
|
|
|
|
err: "cannot contain multiple Content-Length headers"
|
|
|
|
},
|
|
|
|
// Setting an empty header is swallowed by textproto
|
|
|
|
// see: readMIMEHeader()
|
2019-05-30 08:59:30 -04:00
|
|
|
// {
|
2019-05-22 19:28:03 -04:00
|
|
|
// in: "POST / HTTP/1.1\r\nContent-Length:\r\nContent-Length: 3\r\n\r\n",
|
|
|
|
// err: "cannot contain multiple Content-Length headers"
|
|
|
|
// },
|
2019-05-30 08:59:30 -04:00
|
|
|
{
|
2019-05-23 11:59:34 -04:00
|
|
|
in: "HEAD / HTTP/1.1\r\nContent-Length:0\r\nContent-Length: 0\r\n\r\n",
|
2019-05-23 22:04:06 -04:00
|
|
|
headers: [{ key: "Content-Length", value: "0" }]
|
2019-05-29 13:44:14 -04:00
|
|
|
},
|
2019-05-30 08:59:30 -04:00
|
|
|
{
|
2019-05-29 13:44:14 -04:00
|
|
|
in:
|
2019-06-19 00:22:01 -04:00
|
|
|
"POST / HTTP/1.1\r\nContent-Length:0\r\ntransfer-encoding: " +
|
|
|
|
"chunked\r\n\r\n",
|
2019-05-29 13:44:14 -04:00
|
|
|
headers: [],
|
|
|
|
err: "http: Transfer-Encoding and Content-Length cannot be send together"
|
2019-05-23 11:59:34 -04:00
|
|
|
}
|
2019-05-30 08:59:30 -04:00
|
|
|
];
|
|
|
|
for (const test of testCases) {
|
2019-05-22 19:28:03 -04:00
|
|
|
const reader = new BufReader(new StringReader(test.in));
|
2019-05-23 22:04:06 -04:00
|
|
|
let err;
|
2019-06-09 21:42:06 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
let req: any;
|
2019-05-23 22:04:06 -04:00
|
|
|
try {
|
2019-08-05 18:03:55 -04:00
|
|
|
req = await readRequest(mockConn, reader);
|
2019-05-23 22:04:06 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
2019-07-07 15:20:41 -04:00
|
|
|
if (test.err === Deno.EOF) {
|
|
|
|
assertEquals(req, Deno.EOF);
|
2019-05-23 22:04:06 -04:00
|
|
|
} else if (typeof test.err === "string") {
|
|
|
|
assertEquals(err.message, test.err);
|
|
|
|
} else if (test.err) {
|
2019-05-30 08:59:30 -04:00
|
|
|
assert(err instanceof (test.err as typeof UnexpectedEOFError));
|
2019-05-23 11:59:34 -04:00
|
|
|
} else {
|
2019-05-23 22:04:06 -04:00
|
|
|
assertEquals(err, undefined);
|
2019-07-07 15:20:41 -04:00
|
|
|
assertNotEquals(req, Deno.EOF);
|
2019-05-30 08:59:30 -04:00
|
|
|
for (const h of test.headers!) {
|
|
|
|
assertEquals((req! as ServerRequest).headers.get(h.key), h.value);
|
2019-05-23 11:59:34 -04:00
|
|
|
}
|
2019-05-22 19:28:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-05-25 12:22:30 -04:00
|
|
|
|
|
|
|
// Ported from https://github.com/golang/go/blob/f5c43b9/src/net/http/request_test.go#L535-L565
|
|
|
|
test({
|
|
|
|
name: "[http] parseHttpVersion",
|
|
|
|
fn(): void {
|
|
|
|
const testCases = [
|
2019-05-23 22:04:06 -04:00
|
|
|
{ in: "HTTP/0.9", want: [0, 9] },
|
|
|
|
{ in: "HTTP/1.0", want: [1, 0] },
|
|
|
|
{ in: "HTTP/1.1", want: [1, 1] },
|
|
|
|
{ in: "HTTP/3.14", want: [3, 14] },
|
|
|
|
{ in: "HTTP", err: true },
|
|
|
|
{ in: "HTTP/one.one", err: true },
|
|
|
|
{ in: "HTTP/1.1/", err: true },
|
|
|
|
{ in: "HTTP/-1.0", err: true },
|
|
|
|
{ in: "HTTP/0.-1", err: true },
|
|
|
|
{ in: "HTTP/", err: true },
|
|
|
|
{ in: "HTTP/1,0", err: true }
|
2019-05-25 12:22:30 -04:00
|
|
|
];
|
|
|
|
for (const t of testCases) {
|
2019-05-23 22:04:06 -04:00
|
|
|
let r, err;
|
|
|
|
try {
|
|
|
|
r = parseHTTPVersion(t.in);
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
if (t.err) {
|
|
|
|
assert(err instanceof Error, t.in);
|
|
|
|
} else {
|
|
|
|
assertEquals(err, undefined);
|
|
|
|
assertEquals(r, t.want, t.in);
|
|
|
|
}
|
2019-05-25 12:22:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-07-28 07:35:47 -04:00
|
|
|
test({
|
|
|
|
name: "[http] destroyed connection",
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
// Runs a simple server as another process
|
|
|
|
const p = Deno.run({
|
2019-11-26 11:06:32 -05:00
|
|
|
args: [Deno.execPath(), "--allow-net", "http/testdata/simple_server.ts"],
|
2019-07-28 07:35:47 -04:00
|
|
|
stdout: "piped"
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const r = new TextProtoReader(new BufReader(p.stdout!));
|
|
|
|
const s = await r.readLine();
|
|
|
|
assert(s !== Deno.EOF && s.includes("server listening"));
|
|
|
|
|
|
|
|
let serverIsRunning = true;
|
2019-07-30 03:39:32 -04:00
|
|
|
p.status()
|
2019-11-13 13:42:34 -05:00
|
|
|
.then((): void => {
|
|
|
|
serverIsRunning = false;
|
|
|
|
})
|
2019-07-30 03:39:32 -04:00
|
|
|
.catch((_): void => {}); // Ignores the error when closing the process.
|
2019-07-28 07:35:47 -04:00
|
|
|
|
|
|
|
await delay(100);
|
|
|
|
|
|
|
|
// Reqeusts to the server and immediately closes the connection
|
2019-09-26 21:06:59 -04:00
|
|
|
const conn = await Deno.dial({ port: 4502 });
|
2019-07-28 07:35:47 -04:00
|
|
|
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.
|
2019-07-30 03:39:32 -04:00
|
|
|
p.close();
|
2019-07-28 07:35:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-04 13:45:29 -05:00
|
|
|
test({
|
|
|
|
name: "[http] serveTLS",
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
// Runs a simple server as another process
|
|
|
|
const p = Deno.run({
|
|
|
|
args: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"--allow-net",
|
2019-11-26 11:06:32 -05:00
|
|
|
"--allow-read",
|
|
|
|
"http/testdata/simple_https_server.ts"
|
2019-11-04 13:45:29 -05:00
|
|
|
],
|
|
|
|
stdout: "piped"
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const r = new TextProtoReader(new BufReader(p.stdout!));
|
|
|
|
const s = await r.readLine();
|
|
|
|
assert(s !== Deno.EOF && s.includes("server listening"));
|
|
|
|
|
|
|
|
let serverIsRunning = true;
|
|
|
|
p.status()
|
2019-11-13 13:42:34 -05:00
|
|
|
.then((): void => {
|
|
|
|
serverIsRunning = false;
|
|
|
|
})
|
2019-11-04 13:45:29 -05:00
|
|
|
.catch((_): void => {}); // Ignores the error when closing the process.
|
|
|
|
|
|
|
|
// Requests to the server and immediately closes the connection
|
|
|
|
const conn = await Deno.dialTLS({
|
|
|
|
hostname: "localhost",
|
|
|
|
port: 4503,
|
|
|
|
certFile: "http/testdata/tls/RootCA.pem"
|
|
|
|
});
|
|
|
|
await Deno.writeAll(
|
|
|
|
conn,
|
|
|
|
new TextEncoder().encode("GET / HTTP/1.0\r\n\r\n")
|
|
|
|
);
|
|
|
|
const res = new Uint8Array(100);
|
|
|
|
const nread = assertNotEOF(await conn.read(res));
|
|
|
|
conn.close();
|
|
|
|
const resStr = new TextDecoder().decode(res.subarray(0, nread));
|
|
|
|
assert(resStr.includes("Hello HTTPS"));
|
|
|
|
assert(serverIsRunning);
|
|
|
|
} finally {
|
|
|
|
// Stops the sever.
|
|
|
|
p.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-09 14:40:22 -05:00
|
|
|
test({
|
|
|
|
name: "[http] close server while iterating",
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
const server = serve(":8123");
|
|
|
|
const nextWhileClosing = server[Symbol.asyncIterator]().next();
|
|
|
|
server.close();
|
|
|
|
assertEquals(await nextWhileClosing, { value: undefined, done: true });
|
|
|
|
|
|
|
|
const nextAfterClosing = server[Symbol.asyncIterator]().next();
|
|
|
|
assertEquals(await nextAfterClosing, { value: undefined, done: true });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-12-11 19:46:03 -05:00
|
|
|
// TODO(kevinkassimo): create a test that works on Windows.
|
|
|
|
// The following test is to ensure that if an error occurs during respond
|
|
|
|
// would result in connection closed. (such that fd/resource is freed).
|
|
|
|
// On *nix, a delayed second attempt to write to a CLOSE_WAIT connection would
|
|
|
|
// receive a RST and thus trigger an error during response for us to test.
|
|
|
|
// We need to find a way to similarly trigger an error on Windows so that
|
|
|
|
// we can test if connection is closed.
|
|
|
|
if (Deno.build.os !== "win") {
|
|
|
|
test({
|
|
|
|
name: "[http] respond error handling",
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
const connClosedPromise = deferred();
|
|
|
|
const serverRoutine = async (): Promise<void> => {
|
|
|
|
let reqCount = 0;
|
|
|
|
const server = serve(":8124");
|
|
|
|
const serverRid = server.listener["rid"];
|
|
|
|
let connRid = -1;
|
|
|
|
for await (const req of server) {
|
|
|
|
connRid = req.conn.rid;
|
|
|
|
reqCount++;
|
2020-01-02 12:34:33 -05:00
|
|
|
await Deno.readAll(req.body);
|
2019-12-11 19:46:03 -05:00
|
|
|
await connClosedPromise;
|
|
|
|
try {
|
|
|
|
await req.respond({
|
|
|
|
body: new TextEncoder().encode("Hello World")
|
|
|
|
});
|
|
|
|
await delay(100);
|
|
|
|
req.done = deferred();
|
|
|
|
// This duplicate respond is to ensure we get a write failure from the
|
|
|
|
// other side. Our client would enter CLOSE_WAIT stage after close(),
|
|
|
|
// meaning first server .send (.respond) after close would still work.
|
|
|
|
// However, a second send would fail under RST, which is similar
|
|
|
|
// to the scenario where a failure happens during .respond
|
|
|
|
await req.respond({
|
|
|
|
body: new TextEncoder().encode("Hello World")
|
|
|
|
});
|
|
|
|
} catch {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
server.close();
|
|
|
|
const resources = Deno.resources();
|
|
|
|
assert(reqCount === 1);
|
|
|
|
// Server should be gone
|
|
|
|
assert(!(serverRid in resources));
|
|
|
|
// The connection should be destroyed
|
|
|
|
assert(!(connRid in resources));
|
|
|
|
};
|
|
|
|
const p = serverRoutine();
|
|
|
|
const conn = await Deno.dial({
|
|
|
|
hostname: "127.0.0.1",
|
|
|
|
port: 8124
|
|
|
|
});
|
|
|
|
await Deno.writeAll(
|
|
|
|
conn,
|
|
|
|
new TextEncoder().encode("GET / HTTP/1.1\r\n\r\n")
|
|
|
|
);
|
|
|
|
conn.close(); // abruptly closing connection before response.
|
|
|
|
// conn on server side enters CLOSE_WAIT state.
|
|
|
|
connClosedPromise.resolve();
|
|
|
|
await p;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-10 12:55:18 -04:00
|
|
|
runIfMain(import.meta);
|