Skip to content

Commit

Permalink
[fetch] Implement the Response.json static method
Browse files Browse the repository at this point in the history
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
  • Loading branch information
andreubotella authored and youennf committed Mar 22, 2023
1 parent 84eb232 commit 206817f
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@

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)"
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)"
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)"
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)"
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)"
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)"
PASS Check response returned by static json() with init undefined
PASS Check response returned by static json() with init {"status":400}
PASS Check response returned by static json() with init {"statusText":"foo"}
PASS Check response returned by static json() with init {"headers":{}}
PASS Check response returned by static json() with init {"headers":{"content-type":"foo/bar"}}
PASS Check response returned by static json() with init {"headers":{"x-foo":"bar"}}
PASS Throws TypeError when calling static json() with a status of 204
PASS Throws TypeError when calling static json() with a status of 205
PASS Throws TypeError when calling static json() with a status of 304
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)"
PASS Check static json() encodes JSON objects correctly
PASS Check static json() throws when data is not encodable
PASS Check static json() throws when data is circular
FAIL Check static json() propagates JSON serializer errors assert_throws_js: function "function () {
Response.json({ get foo() { throw new CustomError("bar") }});
}" 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 {
name = "CustomError";
}" ("CustomError")
PASS Check static json() propagates JSON serializer errors

Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@

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)"
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)"
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)"
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)"
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)"
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)"
PASS Check response returned by static json() with init undefined
PASS Check response returned by static json() with init {"status":400}
PASS Check response returned by static json() with init {"statusText":"foo"}
PASS Check response returned by static json() with init {"headers":{}}
PASS Check response returned by static json() with init {"headers":{"content-type":"foo/bar"}}
PASS Check response returned by static json() with init {"headers":{"x-foo":"bar"}}
PASS Throws TypeError when calling static json() with a status of 204
PASS Throws TypeError when calling static json() with a status of 205
PASS Throws TypeError when calling static json() with a status of 304
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)"
PASS Check static json() encodes JSON objects correctly
PASS Check static json() throws when data is not encodable
PASS Check static json() throws when data is circular
FAIL Check static json() propagates JSON serializer errors assert_throws_js: function "function () {
Response.json({ get foo() { throw new CustomError("bar") }});
}" 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 {
name = "CustomError";
}" ("CustomError")
PASS Check static json() propagates JSON serializer errors

Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@

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)"
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)"
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)"
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)"
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)"
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)"
PASS Check response returned by static json() with init undefined
PASS Check response returned by static json() with init {"status":400}
PASS Check response returned by static json() with init {"statusText":"foo"}
PASS Check response returned by static json() with init {"headers":{}}
PASS Check response returned by static json() with init {"headers":{"content-type":"foo/bar"}}
PASS Check response returned by static json() with init {"headers":{"x-foo":"bar"}}
PASS Throws TypeError when calling static json() with a status of 204
PASS Throws TypeError when calling static json() with a status of 205
PASS Throws TypeError when calling static json() with a status of 304
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)"
PASS Check static json() encodes JSON objects correctly
PASS Check static json() throws when data is not encodable
PASS Check static json() throws when data is circular
FAIL Check static json() propagates JSON serializer errors assert_throws_js: function "function () {
Response.json({ get foo() { throw new CustomError("bar") }});
}" 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 {
name = "CustomError";
}" ("CustomError")
PASS Check static json() propagates JSON serializer errors

Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@

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)"
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)"
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)"
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)"
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)"
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)"
PASS Check response returned by static json() with init undefined
PASS Check response returned by static json() with init {"status":400}
PASS Check response returned by static json() with init {"statusText":"foo"}
PASS Check response returned by static json() with init {"headers":{}}
PASS Check response returned by static json() with init {"headers":{"content-type":"foo/bar"}}
PASS Check response returned by static json() with init {"headers":{"x-foo":"bar"}}
PASS Throws TypeError when calling static json() with a status of 204
PASS Throws TypeError when calling static json() with a status of 205
PASS Throws TypeError when calling static json() with a status of 304
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)"
PASS Check static json() encodes JSON objects correctly
PASS Check static json() throws when data is not encodable
PASS Check static json() throws when data is circular
FAIL Check static json() propagates JSON serializer errors assert_throws_js: function "function () {
Response.json({ get foo() { throw new CustomError("bar") }});
}" 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 {
name = "CustomError";
}" ("CustomError")
PASS Check static json() propagates JSON serializer errors

11 changes: 10 additions & 1 deletion Source/WebCore/Modules/fetch/FetchBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class FetchBody {
WEBCORE_EXPORT ~FetchBody();
FetchBody& operator=(FetchBody&&) = default;

explicit FetchBody(String&& data)
: m_data(WTFMove(data))
{
}

WEBCORE_EXPORT static std::optional<FetchBody> fromFormData(ScriptExecutionContext&, Ref<FormData>&&);

void loadingFailed(const Exception&);
Expand Down Expand Up @@ -96,7 +101,6 @@ class FetchBody {
explicit FetchBody(Ref<const ArrayBuffer>&& data) : m_data(WTFMove(data)) { }
explicit FetchBody(Ref<const ArrayBufferView>&& data) : m_data(WTFMove(data)) { }
explicit FetchBody(Ref<FormData>&& data) : m_data(WTFMove(data)) { }
explicit FetchBody(String&& data) : m_data(WTFMove(data)) { }
explicit FetchBody(Ref<const URLSearchParams>&& data) : m_data(WTFMove(data)) { }
explicit FetchBody(Ref<ReadableStream>&& stream) : m_data(stream) { m_readableStream = WTFMove(stream); }
explicit FetchBody(FetchBodyConsumer&& consumer) : m_consumer(WTFMove(consumer)) { }
Expand Down Expand Up @@ -130,4 +134,9 @@ class FetchBody {
RefPtr<ReadableStream> m_readableStream;
};

struct FetchBodyWithType {
FetchBody body;
String type;
};

} // namespace WebCore

0 comments on commit 206817f

Please sign in to comment.