Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement Response.json static method (#499)
Co-authored-by: Trevor Elliott <telliott@fastly.com>
- Loading branch information
1 parent
1a9be1c
commit 780067d
Showing
11 changed files
with
313 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
integration-tests/js-compute/fixtures/response-json/bin/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* eslint-env serviceworker */ | ||
import { pass, assert, assertThrows } from "../../../assertions.js"; | ||
import { routes } from "../../../test-harness.js"; | ||
|
||
let error; | ||
routes.set("/response/json", async () => { | ||
const APPLICATION_JSON = "application/json"; | ||
const FOO_BAR = "foo/bar"; | ||
|
||
const INIT_TESTS = [ | ||
[undefined, 200, "", APPLICATION_JSON, {}], | ||
[{ status: 400 }, 400, "", APPLICATION_JSON, {}], | ||
[{ statusText: "foo" }, 200, "foo", APPLICATION_JSON, {}], | ||
[{ headers: {} }, 200, "", APPLICATION_JSON, {}], | ||
[{ headers: { "content-type": FOO_BAR } }, 200, "", FOO_BAR, {}], | ||
[{ headers: { "x-foo": "bar" } }, 200, "", APPLICATION_JSON, { "x-foo": "bar" }], | ||
]; | ||
|
||
for (const [init, expectedStatus, expectedStatusText, expectedContentType, expectedHeaders] of INIT_TESTS) { | ||
const response = Response.json("hello world", init); | ||
error = assert(response.type, "default", 'response.type'); | ||
if (error) { return error; } | ||
error = assert(response.status, expectedStatus, 'response.status'); | ||
if (error) { return error; } | ||
error = assert(response.statusText, expectedStatusText, 'response.statusText'); | ||
if (error) { return error; } | ||
error = assert(response.headers.get("content-type"), expectedContentType, 'response.headers.get("content-type")'); | ||
if (error) { return error; } | ||
for (const key in expectedHeaders) { | ||
error = assert(response.headers.get(key), expectedHeaders[key], 'response.headers.get(key)'); | ||
if (error) { return error; } | ||
} | ||
const data = await response.json(); | ||
error = assert(data, "hello world", 'data'); | ||
if (error) { return error; } | ||
} | ||
|
||
const nullBodyStatus = [204, 205, 304]; | ||
for (const status of nullBodyStatus) { | ||
error = assertThrows( | ||
function () { | ||
Response.json("hello world", { status: status }); | ||
}, | ||
TypeError, | ||
); | ||
if (error) { return error; } | ||
} | ||
|
||
const response = Response.json({ foo: "bar" }); | ||
const data = await response.json(); | ||
error = assert(typeof data, "object", 'typeof data'); | ||
if (error) { return error; } | ||
error = assert(data.foo, "bar", "data.foo"); | ||
if (error) { return error; } | ||
|
||
error = assertThrows( | ||
function () { | ||
Response.json(Symbol("foo")); | ||
}, | ||
TypeError | ||
); | ||
if (error) { return error; } | ||
|
||
const a = { b: 1 }; | ||
a.a = a; | ||
error = assertThrows( | ||
function () { | ||
Response.json(a); | ||
}, | ||
TypeError, | ||
); | ||
if (error) { return error; } | ||
|
||
class CustomError extends Error { | ||
name = "CustomError"; | ||
} | ||
error = assertThrows( | ||
function () { | ||
Response.json({ get foo() { throw new CustomError("bar") } }); | ||
}, | ||
CustomError, | ||
) | ||
if (error) { return error; } | ||
return pass() | ||
}); |
12 changes: 12 additions & 0 deletions
12
integration-tests/js-compute/fixtures/response-json/fastly.toml.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# This file describes a Fastly Compute@Edge package. To learn more visit: | ||
# https://developer.fastly.com/reference/fastly-toml/ | ||
|
||
authors = ["jchampion@fastly.com"] | ||
description = "" | ||
language = "other" | ||
manifest_version = 2 | ||
name = "response-json" | ||
service_id = "" | ||
|
||
[scripts] | ||
build = "node ../../../../js-compute-runtime-cli.js" |
12 changes: 12 additions & 0 deletions
12
integration-tests/js-compute/fixtures/response-json/tests.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"GET /response/json": { | ||
"environments": ["viceroy", "c@e"], | ||
"downstream_request": { | ||
"method": "GET", | ||
"pathname": "/response/json" | ||
}, | ||
"downstream_response": { | ||
"status": 200 | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
integration-tests/js-compute/fixtures/response-redirect/bin/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...res/response-redirect/bin/test-harness.js → integration-tests/js-compute/test-harness.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
tests/wpt-harness/expectations/fetch/api/response/response-static-json.any.js.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"Check response returned by static json() with init undefined": { | ||
"status": "PASS" | ||
}, | ||
"Check response returned by static json() with init {\"status\":400}": { | ||
"status": "PASS" | ||
}, | ||
"Check response returned by static json() with init {\"statusText\":\"foo\"}": { | ||
"status": "PASS" | ||
}, | ||
"Check response returned by static json() with init {\"headers\":{}}": { | ||
"status": "PASS" | ||
}, | ||
"Check response returned by static json() with init {\"headers\":{\"content-type\":\"foo/bar\"}}": { | ||
"status": "PASS" | ||
}, | ||
"Check response returned by static json() with init {\"headers\":{\"x-foo\":\"bar\"}}": { | ||
"status": "PASS" | ||
}, | ||
"Throws TypeError when calling static json() with a status of 204": { | ||
"status": "PASS" | ||
}, | ||
"Throws TypeError when calling static json() with a status of 205": { | ||
"status": "PASS" | ||
}, | ||
"Throws TypeError when calling static json() with a status of 304": { | ||
"status": "PASS" | ||
}, | ||
"Check static json() encodes JSON objects correctly": { | ||
"status": "PASS" | ||
}, | ||
"Check static json() throws when data is not encodable": { | ||
"status": "PASS" | ||
}, | ||
"Check static json() throws when data is circular": { | ||
"status": "PASS" | ||
}, | ||
"Check static json() propagates JSON serializer errors": { | ||
"status": "PASS" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters