Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/bun.js/webcore/response.zig
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ pub const Fetch = struct {

pub const fetch_error_no_args = "fetch() expects a string but received no arguments.";
pub const fetch_error_blank_url = "fetch() URL must not be a blank string.";
pub const fetch_error_unexpected_body = "fetch() request with GET/HEAD/OPTIONS method cannot have body.";
const JSTypeErrorEnum = std.enums.EnumArray(JSType, string);
pub const fetch_type_error_names: JSTypeErrorEnum = brk: {
var errors = JSTypeErrorEnum.initUndefined();
Expand Down Expand Up @@ -1102,6 +1103,11 @@ pub const Fetch = struct {

var deferred_promise = JSC.C.JSObjectMakeDeferredPromise(globalThis, null, null, null);

if (!method.hasRequestBody() and body.size() > 0) {
const fetch_error = fetch_error_unexpected_body;
return JSPromise.rejectedPromiseValue(globalThis, ZigString.init(fetch_error).toErrorInstance(globalThis)).asRef();
}

// var resolve = FetchTasklet.FetchResolver.Class.make(ctx: js.JSContextRef, ptr: *ZigType)
_ = FetchTasklet.queue(
default_allocator,
Expand Down
25 changes: 25 additions & 0 deletions test/bun.js/fetch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,31 @@ describe("fetch", () => {
expect(response.redirected).toBe(true);
server.stop();
});

it("provide body", async () => {
const server = Bun.serve({
port: 4084,
fetch(req) {
return new Response(req.body);
},
});

// 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("buntastic");

// GET cannot have body
try {
await fetch(url, { body: "buntastic" });
expect(false).toBe(true);
} catch (exception) {
expect(exception instanceof TypeError);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(exception instanceof TypeError);
expect(exception instanceof TypeError).toBe(true);

We haven't implemented expect(exception).toBeInstanceOf(TypeError) yet

}

server.stop();
});
});

it("simultaneous HTTPS fetch", async () => {
Expand Down