mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 12:19:12 +00:00
feat(std/http): make req.body a Reader (#3575)
This commit is contained in:
parent
328fef9cd6
commit
5cf2eb7d18
2 changed files with 161 additions and 54 deletions
|
@ -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({
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue