summaryrefslogtreecommitdiff
path: root/std/http/server_test.ts
diff options
context:
space:
mode:
authorKevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com>2020-01-02 09:34:33 -0800
committerRy Dahl <ry@tinyclouds.org>2020-01-02 12:34:33 -0500
commit5cf2eb7d184b9031d46f61a5031ac13a41f4560c (patch)
tree1f6380946c19ee8d19829c93c515c9b029bfdd3e /std/http/server_test.ts
parent328fef9cd6aa0ab3033380cabdf91b9779db5054 (diff)
feat(std/http): make req.body a Reader (#3575)
Diffstat (limited to 'std/http/server_test.ts')
-rw-r--r--std/http/server_test.ts106
1 files changed, 78 insertions, 28 deletions
diff --git a/std/http/server_test.ts b/std/http/server_test.ts
index 557223164..5dbbe526c 100644
--- a/std/http/server_test.ts
+++ b/std/http/server_test.ts
@@ -96,6 +96,40 @@ test(async function responseWrite(): Promise<void> {
}
});
+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);
+ }
+});
+
test(async function requestBodyWithContentLength(): Promise<void> {
{
const req = new ServerRequest();
@@ -103,7 +137,7 @@ test(async function requestBodyWithContentLength(): Promise<void> {
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());
+ const body = dec.decode(await Deno.readAll(req.body));
assertEquals(body, "Hello");
}
@@ -115,7 +149,7 @@ test(async function requestBodyWithContentLength(): Promise<void> {
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());
+ const body = dec.decode(await Deno.readAll(req.body));
assertEquals(body, longText);
}
});
@@ -140,7 +174,7 @@ test(async function requestBodyWithTransferEncoding(): Promise<void> {
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());
+ const body = dec.decode(await Deno.readAll(req.body));
assertEquals(body, shortText);
}
@@ -164,12 +198,12 @@ test(async function requestBodyWithTransferEncoding(): Promise<void> {
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());
+ const body = dec.decode(await Deno.readAll(req.body));
assertEquals(body, longText);
}
});
-test(async function requestBodyStreamWithContentLength(): Promise<void> {
+test(async function requestBodyReaderWithContentLength(): Promise<void> {
{
const shortText = "Hello";
const req = new ServerRequest();
@@ -177,16 +211,20 @@ test(async function requestBodyStreamWithContentLength(): Promise<void> {
req.headers.set("content-length", "" + shortText.length);
const buf = new Buffer(enc.encode(shortText));
req.r = new BufReader(buf);
- const it = await req.bodyStream();
+ const readBuf = new Uint8Array(6);
let offset = 0;
- for await (const chunk of it) {
- const s = dec.decode(chunk);
- assertEquals(shortText.substr(offset, s.length), s);
- offset += s.length;
+ 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;
}
+ const nread = await req.body.read(readBuf);
+ assertEquals(nread, Deno.EOF);
}
- // Larger than internal buf
+ // Larger than given buf
{
const longText = "1234\n".repeat(1000);
const req = new ServerRequest();
@@ -194,17 +232,21 @@ test(async function requestBodyStreamWithContentLength(): Promise<void> {
req.headers.set("Content-Length", "5000");
const buf = new Buffer(enc.encode(longText));
req.r = new BufReader(buf);
- const it = await req.bodyStream();
+ const readBuf = new Uint8Array(1000);
let offset = 0;
- for await (const chunk of it) {
- const s = dec.decode(chunk);
- assertEquals(longText.substr(offset, s.length), s);
- offset += s.length;
+ 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;
}
+ const nread = await req.body.read(readBuf);
+ assertEquals(nread, Deno.EOF);
}
});
-test(async function requestBodyStreamWithTransferEncoding(): Promise<void> {
+test(async function requestBodyReaderWithTransferEncoding(): Promise<void> {
{
const shortText = "Hello";
const req = new ServerRequest();
@@ -224,13 +266,17 @@ test(async function requestBodyStreamWithTransferEncoding(): Promise<void> {
chunksData += "0\r\n\r\n";
const buf = new Buffer(enc.encode(chunksData));
req.r = new BufReader(buf);
- const it = await req.bodyStream();
+ const readBuf = new Uint8Array(6);
let offset = 0;
- for await (const chunk of it) {
- const s = dec.decode(chunk);
- assertEquals(shortText.substr(offset, s.length), s);
- offset += s.length;
+ 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;
}
+ const nread = await req.body.read(readBuf);
+ assertEquals(nread, Deno.EOF);
}
// Larger than internal buf
@@ -253,13 +299,17 @@ test(async function requestBodyStreamWithTransferEncoding(): Promise<void> {
chunksData += "0\r\n\r\n";
const buf = new Buffer(enc.encode(chunksData));
req.r = new BufReader(buf);
- const it = await req.bodyStream();
+ const readBuf = new Uint8Array(1000);
let offset = 0;
- for await (const chunk of it) {
- const s = dec.decode(chunk);
- assertEquals(longText.substr(offset, s.length), s);
- offset += s.length;
+ 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;
}
+ const nread = await req.body.read(readBuf);
+ assertEquals(nread, Deno.EOF);
}
});
@@ -610,7 +660,7 @@ if (Deno.build.os !== "win") {
for await (const req of server) {
connRid = req.conn.rid;
reqCount++;
- await req.body();
+ await Deno.readAll(req.body);
await connClosedPromise;
try {
await req.respond({