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.redirect static method and Response.prototyp…
…e.redirected getter
- Loading branch information
1 parent
e2bb2ee
commit 1623d74
Showing
9 changed files
with
243 additions
and
11 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
44 changes: 44 additions & 0 deletions
44
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* eslint-env serviceworker */ | ||
import { pass, assert, assertThrows } from "../../../assertions.js"; | ||
import { routes } from "./test-harness.js"; | ||
|
||
routes.set("/response/redirect", async () => { | ||
const url = "http://test.url:1234/"; | ||
const redirectResponse = Response.redirect(url); | ||
let error = assert(redirectResponse.type, "default"); | ||
if (error) { return error; } | ||
error = assert(redirectResponse.redirected, false); | ||
if (error) { return error; } | ||
error = assert(redirectResponse.ok, false); | ||
if (error) { return error; } | ||
error = assert(redirectResponse.status, 302, "Default redirect status is 302"); | ||
if (error) { return error; } | ||
error = assert(redirectResponse.headers.get("Location"), url) | ||
if (error) { return error; } | ||
error = assert(redirectResponse.statusText, ""); | ||
if (error) { return error; } | ||
|
||
for (const status of [301, 302, 303, 307, 308]) { | ||
const redirectResponse = Response.redirect(url, status); | ||
error = assert(redirectResponse.type, "default"); | ||
if (error) { return error; } | ||
error = assert(redirectResponse.redirected, false); | ||
if (error) { return error; } | ||
error = assert(redirectResponse.ok, false); | ||
if (error) { return error; } | ||
error = assert(redirectResponse.status, status, "Redirect status is " + status); | ||
if (error) { return error; } | ||
error = assert(redirectResponse.headers.get("Location"), url); | ||
if (error) { return error; } | ||
error = assert(redirectResponse.statusText, ""); | ||
if (error) { return error; } | ||
} | ||
const invalidUrl = "http://:This is not an url"; | ||
error = assertThrows(function () { Response.redirect(invalidUrl); }, TypeError); | ||
if (error) { return error; } | ||
for (const invalidStatus of [200, 309, 400, 500]) { | ||
error = assertThrows(function () { Response.redirect(url, invalidStatus); }, RangeError); | ||
if (error) { return error; } | ||
} | ||
return pass() | ||
}) |
32 changes: 32 additions & 0 deletions
32
integration-tests/js-compute/fixtures/response-redirect/bin/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* eslint-env serviceworker */ | ||
import { env } from 'fastly:env'; | ||
import { fail } from "../../../assertions.js"; | ||
|
||
addEventListener("fetch", event => { | ||
event.respondWith(app(event)) | ||
}) | ||
/** | ||
* @param {FetchEvent} event | ||
* @returns {Response} | ||
*/ | ||
async function app(event) { | ||
try { | ||
const path = (new URL(event.request.url)).pathname | ||
console.log(`path: ${path}`) | ||
console.log(`FASTLY_SERVICE_VERSION: ${env('FASTLY_SERVICE_VERSION')}`) | ||
if (routes.has(path)) { | ||
const routeHandler = routes.get(path) | ||
return await routeHandler() | ||
} | ||
return fail(`${path} endpoint does not exist`) | ||
} catch (error) { | ||
return fail(`The routeHandler threw an error: ${error.message}` + '\n' + error.stack) | ||
} | ||
} | ||
|
||
export const routes = new Map() | ||
routes.set('/', () => { | ||
routes.delete('/') | ||
let test_routes = Array.from(routes.keys()) | ||
return new Response(JSON.stringify(test_routes), { 'headers': { 'content-type': 'application/json' } }) | ||
}) |
12 changes: 12 additions & 0 deletions
12
integration-tests/js-compute/fixtures/response-redirect/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-redirect" | ||
service_id = "" | ||
|
||
[scripts] | ||
build = "node ../../../../js-compute-runtime-cli.js" |
12 changes: 12 additions & 0 deletions
12
integration-tests/js-compute/fixtures/response-redirect/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/redirect": { | ||
"environments": ["viceroy", "c@e"], | ||
"downstream_request": { | ||
"method": "GET", | ||
"pathname": "/response/redirect" | ||
}, | ||
"downstream_response": { | ||
"status": 200 | ||
} | ||
} | ||
} |
20 changes: 10 additions & 10 deletions
20
tests/wpt-harness/expectations/fetch/api/response/response-static-redirect.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 |
---|---|---|
@@ -1,35 +1,35 @@ | ||
{ | ||
"Check default redirect response": { | ||
"status": "FAIL" | ||
"status": "PASS" | ||
}, | ||
"Check response returned by static method redirect(), status = 301": { | ||
"status": "FAIL" | ||
"status": "PASS" | ||
}, | ||
"Check response returned by static method redirect(), status = 302": { | ||
"status": "FAIL" | ||
"status": "PASS" | ||
}, | ||
"Check response returned by static method redirect(), status = 303": { | ||
"status": "FAIL" | ||
"status": "PASS" | ||
}, | ||
"Check response returned by static method redirect(), status = 307": { | ||
"status": "FAIL" | ||
"status": "PASS" | ||
}, | ||
"Check response returned by static method redirect(), status = 308": { | ||
"status": "FAIL" | ||
"status": "PASS" | ||
}, | ||
"Check error returned when giving invalid url to redirect()": { | ||
"status": "PASS" | ||
}, | ||
"Check error returned when giving invalid status to redirect(), status = 200": { | ||
"status": "FAIL" | ||
"status": "PASS" | ||
}, | ||
"Check error returned when giving invalid status to redirect(), status = 309": { | ||
"status": "FAIL" | ||
"status": "PASS" | ||
}, | ||
"Check error returned when giving invalid status to redirect(), status = 400": { | ||
"status": "FAIL" | ||
"status": "PASS" | ||
}, | ||
"Check error returned when giving invalid status to redirect(), status = 500": { | ||
"status": "FAIL" | ||
"status": "PASS" | ||
} | ||
} |