|
| 1 | +// META: global=window,worker |
| 2 | +// META: title=Response: json static method |
| 3 | + |
| 4 | +const APPLICATION_JSON = "application/json"; |
| 5 | +const FOO_BAR = "foo/bar"; |
| 6 | + |
| 7 | +const INIT_TESTS = [ |
| 8 | + [undefined, 200, "", APPLICATION_JSON, {}], |
| 9 | + [{ status: 400 }, 400, "", APPLICATION_JSON, {}], |
| 10 | + [{ statusText: "foo" }, 200, "foo", APPLICATION_JSON, {}], |
| 11 | + [{ headers: {} }, 200, "", APPLICATION_JSON, {}], |
| 12 | + [{ headers: { "content-type": FOO_BAR } }, 200, "", FOO_BAR, {}], |
| 13 | + [{ headers: { "x-foo": "bar" } }, 200, "", APPLICATION_JSON, { "x-foo": "bar" }], |
| 14 | +]; |
| 15 | + |
| 16 | +for (const [init, expectedStatus, expectedStatusText, expectedContentType, expectedHeaders] of INIT_TESTS) { |
| 17 | + promise_test(async function () { |
| 18 | + const response = Response.json("hello world", init); |
| 19 | + assert_equals(response.type, "default", "Response's type is default"); |
| 20 | + assert_equals(response.status, expectedStatus, "Response's status is " + expectedStatus); |
| 21 | + assert_equals(response.statusText, expectedStatusText, "Response's statusText is " + JSON.stringify(expectedStatusText)); |
| 22 | + assert_equals(response.headers.get("content-type"), expectedContentType, "Response's content-type is " + expectedContentType); |
| 23 | + for (const key in expectedHeaders) { |
| 24 | + assert_equals(response.headers.get(key), expectedHeaders[key], "Response's header " + key + " is " + JSON.stringify(expectedHeaders[key])); |
| 25 | + } |
| 26 | + |
| 27 | + const data = await response.json(); |
| 28 | + assert_equals(data, "hello world", "Response's body is 'hello world'"); |
| 29 | + }, `Check response returned by static json() with init ${JSON.stringify(init)}`); |
| 30 | +} |
| 31 | + |
| 32 | +const nullBodyStatus = [204, 205, 304]; |
| 33 | +for (const status of nullBodyStatus) { |
| 34 | + test(function () { |
| 35 | + assert_throws_js( |
| 36 | + TypeError, |
| 37 | + function () { |
| 38 | + Response.json("hello world", { status: status }); |
| 39 | + }, |
| 40 | + ); |
| 41 | + }, `Throws TypeError when calling static json() with a status of ${status}`); |
| 42 | +} |
| 43 | + |
| 44 | +promise_test(async function () { |
| 45 | + const response = Response.json({ foo: "bar" }); |
| 46 | + const data = await response.json(); |
| 47 | + assert_equals(typeof data, "object", "Response's json body is an object"); |
| 48 | + assert_equals(data.foo, "bar", "Response's json body is { foo: 'bar' }"); |
| 49 | +}, "Check static json() encodes JSON objects correctly"); |
| 50 | + |
| 51 | +test(function () { |
| 52 | + assert_throws_js( |
| 53 | + TypeError, |
| 54 | + function () { |
| 55 | + Response.json(Symbol("foo")); |
| 56 | + }, |
| 57 | + ); |
| 58 | +}, "Check static json() throws when data is not encodable"); |
| 59 | + |
| 60 | +test(function () { |
| 61 | + const a = { b: 1 }; |
| 62 | + a.a = a; |
| 63 | + assert_throws_js( |
| 64 | + TypeError, |
| 65 | + function () { |
| 66 | + Response.json(a); |
| 67 | + }, |
| 68 | + ); |
| 69 | +}, "Check static json() throws when data is circular"); |
| 70 | + |
| 71 | +promise_test(async function () { |
| 72 | + class CustomError extends Error { |
| 73 | + name = "CustomError"; |
| 74 | + } |
| 75 | + assert_throws_js( |
| 76 | + CustomError, |
| 77 | + function () { |
| 78 | + Response.json({ get foo() { throw new CustomError("bar") }}); |
| 79 | + } |
| 80 | + ) |
| 81 | +}, "Check static json() propagates JSON serializer errors"); |
0 commit comments