aboutsummaryrefslogtreecommitdiff
path: root/test/js/web/fetch/fetch.stream.test.ts
diff options
context:
space:
mode:
authorGravatar Ciro Spaciari <ciro.spaciari@gmail.com> 2023-09-05 19:22:09 -0300
committerGravatar GitHub <noreply@github.com> 2023-09-05 15:22:09 -0700
commit6e50dd210fb052a4db4867fa03fe450ce87b4179 (patch)
treee8b4c5d7db0ced46065bf08fe37602532a48ac9f /test/js/web/fetch/fetch.stream.test.ts
parentd268097ded4513abe3cff9ca0037f72e90c23a21 (diff)
downloadbun-6e50dd210fb052a4db4867fa03fe450ce87b4179.tar.gz
bun-6e50dd210fb052a4db4867fa03fe450ce87b4179.tar.zst
bun-6e50dd210fb052a4db4867fa03fe450ce87b4179.zip
fix(fetch) always use readable stream if it is available (#4503)
* always use readable stream if it is available * use bun sleep * fix tests * rm uws dep
Diffstat (limited to 'test/js/web/fetch/fetch.stream.test.ts')
-rw-r--r--test/js/web/fetch/fetch.stream.test.ts52
1 files changed, 52 insertions, 0 deletions
diff --git a/test/js/web/fetch/fetch.stream.test.ts b/test/js/web/fetch/fetch.stream.test.ts
index 49cc0dd6a..98271ee79 100644
--- a/test/js/web/fetch/fetch.stream.test.ts
+++ b/test/js/web/fetch/fetch.stream.test.ts
@@ -4,6 +4,17 @@ import { join } from "path";
import { describe, expect, it } from "bun:test";
import { gcTick } from "harness";
import zlib from "zlib";
+import http from "http";
+import { createReadStream } from "fs";
+import { pipeline } from "stream";
+import type { AddressInfo } from "net";
+
+const files = [
+ join(import.meta.dir, "fixture.html"),
+ join(import.meta.dir, "fixture.png"),
+ join(import.meta.dir, "fixture.png.gz"),
+];
+
const fixtures = {
"fixture": readFileSync(join(import.meta.dir, "fixture.html")),
"fixture.png": readFileSync(join(import.meta.dir, "fixture.png")),
@@ -51,6 +62,47 @@ describe("fetch() with streaming", () => {
}
});
+ for (let file of files) {
+ it("stream can handle response.body + await response.something() #4500", async () => {
+ let server: ReturnType<typeof http.createServer> | null = null;
+ try {
+ const errorHandler = (err: any) => expect(err).toBeUndefined();
+
+ server = http
+ .createServer(function (req, res) {
+ res.writeHead(200, { "Content-Type": "text/plain" });
+
+ pipeline(createReadStream(file), res, errorHandler);
+ })
+ .listen(0);
+
+ const address = server.address() as AddressInfo;
+ const url = `http://${address.address}:${address.port}`;
+ async function getRequestLen(url: string) {
+ const response = await fetch(url);
+ const hasBody = response.body;
+ if (hasBody) {
+ const res = await response.blob();
+ return res.size;
+ }
+ return 0;
+ }
+
+ for (let i = 0; i < 10; i++) {
+ let len = await getRequestLen(url);
+ if (len <= 0) {
+ throw new Error("Request length is 0");
+ }
+ await Bun.sleep(50);
+ }
+
+ expect(true).toBe(true);
+ } finally {
+ server?.close();
+ }
+ });
+ }
+
it("stream still works after response get out of scope", async () => {
let server: Server | null = null;
try {