diff options
Diffstat (limited to 'test/js')
-rw-r--r-- | test/js/bun/util/inspect.test.js | 5 | ||||
-rw-r--r-- | test/js/web/fetch/headers.test.ts | 41 | ||||
-rw-r--r-- | test/js/web/html/FormData.test.ts | 24 | ||||
-rw-r--r-- | test/js/web/html/URLSearchParams.test.ts | 63 | ||||
-rw-r--r-- | test/js/web/url/url.test.ts | 32 |
5 files changed, 132 insertions, 33 deletions
diff --git a/test/js/bun/util/inspect.test.js b/test/js/bun/util/inspect.test.js index 5c67999fe..e91204c69 100644 --- a/test/js/bun/util/inspect.test.js +++ b/test/js/bun/util/inspect.test.js @@ -54,6 +54,7 @@ it("Blob inspect", () => { expect(Bun.inspect(new Response(new Blob()))).toBe(`Response (0 KB) { ok: true, url: "", + headers: Headers {}, statusText: "", redirected: false, bodyUsed: false, @@ -63,6 +64,7 @@ it("Blob inspect", () => { expect(Bun.inspect(new Response("Hello"))).toBe(`Response (5 bytes) { ok: true, url: "", + headers: Headers {}, statusText: "", redirected: false, bodyUsed: false, @@ -105,7 +107,8 @@ it("Request object", () => { ` Request (0 KB) { method: "GET", - url: "https://example.com" + url: "https://example.com", + headers: Headers {} }`.trim(), ); }); 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,10 +412,46 @@ 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({}); }); it("can convert to json", () => { @@ -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); diff --git a/test/js/web/html/FormData.test.ts b/test/js/web/html/FormData.test.ts index af2871b10..abb298c1a 100644 --- a/test/js/web/html/FormData.test.ts +++ b/test/js/web/html/FormData.test.ts @@ -338,6 +338,30 @@ describe("FormData", () => { expect(Bun.inspect(formData).length > 0).toBe(true); }); + describe("non-standard extensions", () => { + it("should support .length", () => { + const formData = new FormData(); + formData.append("foo", "bar"); + formData.append("foo", new Blob(["bar"])); + formData.append("bar", "baz"); + // @ts-ignore + expect(formData.length).toBe(3); + formData.delete("foo"); + // @ts-ignore + expect(formData.length).toBe(1); + formData.append("foo", "bar"); + // @ts-ignore + expect(formData.length).toBe(2); + formData.delete("foo"); + formData.delete("foo"); + // @ts-ignore + expect(formData.length).toBe(1); + formData.delete("bar"); + // @ts-ignore + expect(formData.length).toBe(0); + }); + }); + describe("URLEncoded", () => { test("should parse URL encoded", async () => { const response = new Response("foo=bar&baz=qux", { diff --git a/test/js/web/html/URLSearchParams.test.ts b/test/js/web/html/URLSearchParams.test.ts new file mode 100644 index 000000000..028590971 --- /dev/null +++ b/test/js/web/html/URLSearchParams.test.ts @@ -0,0 +1,63 @@ +import { describe, it, expect } from "bun:test"; + +describe("URLSearchParams", () => { + describe("non-standard extensions", () => { + it("should support .length", () => { + const params = new URLSearchParams(); + params.append("foo", "bar"); + params.append("foo", "boop"); + params.append("bar", "baz"); + expect(params.length).toBe(3); + params.delete("foo"); + expect(params.length).toBe(1); + params.append("foo", "bar"); + expect(params.length).toBe(2); + params.delete("foo"); + params.delete("foo"); + expect(params.length).toBe(1); + params.delete("bar"); + expect(params.length).toBe(0); + }); + + it("should support .toJSON", () => { + const params = new URLSearchParams(); + params.append("foo", "bar"); + params.append("foo", "boop"); + params.append("bar", "baz"); + // @ts-ignore + expect(params.toJSON()).toEqual({ + foo: ["bar", "boop"], + bar: "baz", + }); + expect(JSON.parse(JSON.stringify(params))).toEqual({ + foo: ["bar", "boop"], + bar: "baz", + }); + expect(Bun.inspect(params)).toBe( + "URLSearchParams {" + "\n" + ' foo: [ "bar", "boop" ],' + "\n" + ' bar: "baz"' + "\n" + "}", + ); + params.delete("foo"); + // @ts-ignore + expect(params.toJSON()).toEqual({ + bar: "baz", + }); + params.append("foo", "bar"); + // @ts-ignore + expect(params.toJSON()).toEqual({ + foo: "bar", + bar: "baz", + }); + params.delete("foo"); + params.delete("foo"); + // @ts-ignore + expect(params.toJSON()).toEqual({ + bar: "baz", + }); + params.delete("bar"); + // @ts-ignore + expect(params.toJSON()).toEqual({}); + + expect(JSON.stringify(params)).toBe("{}"); + }); + }); +}); diff --git a/test/js/web/url/url.test.ts b/test/js/web/url/url.test.ts index 4e71c44d7..6ad691c1b 100644 --- a/test/js/web/url/url.test.ts +++ b/test/js/web/url/url.test.ts @@ -73,21 +73,7 @@ describe("url", () => { pathname: "/", hash: "", search: "", - searchParams: URLSearchParams { - append: [Function: append], - delete: [Function: delete], - get: [Function: get], - getAll: [Function: getAll], - has: [Function: has], - set: [Function: set], - sort: [Function: sort], - entries: [Function: entries], - keys: [Function: keys], - values: [Function: values], - forEach: [Function: forEach], - toString: [Function: toString], - [Symbol(Symbol.iterator)]: [Function: entries] - }, + searchParams: ${Bun.inspect(new URLSearchParams())}, toJSON: [Function: toJSON], toString: [Function: toString] }`); @@ -108,21 +94,7 @@ describe("url", () => { pathname: "/oven-sh/bun/issues/135", hash: "", search: "?hello%20i%20have%20spaces%20thank%20you%20good%20night", - searchParams: URLSearchParams { - append: [Function: append], - delete: [Function: delete], - get: [Function: get], - getAll: [Function: getAll], - has: [Function: has], - set: [Function: set], - sort: [Function: sort], - entries: [Function: entries], - keys: [Function: keys], - values: [Function: values], - forEach: [Function: forEach], - toString: [Function: toString], - [Symbol(Symbol.iterator)]: [Function: entries] - }, + searchParams: URLSearchParams {\n \"hello i have spaces thank you good night\": \"\"\n }, toJSON: [Function: toJSON], toString: [Function: toString] }`); |