mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -05:00
fix: implement ReadableStream fetch body handling (#8855)
This commit is contained in:
parent
097c3379ba
commit
ddda669a02
2 changed files with 46 additions and 2 deletions
|
@ -1013,3 +1013,43 @@ MNf4EgWfK+tZMnuqfpfO9740KzfcVoMNo4QJD4yn5YxroUOO/Azi
|
||||||
client.close();
|
client.close();
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
unitTest(
|
||||||
|
{
|
||||||
|
perms: { net: true },
|
||||||
|
},
|
||||||
|
async function fetchPostBodyReadableStream(): Promise<void> {
|
||||||
|
const addr = "127.0.0.1:4502";
|
||||||
|
const buf = bufferServer(addr);
|
||||||
|
const stream = new TransformStream();
|
||||||
|
const writer = stream.writable.getWriter();
|
||||||
|
await writer.write(new TextEncoder().encode("hello "));
|
||||||
|
await writer.write(new TextEncoder().encode("world"));
|
||||||
|
await writer.close();
|
||||||
|
const response = await fetch(`http://${addr}/blah`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: [
|
||||||
|
["Hello", "World"],
|
||||||
|
["Foo", "Bar"],
|
||||||
|
],
|
||||||
|
body: stream.readable,
|
||||||
|
});
|
||||||
|
await response.arrayBuffer();
|
||||||
|
assertEquals(response.status, 404);
|
||||||
|
assertEquals(response.headers.get("Content-Length"), "2");
|
||||||
|
|
||||||
|
const actual = new TextDecoder().decode(buf.bytes());
|
||||||
|
const expected = [
|
||||||
|
"POST /blah HTTP/1.1\r\n",
|
||||||
|
"hello: World\r\n",
|
||||||
|
"foo: Bar\r\n",
|
||||||
|
"accept: */*\r\n",
|
||||||
|
`user-agent: Deno/${Deno.version.deno}\r\n`,
|
||||||
|
"accept-encoding: gzip, br\r\n",
|
||||||
|
`host: ${addr}\r\n`,
|
||||||
|
`content-length: 11\r\n\r\n`,
|
||||||
|
"hello world",
|
||||||
|
].join("");
|
||||||
|
assertEquals(actual, expected);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
|
@ -1246,8 +1246,12 @@
|
||||||
body = multipartBuilder.getBody();
|
body = multipartBuilder.getBody();
|
||||||
contentType = multipartBuilder.getContentType();
|
contentType = multipartBuilder.getContentType();
|
||||||
} else {
|
} else {
|
||||||
// TODO: ReadableStream
|
// TODO(lucacasonato): do this in a streaming fashion once we support it
|
||||||
throw new Error("Not implemented");
|
const buf = new Buffer();
|
||||||
|
for await (const chunk of init.body) {
|
||||||
|
buf.write(chunk);
|
||||||
|
}
|
||||||
|
body = buf.bytes();
|
||||||
}
|
}
|
||||||
if (contentType && !headers.has("content-type")) {
|
if (contentType && !headers.has("content-type")) {
|
||||||
headers.set("content-type", contentType);
|
headers.set("content-type", contentType);
|
||||||
|
|
Loading…
Reference in a new issue