Skip to content

Commit 923f777

Browse files
KhafraDevronag
authored andcommitted
wpt: add redirect-location.any.js
1 parent b460838 commit 923f777

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

test/wpt/status/fetch.status.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,24 @@
134134
"fail": [
135135
"Redirect 303 with TESTING"
136136
]
137+
},
138+
"redirect-location.any.js": {
139+
"fail": [
140+
"Redirect 301 in \"manual\" mode without location",
141+
"Redirect 301 in \"manual\" mode with invalid location",
142+
"Redirect 301 in \"manual\" mode with data location",
143+
"Redirect 302 in \"manual\" mode without location",
144+
"Redirect 302 in \"manual\" mode with invalid location",
145+
"Redirect 302 in \"manual\" mode with data location",
146+
"Redirect 303 in \"manual\" mode without location",
147+
"Redirect 303 in \"manual\" mode with invalid location",
148+
"Redirect 303 in \"manual\" mode with data location",
149+
"Redirect 307 in \"manual\" mode without location",
150+
"Redirect 307 in \"manual\" mode with invalid location",
151+
"Redirect 307 in \"manual\" mode with data location",
152+
"Redirect 308 in \"manual\" mode without location",
153+
"Redirect 308 in \"manual\" mode with invalid location",
154+
"Redirect 308 in \"manual\" mode with data location"
155+
]
137156
}
138157
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// META: global=window,worker
2+
// META: script=../resources/utils.js
3+
4+
function redirectLocation(desc, redirectUrl, redirectLocation, redirectStatus, redirectMode, shouldPass) {
5+
var url = redirectUrl;
6+
var urlParameters = "?redirect_status=" + redirectStatus;
7+
if (redirectLocation)
8+
urlParameters += "&location=" + encodeURIComponent(redirectLocation);
9+
10+
var requestInit = {"redirect": redirectMode};
11+
12+
promise_test(function(test) {
13+
if (redirectMode === "error" || !shouldPass)
14+
return promise_rejects_js(test, TypeError, fetch(url + urlParameters, requestInit));
15+
if (redirectMode === "manual")
16+
return fetch(url + urlParameters, requestInit).then(function(resp) {
17+
assert_equals(resp.status, 0, "Response's status is 0");
18+
assert_equals(resp.type, "opaqueredirect", "Response's type is opaqueredirect");
19+
assert_equals(resp.statusText, "", "Response's statusText is \"\"");
20+
assert_true(resp.headers.entries().next().done, "Headers should be empty");
21+
});
22+
23+
if (redirectMode === "follow")
24+
return fetch(url + urlParameters, requestInit).then(function(resp) {
25+
assert_equals(resp.status, redirectStatus, "Response's status is " + redirectStatus);
26+
});
27+
assert_unreached(redirectMode + " is not a valid redirect mode");
28+
}, desc);
29+
}
30+
31+
var redirUrl = RESOURCES_DIR + "redirect.py";
32+
var locationUrl = "top.txt";
33+
var invalidLocationUrl = "invalidurl:";
34+
var dataLocationUrl = "data:,data%20url";
35+
// FIXME: We may want to mix redirect-mode and cors-mode.
36+
// FIXME: Add tests for "error" redirect-mode.
37+
for (var statusCode of [301, 302, 303, 307, 308]) {
38+
redirectLocation("Redirect " + statusCode + " in \"follow\" mode without location", redirUrl, undefined, statusCode, "follow", true);
39+
redirectLocation("Redirect " + statusCode + " in \"manual\" mode without location", redirUrl, undefined, statusCode, "manual", true);
40+
41+
redirectLocation("Redirect " + statusCode + " in \"follow\" mode with invalid location", redirUrl, invalidLocationUrl, statusCode, "follow", false);
42+
redirectLocation("Redirect " + statusCode + " in \"manual\" mode with invalid location", redirUrl, invalidLocationUrl, statusCode, "manual", true);
43+
44+
redirectLocation("Redirect " + statusCode + " in \"follow\" mode with data location", redirUrl, dataLocationUrl, statusCode, "follow", false);
45+
redirectLocation("Redirect " + statusCode + " in \"manual\" mode with data location", redirUrl, dataLocationUrl, statusCode, "manual", true);
46+
}
47+
48+
done();

0 commit comments

Comments
 (0)