aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/bun.js/webcore/response.zig18
-rw-r--r--test/bun.js/fetch.test.js18
2 files changed, 25 insertions, 11 deletions
diff --git a/src/bun.js/webcore/response.zig b/src/bun.js/webcore/response.zig
index 32e1e3f9b..db8531253 100644
--- a/src/bun.js/webcore/response.zig
+++ b/src/bun.js/webcore/response.zig
@@ -801,9 +801,16 @@ pub const Fetch = struct {
var disable_timeout = false;
var disable_keepalive = false;
var verbose = false;
- if (first_arg.isString()) {
+ if (first_arg.as(Request)) |request| {
+ url = ZigURL.parse(getAllocator(ctx).dupe(u8, request.url) catch unreachable);
+ method = request.method;
+ if (request.headers) |head| {
+ headers = Headers.from(head, bun.default_allocator) catch unreachable;
+ }
+ body = request.body.useAsAnyBlob();
+ } else if (first_arg.toStringOrNull(globalThis)) |jsstring| {
var url_zig_str = ZigString.init("");
- JSValue.fromRef(arguments[0]).toZigString(&url_zig_str, globalThis);
+ jsstring.toZigString(globalThis, &url_zig_str);
var url_str = url_zig_str.slice();
if (url_str.len == 0) {
@@ -876,13 +883,6 @@ pub const Fetch = struct {
}
}
}
- } else if (first_arg.as(Request)) |request| {
- url = ZigURL.parse(getAllocator(ctx).dupe(u8, request.url) catch unreachable);
- method = request.method;
- if (request.headers) |head| {
- headers = Headers.from(head, bun.default_allocator) catch unreachable;
- }
- body = request.body.useAsAnyBlob();
} else {
const fetch_error = fetch_type_error_strings.get(js.JSValueGetType(ctx, arguments[0]));
exception.* = ZigString.init(fetch_error).toErrorInstance(globalThis).asObjectRef();
diff --git a/test/bun.js/fetch.test.js b/test/bun.js/fetch.test.js
index 1b9d0779e..603cdfb3c 100644
--- a/test/bun.js/fetch.test.js
+++ b/test/bun.js/fetch.test.js
@@ -9,10 +9,24 @@ const exampleFixture = fs.readFileSync(
);
describe("fetch", () => {
- const urls = ["https://example.com", "http://example.com"];
+ const urls = [
+ "https://example.com",
+ "http://example.com",
+ new URL("https://example.com"),
+ new Request({ url: "https://example.com" }),
+ { toString: () => "https://example.com" },
+ ];
for (let url of urls) {
gc();
- it(url, async () => {
+ let name = url;
+ if (name instanceof URL) {
+ name = "URL: " + name;
+ } else if (name instanceof Request) {
+ name = "Request: " + name.url;
+ } else if (name.hasOwnProperty("toString")) {
+ name = "Object: " + name.toString();
+ }
+ it(name, async () => {
gc();
const response = await fetch(url, {}, { verbose: true });
gc();