From c21fadf9bcc939dcf7d949cda86c974481b5f609 Mon Sep 17 00:00:00 2001 From: dave caruso Date: Sun, 2 Jul 2023 23:24:15 -0400 Subject: set content-length 0 in some cases (#3503) --- test/js/web/fetch/fetch.test.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'test/js/web/fetch/fetch.test.ts') diff --git a/test/js/web/fetch/fetch.test.ts b/test/js/web/fetch/fetch.test.ts index 4d529b231..6b87cc613 100644 --- a/test/js/web/fetch/fetch.test.ts +++ b/test/js/web/fetch/fetch.test.ts @@ -387,6 +387,25 @@ describe("fetch", () => { }).toThrow("fetch() request with GET/HEAD/OPTIONS method cannot have body."); }), ); + + it("content length is inferred", async () => { + startServer({ + fetch(req) { + return new Response(req.headers.get("content-length")); + }, + hostname: "localhost", + }); + + // POST with body + const url = `http://${server.hostname}:${server.port}`; + const response = await fetch(url, { method: "POST", body: "buntastic" }); + expect(response.status).toBe(200); + expect(await response.text()).toBe("9"); + + const response2 = await fetch(url, { method: "POST", body: "" }); + expect(response2.status).toBe(200); + expect(await response2.text()).toBe("0"); + }); }); it("simultaneous HTTPS fetch", async () => { -- cgit v1.2.3 From 95ddfcc4377350b1d604c39c36562bde45fad2a9 Mon Sep 17 00:00:00 2001 From: Ciro Spaciari Date: Thu, 6 Jul 2023 16:01:16 -0300 Subject: fix query without slash (#3547) --- src/url.zig | 20 ++++++++++---------- test/js/web/fetch/fetch.test.ts | 4 ++++ 2 files changed, 14 insertions(+), 10 deletions(-) (limited to 'test/js/web/fetch/fetch.test.ts') diff --git a/src/url.zig b/src/url.zig index 66c4ff015..af13b139c 100644 --- a/src/url.zig +++ b/src/url.zig @@ -348,8 +348,8 @@ pub const URL = struct { url.username = str[0..i]; return i + 1; }, - // if we reach a slash, there's no username - '/' => { + // if we reach a slash or "?", there's no username + '?', '/' => { return null; }, else => {}, @@ -374,8 +374,8 @@ pub const URL = struct { if (Environment.allow_assert) std.debug.assert(str[i..].len < 2 or std.mem.readIntNative(u16, str[i..][0..2]) != std.mem.readIntNative(u16, "//")); return i + 1; }, - // if we reach a slash, there's no password - '/' => { + // if we reach a slash or "?", there's no password + '?', '/' => { return null; }, else => {}, @@ -402,8 +402,8 @@ pub const URL = struct { ipv6_i = if (ipv6_i == null and str[i] == ']') i else ipv6_i; colon_i = if (ipv6_i != null and colon_i == null and str[i] == ':') i else colon_i; switch (str[i]) { - // alright, we found the slash - '/' => { + // alright, we found the slash or "?" + '?', '/' => { break; }, else => {}, @@ -421,8 +421,8 @@ pub const URL = struct { } } else { - // look for the first "/" - // if we have a slash, anything before that is the host + // look for the first "/" or "?" + // if we have a slash or "?", anything before that is the host // anything before the colon is the hostname // anything after the colon but before the slash is the port // the origin is the scheme before the slash @@ -432,8 +432,8 @@ pub const URL = struct { colon_i = if (colon_i == null and str[i] == ':') i else colon_i; switch (str[i]) { - // alright, we found the slash - '/' => { + // alright, we found the slash or "?" + '?', '/' => { break; }, else => {}, diff --git a/test/js/web/fetch/fetch.test.ts b/test/js/web/fetch/fetch.test.ts index 6b87cc613..768818420 100644 --- a/test/js/web/fetch/fetch.test.ts +++ b/test/js/web/fetch/fetch.test.ts @@ -1175,6 +1175,10 @@ it("#2794", () => { expect(typeof Bun.fetch.bind).toBe("function"); }); +it("#3545", () => { + expect(() => fetch("http://example.com?a=b")).not.toThrow(); +}); + it("invalid header doesnt crash", () => { expect(() => fetch("http://example.com", { -- cgit v1.2.3