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-03-06 16:39:50 -05:00
|
|
|
import { test } from "../testing/mod.ts";
|
2019-03-06 19:42:24 -05:00
|
|
|
import { assertEquals } from "../testing/asserts.ts";
|
2019-02-19 12:38:19 -05:00
|
|
|
import { Response, ServerRequest } from "./server.ts";
|
|
|
|
import { BufReader, BufWriter } from "../io/bufio.ts";
|
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;
|
|
|
|
|
2019-04-13 15:23:56 -04:00
|
|
|
interface Deferred {
|
|
|
|
promise: Promise<{}>;
|
2019-04-19 11:25:44 -04:00
|
|
|
resolve: Handler;
|
|
|
|
reject: Handler;
|
2019-04-13 15:23:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function deferred(isResolved = false): Deferred {
|
2019-04-24 07:41:23 -04:00
|
|
|
let resolve: Handler = (): void => void 0;
|
|
|
|
let reject: Handler = (): void => void 0;
|
|
|
|
const promise = new Promise(
|
|
|
|
(res, rej): void => {
|
|
|
|
resolve = res;
|
|
|
|
reject = rej;
|
|
|
|
}
|
|
|
|
);
|
2019-04-13 15:23:56 -04:00
|
|
|
if (isResolved) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
promise,
|
|
|
|
resolve,
|
|
|
|
reject
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-17 11:49:10 -05:00
|
|
|
const responseTests: ResponseTest[] = [
|
|
|
|
// Default response
|
|
|
|
{
|
|
|
|
response: {},
|
|
|
|
raw: "HTTP/1.1 200 OK\r\n" + "\r\n"
|
|
|
|
},
|
|
|
|
// 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();
|
2019-04-13 15:23:56 -04:00
|
|
|
request.pipelineId = 1;
|
2019-02-19 12:38:19 -05:00
|
|
|
request.w = bufw;
|
2019-04-13 15:23:56 -04:00
|
|
|
request.conn = {
|
|
|
|
localAddr: "",
|
|
|
|
remoteAddr: "",
|
|
|
|
rid: -1,
|
2019-04-24 07:41:23 -04:00
|
|
|
closeRead: (): void => {},
|
|
|
|
closeWrite: (): void => {},
|
|
|
|
read: async (): Promise<Deno.ReadResult> => {
|
2019-04-13 15:23:56 -04:00
|
|
|
return { eof: true, nread: 0 };
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
write: async (): Promise<number> => {
|
2019-04-13 15:23:56 -04:00
|
|
|
return -1;
|
|
|
|
},
|
2019-04-24 07:41:23 -04:00
|
|
|
close: (): void => {},
|
2019-04-13 15:23:56 -04:00
|
|
|
lastPipelineId: 0,
|
|
|
|
pendingDeferredMap: new Map([[0, deferred(true)], [1, deferred()]])
|
|
|
|
};
|
2018-12-18 20:48:05 -05:00
|
|
|
|
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-02-19 12:38:19 -05:00
|
|
|
}
|
2019-02-15 11:03:57 -05:00
|
|
|
});
|
2018-12-18 20:48:05 -05:00
|
|
|
|
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);
|
|
|
|
const body = dec.decode(await 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);
|
|
|
|
const body = dec.decode(await 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);
|
|
|
|
const body = dec.decode(await 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);
|
|
|
|
const body = dec.decode(await 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
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function requestBodyStreamWithContentLength(): 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);
|
|
|
|
const it = await req.bodyStream();
|
|
|
|
let offset = 0;
|
|
|
|
for await (const chunk of it) {
|
|
|
|
const s = dec.decode(chunk);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(shortText.substr(offset, s.length), s);
|
2019-02-19 12:38:19 -05:00
|
|
|
offset += s.length;
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 20:48:05 -05:00
|
|
|
|
2019-02-19 12:38:19 -05:00
|
|
|
// Larger than internal 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);
|
|
|
|
const it = await req.bodyStream();
|
|
|
|
let offset = 0;
|
|
|
|
for await (const chunk of it) {
|
|
|
|
const s = dec.decode(chunk);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(longText.substr(offset, s.length), s);
|
2019-02-19 12:38:19 -05:00
|
|
|
offset += s.length;
|
|
|
|
}
|
2018-12-18 20:48:05 -05:00
|
|
|
}
|
2019-02-19 12:38:19 -05:00
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function requestBodyStreamWithTransferEncoding(): 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);
|
|
|
|
const it = await req.bodyStream();
|
|
|
|
let offset = 0;
|
|
|
|
for await (const chunk of it) {
|
|
|
|
const s = dec.decode(chunk);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(shortText.substr(offset, s.length), s);
|
2019-02-19 12:38:19 -05:00
|
|
|
offset += s.length;
|
|
|
|
}
|
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);
|
|
|
|
const it = await req.bodyStream();
|
|
|
|
let offset = 0;
|
|
|
|
for await (const chunk of it) {
|
|
|
|
const s = dec.decode(chunk);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(longText.substr(offset, s.length), s);
|
2019-02-19 12:38:19 -05:00
|
|
|
offset += s.length;
|
|
|
|
}
|
2018-12-18 20:48:05 -05:00
|
|
|
}
|
|
|
|
});
|