aboutsummaryrefslogtreecommitdiff
path: root/test/bun.js/fetch_headers.test.js
diff options
context:
space:
mode:
authorGravatar Justin Whear <justin.whear@gmail.com> 2023-02-17 15:54:05 -0800
committerGravatar GitHub <noreply@github.com> 2023-02-17 15:54:05 -0800
commitfb313f210a51387fd77e0ddf4172ee3b52d0bdc9 (patch)
tree3cd3c3f53ea5be5e36f70ec77a1db75b3d171065 /test/bun.js/fetch_headers.test.js
parentc60d7db178b66a70adadb85a5054d524169397f1 (diff)
downloadbun-fb313f210a51387fd77e0ddf4172ee3b52d0bdc9.tar.gz
bun-fb313f210a51387fd77e0ddf4172ee3b52d0bdc9.tar.zst
bun-fb313f210a51387fd77e0ddf4172ee3b52d0bdc9.zip
Fix #1602 (#2066)
* initial test case * fix segfault from JSObjectMakeDeferredPromise * pass exceptions through from FetchHeader.createFromJS * not resolved, but getting close * implement review suggestions * fix exception check, tests * Change how header filtering is accomplished Previously the FetchHeaders implementation relied on converting names and values to IDLByteString to catch non-ASCII data, though not always reliably. This resulted in message-less TypeErrors when headers contained invalid characters. This commit shifts everything to IDLDOMString for the conversion and relies on the actual error checking in FetchHeaders.canWriteHeader, resulting in nicer error messages. To ensure that all headers are written as ASCII/UTF8 rather than UTF16, the copyTo bindings function checks the encoding and converts if necessary. * wrapping up FetchHeader fixes * since utf8 allocates only do so when needed * Update src/bun.js/bindings/bindings.cpp Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> * WebCore__FetchHeaders__has should return on exception path * strip out log calls from test --------- Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
Diffstat (limited to 'test/bun.js/fetch_headers.test.js')
-rw-r--r--test/bun.js/fetch_headers.test.js58
1 files changed, 58 insertions, 0 deletions
diff --git a/test/bun.js/fetch_headers.test.js b/test/bun.js/fetch_headers.test.js
new file mode 100644
index 000000000..7f8fab188
--- /dev/null
+++ b/test/bun.js/fetch_headers.test.js
@@ -0,0 +1,58 @@
+import { describe, it, expect, beforeAll, afterAll } from "bun:test";
+const port = 3009;
+const url = `http://localhost:${port}`;
+let server;
+
+describe("Headers", async () => {
+ // Start up a single server and reuse it between tests
+ beforeAll(() => {
+ server = Bun.serve({
+ fetch(req) {
+ const hdr = req.headers.get("x-test");
+ return new Response(hdr);
+ },
+ port: port,
+ });
+ });
+ afterAll(() => {
+ server.stop();
+ });
+
+ it("Headers should work", async () => {
+ expect(await fetchContent({"x-test": "header 1"})).toBe("header 1");
+ });
+
+ it("Header names must be valid", async () => {
+ expect(() => fetch(url, {headers: {"a\tb:c": "foo" }})).toThrow("Invalid header name: 'a\tb:c'");
+ expect(() => fetch(url, {headers: {"❤️": "foo" }})).toThrow("Invalid header name: '❤️'");
+ });
+
+ it("Header values must be valid", async () => {
+ expect(() => fetch(url, {headers: {"x-test": "\0" }})).toThrow("Header 'x-test' has invalid value: '\0'");
+ expect(() => fetch(url, {headers: {"x-test": "❤️" }})).toThrow("Header 'x-test' has invalid value: '❤️'");
+ });
+
+ it("repro 1602", async () => {
+ const origString = "😂1234".slice(3);
+
+ var encoder = new TextEncoder();
+ var decoder = new TextDecoder();
+ const roundTripString = decoder.decode(encoder.encode(origString));
+
+ expect(roundTripString).toBe(origString);
+
+ // This one will pass
+ expect(await fetchContent({"x-test": roundTripString})).toBe(roundTripString);
+ // This would hang
+ expect(await fetchContent({"x-test": origString})).toBe(origString);
+ });
+});
+
+async function fetchContent(headers) {
+ const res = await fetch(
+ url,
+ { headers: headers },
+ { verbose: true }
+ );
+ return await res.text();
+}