diff options
author | 2022-03-18 20:05:56 -0700 | |
---|---|---|
committer | 2022-03-18 20:05:56 -0700 | |
commit | f4eecd378230bda6b230227422ef9f4d1623ad2c (patch) | |
tree | 82f1a73eff6da6cc210f748d9119e98aa4c53bdb /integration/bunjs-only-snippets/fetch.test.js | |
parent | 2e920aaac38808865cd78e974ecc3be9fd1fd9a7 (diff) | |
download | bun-f4eecd378230bda6b230227422ef9f4d1623ad2c.tar.gz bun-f4eecd378230bda6b230227422ef9f4d1623ad2c.tar.zst bun-f4eecd378230bda6b230227422ef9f4d1623ad2c.zip |
Add tests for Response.json, Response.error, and Response.redirect
Diffstat (limited to 'integration/bunjs-only-snippets/fetch.test.js')
-rw-r--r-- | integration/bunjs-only-snippets/fetch.test.js | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/integration/bunjs-only-snippets/fetch.test.js b/integration/bunjs-only-snippets/fetch.test.js index 21e01ebad..87cd5414f 100644 --- a/integration/bunjs-only-snippets/fetch.test.js +++ b/integration/bunjs-only-snippets/fetch.test.js @@ -260,6 +260,95 @@ describe("Blob", () => { }); describe("Response", () => { + describe("Response.json", () => { + it("works", async () => { + const inputs = [ + "hellooo", + [[123], 456, 789], + { hello: "world" }, + { ok: "😉 😌 😍 🥰 😘 " }, + ]; + for (let input of inputs) { + const output = JSON.stringify(input); + expect(await Response.json(input).text()).toBe(output); + } + // JSON.stringify() returns undefined + expect(await Response.json().text()).toBe(""); + // JSON.stringify("") returns '""' + expect(await Response.json("").text()).toBe('""'); + }); + it("sets the content-type header", () => { + let response = Response.json("hello"); + expect(response.type).toBe("basic"); + expect(response.headers.get("content-type")).toBe("application/json"); + expect(response.status).toBe(200); + }); + it("supports number status code", () => { + let response = Response.json("hello", 407); + expect(response.type).toBe("basic"); + expect(response.headers.get("content-type")).toBe("application/json"); + expect(response.status).toBe(407); + }); + it("overrides the content-type header", () => { + var response = Response.json("hello", { + headers: { + "content-type": "potato", + }, + }); + expect(response.type).toBe("basic"); + expect(response.headers.get("content-type")).toBe("application/json"); + }); + it("supports headers", () => { + var response = Response.json("hello", { + headers: { + "content-type": "potato", + "x-hello": "world", + }, + statusCode: 408, + }); + expect(response.headers.get("content-type")).toBe("application/json"); + expect(response.headers.get("x-hello")).toBe("world"); + expect(response.status).toBe(408); + }); + }); + describe("Response.redirect", () => { + it("works", () => { + const inputs = [ + "http://example.com", + "http://example.com/", + "http://example.com/hello", + "http://example.com/hello/", + "http://example.com/hello/world", + "http://example.com/hello/world/", + ]; + for (let input of inputs) { + expect(Response.redirect(input).headers.get("Location")).toBe(input); + } + }); + + it("supports headers", () => { + var response = Response.redirect("https://example.com", { + headers: { + "content-type": "potato", + "x-hello": "world", + Location: "https://wrong.com", + }, + statusCode: 408, + }); + expect(response.headers.get("x-hello")).toBe("world"); + expect(response.headers.get("Location")).toBe("https://example.com"); + expect(response.status).toBe(302); + expect(response.type).toBe("basic"); + expect(response.ok).toBe(false); + }); + }); + describe("Response.error", () => { + it("works", () => { + expect(Response.error().type).toBe("error"); + expect(Response.error().ok).toBe(false); + expect(Response.error().status).toBe(0); + }); + }); it("clone", async () => { gc(); var body = new Response("<div>hello</div>", { |