Skip to content

Commit 206817f

Browse files
andreubotellayouennf
authored andcommitted
[fetch] Implement the Response.json static method
https://bugs.webkit.org/show_bug.cgi?id=240375 Reviewed by Youenn Fablet. This implements the `Response.json` static method, added to the fetch spec in whatwg/fetch#1392. * LayoutTests/imported/w3c/web-platform-tests/fetch/api/response/response-static-json.any-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/fetch/api/response/response-static-json.any.serviceworker-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/fetch/api/response/response-static-json.any.sharedworker-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/fetch/api/response/response-static-json.any.worker-expected.txt: * Source/WebCore/Modules/fetch/FetchBody.h: * Source/WebCore/Modules/fetch/FetchResponse.cpp: (WebCore::FetchResponse::create): Added this method overload to implement the spec's "initialize a response" algorithm. This algortihm used to be combined with the regular Response creation algorithm which extracts the body, but Response.json cannot use that directly. (WebCore::FetchResponse::staticJson): * Source/WebCore/Modules/fetch/FetchResponse.h: * Source/WebCore/Modules/fetch/FetchResponse.idl: Canonical link: https://commits.webkit.org/261960@main
1 parent 84eb232 commit 206817f

8 files changed

+93
-92
lines changed
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11

2-
FAIL Check response returned by static json() with init undefined promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
3-
FAIL Check response returned by static json() with init {"status":400} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
4-
FAIL Check response returned by static json() with init {"statusText":"foo"} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
5-
FAIL Check response returned by static json() with init {"headers":{}} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
6-
FAIL Check response returned by static json() with init {"headers":{"content-type":"foo/bar"}} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
7-
FAIL Check response returned by static json() with init {"headers":{"x-foo":"bar"}} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
2+
PASS Check response returned by static json() with init undefined
3+
PASS Check response returned by static json() with init {"status":400}
4+
PASS Check response returned by static json() with init {"statusText":"foo"}
5+
PASS Check response returned by static json() with init {"headers":{}}
6+
PASS Check response returned by static json() with init {"headers":{"content-type":"foo/bar"}}
7+
PASS Check response returned by static json() with init {"headers":{"x-foo":"bar"}}
88
PASS Throws TypeError when calling static json() with a status of 204
99
PASS Throws TypeError when calling static json() with a status of 205
1010
PASS Throws TypeError when calling static json() with a status of 304
11-
FAIL Check static json() encodes JSON objects correctly promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json({ foo: "bar" })', 'Response.json' is undefined)"
11+
PASS Check static json() encodes JSON objects correctly
1212
PASS Check static json() throws when data is not encodable
1313
PASS Check static json() throws when data is circular
14-
FAIL Check static json() propagates JSON serializer errors assert_throws_js: function "function () {
15-
Response.json({ get foo() { throw new CustomError("bar") }});
16-
}" threw object "TypeError: Response.json is not a function. (In 'Response.json({ get foo() { throw new CustomError("bar") }})', 'Response.json' is undefined)" ("TypeError") expected instance of function "class CustomError extends Error {
17-
name = "CustomError";
18-
}" ("CustomError")
14+
PASS Check static json() propagates JSON serializer errors
1915

Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11

2-
FAIL Check response returned by static json() with init undefined promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
3-
FAIL Check response returned by static json() with init {"status":400} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
4-
FAIL Check response returned by static json() with init {"statusText":"foo"} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
5-
FAIL Check response returned by static json() with init {"headers":{}} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
6-
FAIL Check response returned by static json() with init {"headers":{"content-type":"foo/bar"}} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
7-
FAIL Check response returned by static json() with init {"headers":{"x-foo":"bar"}} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
2+
PASS Check response returned by static json() with init undefined
3+
PASS Check response returned by static json() with init {"status":400}
4+
PASS Check response returned by static json() with init {"statusText":"foo"}
5+
PASS Check response returned by static json() with init {"headers":{}}
6+
PASS Check response returned by static json() with init {"headers":{"content-type":"foo/bar"}}
7+
PASS Check response returned by static json() with init {"headers":{"x-foo":"bar"}}
88
PASS Throws TypeError when calling static json() with a status of 204
99
PASS Throws TypeError when calling static json() with a status of 205
1010
PASS Throws TypeError when calling static json() with a status of 304
11-
FAIL Check static json() encodes JSON objects correctly promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json({ foo: "bar" })', 'Response.json' is undefined)"
11+
PASS Check static json() encodes JSON objects correctly
1212
PASS Check static json() throws when data is not encodable
1313
PASS Check static json() throws when data is circular
14-
FAIL Check static json() propagates JSON serializer errors assert_throws_js: function "function () {
15-
Response.json({ get foo() { throw new CustomError("bar") }});
16-
}" threw object "TypeError: Response.json is not a function. (In 'Response.json({ get foo() { throw new CustomError("bar") }})', 'Response.json' is undefined)" ("TypeError") expected instance of function "class CustomError extends Error {
17-
name = "CustomError";
18-
}" ("CustomError")
14+
PASS Check static json() propagates JSON serializer errors
1915

Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11

2-
FAIL Check response returned by static json() with init undefined promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
3-
FAIL Check response returned by static json() with init {"status":400} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
4-
FAIL Check response returned by static json() with init {"statusText":"foo"} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
5-
FAIL Check response returned by static json() with init {"headers":{}} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
6-
FAIL Check response returned by static json() with init {"headers":{"content-type":"foo/bar"}} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
7-
FAIL Check response returned by static json() with init {"headers":{"x-foo":"bar"}} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
2+
PASS Check response returned by static json() with init undefined
3+
PASS Check response returned by static json() with init {"status":400}
4+
PASS Check response returned by static json() with init {"statusText":"foo"}
5+
PASS Check response returned by static json() with init {"headers":{}}
6+
PASS Check response returned by static json() with init {"headers":{"content-type":"foo/bar"}}
7+
PASS Check response returned by static json() with init {"headers":{"x-foo":"bar"}}
88
PASS Throws TypeError when calling static json() with a status of 204
99
PASS Throws TypeError when calling static json() with a status of 205
1010
PASS Throws TypeError when calling static json() with a status of 304
11-
FAIL Check static json() encodes JSON objects correctly promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json({ foo: "bar" })', 'Response.json' is undefined)"
11+
PASS Check static json() encodes JSON objects correctly
1212
PASS Check static json() throws when data is not encodable
1313
PASS Check static json() throws when data is circular
14-
FAIL Check static json() propagates JSON serializer errors assert_throws_js: function "function () {
15-
Response.json({ get foo() { throw new CustomError("bar") }});
16-
}" threw object "TypeError: Response.json is not a function. (In 'Response.json({ get foo() { throw new CustomError("bar") }})', 'Response.json' is undefined)" ("TypeError") expected instance of function "class CustomError extends Error {
17-
name = "CustomError";
18-
}" ("CustomError")
14+
PASS Check static json() propagates JSON serializer errors
1915

Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11

2-
FAIL Check response returned by static json() with init undefined promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
3-
FAIL Check response returned by static json() with init {"status":400} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
4-
FAIL Check response returned by static json() with init {"statusText":"foo"} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
5-
FAIL Check response returned by static json() with init {"headers":{}} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
6-
FAIL Check response returned by static json() with init {"headers":{"content-type":"foo/bar"}} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
7-
FAIL Check response returned by static json() with init {"headers":{"x-foo":"bar"}} promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json("hello world", init)', 'Response.json' is undefined)"
2+
PASS Check response returned by static json() with init undefined
3+
PASS Check response returned by static json() with init {"status":400}
4+
PASS Check response returned by static json() with init {"statusText":"foo"}
5+
PASS Check response returned by static json() with init {"headers":{}}
6+
PASS Check response returned by static json() with init {"headers":{"content-type":"foo/bar"}}
7+
PASS Check response returned by static json() with init {"headers":{"x-foo":"bar"}}
88
PASS Throws TypeError when calling static json() with a status of 204
99
PASS Throws TypeError when calling static json() with a status of 205
1010
PASS Throws TypeError when calling static json() with a status of 304
11-
FAIL Check static json() encodes JSON objects correctly promise_test: Unhandled rejection with value: object "TypeError: Response.json is not a function. (In 'Response.json({ foo: "bar" })', 'Response.json' is undefined)"
11+
PASS Check static json() encodes JSON objects correctly
1212
PASS Check static json() throws when data is not encodable
1313
PASS Check static json() throws when data is circular
14-
FAIL Check static json() propagates JSON serializer errors assert_throws_js: function "function () {
15-
Response.json({ get foo() { throw new CustomError("bar") }});
16-
}" threw object "TypeError: Response.json is not a function. (In 'Response.json({ get foo() { throw new CustomError("bar") }})', 'Response.json' is undefined)" ("TypeError") expected instance of function "class CustomError extends Error {
17-
name = "CustomError";
18-
}" ("CustomError")
14+
PASS Check static json() propagates JSON serializer errors
1915

Source/WebCore/Modules/fetch/FetchBody.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ class FetchBody {
6060
WEBCORE_EXPORT ~FetchBody();
6161
FetchBody& operator=(FetchBody&&) = default;
6262

63+
explicit FetchBody(String&& data)
64+
: m_data(WTFMove(data))
65+
{
66+
}
67+
6368
WEBCORE_EXPORT static std::optional<FetchBody> fromFormData(ScriptExecutionContext&, Ref<FormData>&&);
6469

6570
void loadingFailed(const Exception&);
@@ -96,7 +101,6 @@ class FetchBody {
96101
explicit FetchBody(Ref<const ArrayBuffer>&& data) : m_data(WTFMove(data)) { }
97102
explicit FetchBody(Ref<const ArrayBufferView>&& data) : m_data(WTFMove(data)) { }
98103
explicit FetchBody(Ref<FormData>&& data) : m_data(WTFMove(data)) { }
99-
explicit FetchBody(String&& data) : m_data(WTFMove(data)) { }
100104
explicit FetchBody(Ref<const URLSearchParams>&& data) : m_data(WTFMove(data)) { }
101105
explicit FetchBody(Ref<ReadableStream>&& stream) : m_data(stream) { m_readableStream = WTFMove(stream); }
102106
explicit FetchBody(FetchBodyConsumer&& consumer) : m_consumer(WTFMove(consumer)) { }
@@ -130,4 +134,9 @@ class FetchBody {
130134
RefPtr<ReadableStream> m_readableStream;
131135
};
132136

137+
struct FetchBodyWithType {
138+
FetchBody body;
139+
String type;
140+
};
141+
133142
} // namespace WebCore

0 commit comments

Comments
 (0)