Skip to content

Commit 76d7095

Browse files
KhafraDevronag
authored andcommitted
wpt: add redirect-to-dataurl.any.js
1 parent 0dab43c commit 76d7095

File tree

3 files changed

+136
-2
lines changed

3 files changed

+136
-2
lines changed

test/wpt/server/routes/redirect.mjs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { setTimeout } from 'timers/promises'
2+
3+
const stash = new Map()
4+
5+
/**
6+
* @see https://github.com/web-platform-tests/wpt/blob/master/fetch/connection-pool/resources/network-partition-key.py
7+
* @param {Parameters<import('http').RequestListener>[0]} req
8+
* @param {Parameters<import('http').RequestListener>[1]} res
9+
* @param {URL} url
10+
*/
11+
export async function route (req, res, { searchParams }) {
12+
let stashedData = { count: 0, preflight: 0 }
13+
let status = 302
14+
res.setHeader('Content-Type', 'text/plain')
15+
res.setHeader('Cache-Control', 'no-cache')
16+
res.setHeader('Pragma', 'no-cache')
17+
18+
if (Object.hasOwn(req.headers, 'origin')) {
19+
res.setHeader('Access-Control-Allow-Origin', req.headers.origin ?? '')
20+
res.setHeader('Access-Control-Allow-Credentials', 'true')
21+
} else {
22+
res.setHeader('Access-Control-Allow-Origin', '*')
23+
}
24+
25+
let token = null
26+
if (searchParams.has('token')) {
27+
token = searchParams.get('token')
28+
const data = stash.get(token)
29+
stash.delete(token)
30+
if (data) {
31+
stashedData = data
32+
}
33+
}
34+
35+
if (req.method === 'OPTIONS') {
36+
if (searchParams.has('allow_headers')) {
37+
res.setHeader('Access-Control-Allow-Headers', searchParams.get('allow_headers'))
38+
}
39+
40+
stashedData.preflight = '1'
41+
42+
if (!searchParams.has('redirect_preflight')) {
43+
if (token) {
44+
stash.set(searchParams.get('token'), stashedData)
45+
}
46+
47+
res.statusCode = 200
48+
res.end('')
49+
return
50+
}
51+
}
52+
53+
if (searchParams.has('redirect_status')) {
54+
status = parseInt(searchParams.get('redirect_status'))
55+
}
56+
57+
stashedData.count += 1
58+
59+
if (searchParams.has('location')) {
60+
let url = decodeURIComponent(searchParams.get('location'))
61+
62+
if (!searchParams.has('simple')) {
63+
const scheme = new URL(url).protocol
64+
65+
if (scheme === 'http:' || scheme === 'https:') {
66+
url += url.includes('?') ? '&' : '?'
67+
68+
for (const [key, value] of searchParams) {
69+
url += '&' + encodeURIComponent(key) + '=' + encodeURIComponent(value)
70+
}
71+
72+
url += '&count=' + stashedData.count
73+
}
74+
}
75+
76+
res.setHeader('location', url)
77+
}
78+
79+
if (searchParams.has('redirect_referrerpolicy')) {
80+
res.setHeader('Referrer-Policy', searchParams.get('redirect_referrerpolicy'))
81+
}
82+
83+
if (searchParams.has('delay')) {
84+
await setTimeout(parseFloat(searchParams.get('delay') ?? 0))
85+
}
86+
87+
if (token) {
88+
stash.set(searchParams.get('token'), stashedData)
89+
90+
if (searchParams.has('max_count')) {
91+
const maxCount = parseInt(searchParams.get('max_count'))
92+
93+
if (stashedData.count > maxCount) {
94+
res.end((stashedData.count - 1).toString())
95+
return
96+
}
97+
}
98+
}
99+
100+
res.statusCode = status
101+
res.end('')
102+
}

test/wpt/server/server.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import process from 'node:process'
55
import { fileURLToPath } from 'node:url'
66
import { createReadStream, readFileSync } from 'node:fs'
77
import { setTimeout as sleep } from 'node:timers/promises'
8-
import { route } from './routes/network-partition-key.mjs'
8+
import { route as networkPartitionRoute } from './routes/network-partition-key.mjs'
9+
import { route as redirectRoute } from './routes/redirect.mjs'
910

1011
const tests = fileURLToPath(join(import.meta.url, '../../tests'))
1112

@@ -221,13 +222,16 @@ const server = createServer(async (req, res) => {
221222
break
222223
}
223224
case '/fetch/connection-pool/resources/network-partition-key.py': {
224-
return route(req, res, fullUrl)
225+
return networkPartitionRoute(req, res, fullUrl)
225226
}
226227
case '/resources/top.txt': {
227228
return createReadStream(join(tests, 'fetch/api/', fullUrl.pathname))
228229
.on('end', () => res.end())
229230
.pipe(res)
230231
}
232+
case '/fetch/api/resources/redirect.py': {
233+
return redirectRoute(req, res, fullUrl)
234+
}
231235
default: {
232236
res.statusCode = 200
233237
res.end('body')
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// META: global=window,worker
2+
// META: script=/common/get-host-info.sub.js
3+
4+
var dataURL = "data:text/plain;base64,cmVzcG9uc2UncyBib2R5";
5+
var body = "response's body";
6+
var contentType = "text/plain";
7+
8+
function redirectDataURL(desc, redirectUrl, mode) {
9+
var url = redirectUrl + "?cors&location=" + encodeURIComponent(dataURL);
10+
11+
var requestInit = {"mode": mode};
12+
13+
promise_test(function(test) {
14+
return promise_rejects_js(test, TypeError, fetch(url, requestInit));
15+
}, desc);
16+
}
17+
18+
var redirUrl = get_host_info().HTTP_ORIGIN + "/fetch/api/resources/redirect.py";
19+
var corsRedirUrl = get_host_info().HTTP_REMOTE_ORIGIN + "/fetch/api/resources/redirect.py";
20+
21+
redirectDataURL("Testing data URL loading after same-origin redirection (cors mode)", redirUrl, "cors");
22+
redirectDataURL("Testing data URL loading after same-origin redirection (no-cors mode)", redirUrl, "no-cors");
23+
redirectDataURL("Testing data URL loading after same-origin redirection (same-origin mode)", redirUrl, "same-origin");
24+
25+
redirectDataURL("Testing data URL loading after cross-origin redirection (cors mode)", corsRedirUrl, "cors");
26+
redirectDataURL("Testing data URL loading after cross-origin redirection (no-cors mode)", corsRedirUrl, "no-cors");
27+
28+
done();

0 commit comments

Comments
 (0)