Skip to content

Commit 75c3ce3

Browse files
KhafraDevronag
authored andcommitted
wpt: add http-response-code.any.js
1 parent af38ecb commit 75c3ce3

File tree

4 files changed

+130
-1
lines changed

4 files changed

+130
-1
lines changed

test/wpt/runner/runner/util.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export function handlePipes (code, url) {
126126
export function normalizeName (name) {
127127
return name.replace(/(\v)/g, (_, match) => {
128128
switch (inspect(match)) {
129-
case `'\\x0B'`: return '\\x0B'
129+
case '\'\\x0B\'': return '\\x0B'
130130
default: return match
131131
}
132132
})
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
const stash = new Map()
2+
3+
/**
4+
* @see https://github.com/web-platform-tests/wpt/blob/master/fetch/connection-pool/resources/network-partition-key.py
5+
* @param {Parameters<import('http').RequestListener>[0]} req
6+
* @param {Parameters<import('http').RequestListener>[1]} res
7+
* @param {URL} url
8+
*/
9+
export function route (req, res, { searchParams, port }) {
10+
res.setHeader('Cache-Control', 'no-store')
11+
12+
const dispatch = searchParams.get('dispatch')
13+
const uuid = searchParams.get('uuid')
14+
const partitionId = searchParams.get('partition_id')
15+
16+
if (!uuid || !dispatch || !partitionId) {
17+
res.statusCode = 404
18+
res.end('Invalid query parameters')
19+
return
20+
}
21+
22+
let testFailed = false
23+
let requestCount = 0
24+
let connectionCount = 0
25+
26+
if (searchParams.get('nocheck_partition') !== 'True') {
27+
const addressKey = `${req.socket.localAddress}|${port}`
28+
const serverState = stash.get(uuid) ?? {
29+
testFailed: false,
30+
requestCount: 0,
31+
connectionCount: 0
32+
}
33+
34+
stash.delete(uuid)
35+
requestCount = serverState.requestCount + 1
36+
serverState.requestCount = requestCount
37+
38+
if (Object.hasOwn(serverState, addressKey)) {
39+
if (serverState[addressKey] !== partitionId) {
40+
serverState.testFailed = true
41+
}
42+
} else {
43+
connectionCount = serverState.connectionCount + 1
44+
serverState.connectionCount = connectionCount
45+
}
46+
47+
serverState[addressKey] = partitionId
48+
testFailed = serverState.testFailed
49+
stash.set(uuid, serverState)
50+
}
51+
52+
const origin = req.headers.origin
53+
if (origin) {
54+
res.setHeader('Access-Control-Allow-Origin', origin)
55+
res.setHeader('Access-Control-Allow-Credentials', 'true')
56+
}
57+
58+
if (req.method === 'OPTIONS') {
59+
return handlePreflight(req, res)
60+
}
61+
62+
if (dispatch === 'fetch_file') {
63+
res.end()
64+
return
65+
}
66+
67+
if (dispatch === 'check_partition') {
68+
const status = searchParams.get('status') ?? 200
69+
70+
if (testFailed) {
71+
res.statusCode = status
72+
res.end('Multiple partition IDs used on a socket')
73+
return
74+
}
75+
76+
let body = 'ok'
77+
if (searchParams.get('addcounter')) {
78+
body += `. Request was sent ${requestCount} times. ${connectionCount} connections were created.`
79+
res.statusCode = status
80+
res.end(body)
81+
return
82+
}
83+
}
84+
85+
if (dispatch === 'clean_up') {
86+
stash.delete(uuid)
87+
res.statusCode = 200
88+
if (testFailed) {
89+
res.end('Test failed, but cleanup completed.')
90+
} else {
91+
res.end('cleanup complete')
92+
}
93+
94+
return
95+
}
96+
97+
res.statusCode = 404
98+
res.end('Unrecognized dispatch parameter: ' + dispatch)
99+
}
100+
101+
/**
102+
* @param {Parameters<import('http').RequestListener>[0]} req
103+
* @param {Parameters<import('http').RequestListener>[1]} res
104+
*/
105+
function handlePreflight (req, res) {
106+
res.statusCode = 200
107+
res.setHeader('Access-Control-Allow-Methods', 'GET')
108+
res.setHeader('Access-Control-Allow-Headers', 'header-to-force-cors')
109+
res.setHeader('Access-Control-Max-Age', '86400')
110+
res.end('Preflight request')
111+
}

test/wpt/server/server.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ 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'
89

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

@@ -218,6 +219,9 @@ const server = createServer(async (req, res) => {
218219
res.end()
219220
break
220221
}
222+
case '/fetch/connection-pool/resources/network-partition-key.py': {
223+
return route(req, res, fullUrl)
224+
}
221225
default: {
222226
res.statusCode = 200
223227
res.end('body')
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// META: global=window,worker
2+
// META: script=../resources/utils.js
3+
// META: script=/common/utils.js
4+
// META: script=/common/get-host-info.sub.js
5+
6+
promise_test(async (test) => {
7+
const resp = await fetch(
8+
"/fetch/connection-pool/resources/network-partition-key.py?"
9+
+ `status=425&uuid=${token()}&partition_id=${get_host_info().ORIGIN}`
10+
+ `&dispatch=check_partition&addcounter=true`);
11+
assert_equals(resp.status, 425);
12+
const text = await resp.text();
13+
assert_equals(text, "ok. Request was sent 1 times. 1 connections were created.");
14+
}, "Fetch on 425 response should not be retried for non TLS early data.");

0 commit comments

Comments
 (0)