Files
bun.sh/test/js/web/fetch/response.test.ts
Jarred Sumner 5c65c18e72 Delete incorrect assertion in ComptimeStringMap (#21504)
### What does this PR do?

Resolves
```js
Bun v1.2.13 ([64ed68c](64ed68c9e0)) on windows x86_64 [TestCommand]

panic: ComptimeStringMap.fromJS: input is not a string

[comptime_string_map.zig:268](64ed68c9e0/src/comptime_string_map.zig (L268)): getWithEql
[Response.zig:682](64ed68c9e0/src/bun.js/webcore/Response.zig (L682)): init
[Request.zig:679](64ed68c9e0/src/bun.js/webcore/Request.zig (L679)): constructInto
[ZigGeneratedClasses.cpp:37976](64ed68c9e0/C:/buildkite-agent/builds/EC2AMAZ-Q4V5GV4/bun/bun/build/release/codegen/ZigGeneratedClasses.cpp#L37976): WebCore::JSRequestConstructor::construct
2 unknown/js code
llint_entry

Features: tsconfig, Bun.stdout, dotenv, jsc
```

### How did you verify your code works?

There is a test.
2025-07-31 00:56:50 -07:00

112 lines
3.7 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { normalizeBunSnapshot } from "harness";
test("zero args returns an otherwise empty 200 response", () => {
const response = new Response();
expect(response.status).toBe(200);
expect(response.statusText).toBe("");
});
test("calling cancel() on response body doesn't throw", () => {
expect(() => new Response("").body?.cancel()).not.toThrow();
});
test("undefined args don't throw", () => {
const response = new Response("", {
status: undefined,
statusText: undefined,
headers: undefined,
});
expect(response.status).toBe(200);
expect(response.statusText).toBe("");
});
test("1-arg form returns a 200 response", () => {
const response = new Response("body text");
expect(response.status).toBe(200);
expect(response.statusText).toBe("");
});
describe("2-arg form", () => {
test("can fill in status/statusText, and it works", () => {
const response = new Response("body text", {
status: 202,
statusText: "Accepted.",
});
expect(response.status).toBe(202);
expect(response.statusText).toBe("Accepted.");
});
test('empty object continues to return 200/""', () => {
const response = new Response("body text", {});
expect(response.status).toBe(200);
expect(response.statusText).toBe("");
});
});
test("print size", () => {
expect(normalizeBunSnapshot(Bun.inspect(new Response(Bun.file(import.meta.filename)))), import.meta.dir)
.toMatchInlineSnapshot(`
"Response (3.82 KB) {
ok: true,
url: "",
status: 200,
statusText: "",
headers: Headers {
"content-type": "text/javascript;charset=utf-8",
},
redirected: false,
bodyUsed: false,
FileRef ("<cwd>/test/js/web/fetch/response.test.ts") {
type: "text/javascript;charset=utf-8"
}
}"
`);
});
test("Response.redirect with invalid arguments should not crash", () => {
// This should not crash - issue #18414
// Passing a number as URL and string as init should handle gracefully
expect(() => Response.redirect(400, "a")).not.toThrow();
// Test various invalid argument combinations - should not crash
expect(() => Response.redirect(42, "test")).not.toThrow();
expect(() => Response.redirect(true, "string")).not.toThrow();
expect(() => Response.redirect(null, "init")).not.toThrow();
expect(() => Response.redirect(undefined, "value")).not.toThrow();
});
test("Response.redirect status code validation", () => {
// Valid redirect status codes should work
expect(() => Response.redirect("url", 301)).not.toThrow();
expect(() => Response.redirect("url", 302)).not.toThrow();
expect(() => Response.redirect("url", 303)).not.toThrow();
expect(() => Response.redirect("url", 307)).not.toThrow();
expect(() => Response.redirect("url", 308)).not.toThrow();
// Invalid status codes should throw RangeError
expect(() => Response.redirect("url", 200)).toThrow(RangeError);
expect(() => Response.redirect("url", 400)).toThrow(RangeError);
expect(() => Response.redirect("url", 500)).toThrow(RangeError);
// Status in object should also be validated
expect(() => Response.redirect("url", { status: 307 })).not.toThrow();
expect(() => Response.redirect("url", { status: 400 })).toThrow(RangeError);
// Check that the correct status is set
expect(Response.redirect("url", 301).status).toBe(301);
expect(Response.redirect("url", { status: 308 }).status).toBe(308);
});
test("new Response(123, { statusText: 123 }) does not throw", () => {
// @ts-expect-error
expect(new Response("123", { statusText: 123 }).statusText).toBe("123");
});
test("new Response(123, { method: 456 }) does not throw", () => {
// @ts-expect-error
expect(() => new Response("123", { method: 456 })).not.toThrow();
});