From 3a0735e16470cde796c1420f4db982af61bdab9c Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Fri, 26 May 2023 16:28:09 -0700 Subject: Pretty formatter for `Headers` & `URLSearchParams` (#3081) * Pretty formatter for `Headers` & `URLSearchParams` * cleanup * console.log on Headers, FormData, URLSearchParams will always quote the keys now --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- test/js/web/fetch/headers.test.ts | 41 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'test/js/web/fetch/headers.test.ts') diff --git a/test/js/web/fetch/headers.test.ts b/test/js/web/fetch/headers.test.ts index 48cd3ad35..8ae17c11e 100644 --- a/test/js/web/fetch/headers.test.ts +++ b/test/js/web/fetch/headers.test.ts @@ -412,8 +412,44 @@ describe("Headers", () => { ]); }); }); - describe("toJSON()", () => { + describe("Bun.inspect()", () => { const it = "toJSON" in new Headers() ? test : test.skip; + it("can convert to json when empty", () => { + const headers = new Headers(); + expect(Bun.inspect(headers)).toStrictEqual(`Headers {}`); + }); + it("can convert to json", () => { + const headers = new Headers({ + "cache-control": "public, immutable", + }); + expect(Bun.inspect(headers)).toStrictEqual( + "Headers {" + "\n " + `"cache-control": "public, immutable"` + "\n" + "}", + ); + }); + it("can convert to json normalized", () => { + const headers = new Headers({ + "user-agent": "bun", + "X-Custom-Header": "1", + "cache-control": "public, immutable", + }); + expect(Bun.inspect(headers)).toStrictEqual( + "Headers " + + JSON.stringify( + { + "user-agent": "bun", + "cache-control": "public, immutable", + "x-custom-header": "1", + }, + null, + 2, + ), + ); + }); + }); + describe("toJSON()", () => { + // @ts-ignore + const it = new Headers()?.toJSON ? test : test.skip; + it("can convert to json when empty", () => { const headers = new Headers(); expect(headers.toJSON()).toStrictEqual({}); @@ -440,7 +476,8 @@ describe("Headers", () => { }); }); describe("count", () => { - const it = "count" in new Headers() ? test : test.skip; + // @ts-ignore + const it = typeof new Headers()?.count !== "undefined" ? test : test.skip; it("can count headers when empty", () => { const headers = new Headers(); expect(headers.count).toBe(0); -- cgit v1.2.3