aboutsummaryrefslogtreecommitdiff
path: root/test/js/web/fetch/fetch.test.ts
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-07-30 23:51:43 -0700
committerGravatar GitHub <noreply@github.com> 2023-07-30 23:51:43 -0700
commit9ecae59bbb1a302977afa94fd879a0c6f8d6195f (patch)
tree7bfff6d9c3264b8d1fce85f63891a1f3e5833a52 /test/js/web/fetch/fetch.test.ts
parent2ea7290172da21f4a9cd58232bf9c1ea0997d6e2 (diff)
downloadbun-9ecae59bbb1a302977afa94fd879a0c6f8d6195f.tar.gz
bun-9ecae59bbb1a302977afa94fd879a0c6f8d6195f.tar.zst
bun-9ecae59bbb1a302977afa94fd879a0c6f8d6195f.zip
Fix memory leak in response.clone(), further reduce memory usage of Request & Response (#3902)
* Atomize respsone.url & response.statusText * Fix warning * Atomize Request & Response URLs when possible * Fix memory leak in response.clone() bun/bench/snippets on  jarred/atomize ❯ mem bun --smol request-response-clone.mjs cpu: Apple M1 Max runtime: bun 0.7.2 (arm64-darwin) benchmark time (avg) (min … max) p75 p99 p995 -------------------------------------------------------- ----------------------------- req.clone().url 77.3 ns/iter (40.35 ns … 222.64 ns) 91.53 ns 128.11 ns 172.78 ns resp.clone().url 162.43 ns/iter (116 ns … 337.77 ns) 177.4 ns 232.38 ns 262.65 ns Peak memory usage: 60 MB bun/bench/snippets on  jarred/atomize ❯ mem bun-0.7.1 --smol request-response-clone.mjs cpu: Apple M1 Max runtime: bun 0.7.1 (arm64-darwin) benchmark time (avg) (min … max) p75 p99 p995 -------------------------------------------------------- ----------------------------- req.clone().url 115.85 ns/iter (80.35 ns … 247.39 ns) 128.19 ns 181.93 ns 207.23 ns resp.clone().url 252.32 ns/iter (202.6 ns … 351.07 ns) 266.56 ns 325.88 ns 334.73 ns Peak memory usage: 1179 MB * Update tests * Update js_ast.zig * Update test --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'test/js/web/fetch/fetch.test.ts')
-rw-r--r--test/js/web/fetch/fetch.test.ts40
1 files changed, 37 insertions, 3 deletions
diff --git a/test/js/web/fetch/fetch.test.ts b/test/js/web/fetch/fetch.test.ts
index d7fc87ade..ec73069de 100644
--- a/test/js/web/fetch/fetch.test.ts
+++ b/test/js/web/fetch/fetch.test.ts
@@ -30,6 +30,14 @@ afterAll(() => {
const payload = new Uint8Array(1024 * 1024 * 2);
crypto.getRandomValues(payload);
+it("new Request(invalid url) throws", () => {
+ expect(() => new Request("http")).toThrow();
+ expect(() => new Request("")).toThrow();
+ expect(() => new Request("http://[::1")).toThrow();
+ expect(() => new Request("https://[::1")).toThrow();
+ expect(() => new Request("!")).toThrow();
+});
+
describe("AbortSignal", () => {
beforeEach(() => {
startServer({
@@ -1165,9 +1173,9 @@ it("should not be able to parse json from empty body", () => {
});
it("#874", () => {
- expect(new Request(new Request("https://example.com"), {}).url).toBe("https://example.com");
- expect(new Request(new Request("https://example.com")).url).toBe("https://example.com");
- expect(new Request({ url: "https://example.com" }).url).toBe("https://example.com");
+ expect(new Request(new Request("https://example.com"), {}).url).toBe("https://example.com/");
+ expect(new Request(new Request("https://example.com")).url).toBe("https://example.com/");
+ expect(new Request({ url: "https://example.com" }).url).toBe("https://example.com/");
});
it("#2794", () => {
@@ -1213,3 +1221,29 @@ it("fetch() file:// works", async () => {
await Bun.file(Bun.fileURLToPath(new URL("file with space in the name.txt", import.meta.url))).text(),
);
});
+
+it("cloned response headers are independent before accessing", () => {
+ const response = new Response("hello", {
+ headers: {
+ "content-type": "text/html; charset=utf-8",
+ },
+ });
+ const cloned = response.clone();
+ cloned.headers.set("content-type", "text/plain");
+ expect(response.headers.get("content-type")).toBe("text/html; charset=utf-8");
+});
+
+it("cloned response headers are independent after accessing", () => {
+ const response = new Response("hello", {
+ headers: {
+ "content-type": "text/html; charset=utf-8",
+ },
+ });
+
+ // create the headers
+ response.headers;
+
+ const cloned = response.clone();
+ cloned.headers.set("content-type", "text/plain");
+ expect(response.headers.get("content-type")).toBe("text/html; charset=utf-8");
+});