Skip to content

Commit 4d15b9a

Browse files
committed
add websockets web-platform test suite
1 parent 18f01cb commit 4d15b9a

File tree

8 files changed

+523
-11
lines changed

8 files changed

+523
-11
lines changed

build/wpt_test.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ const unitTests :Workerd.Config = (
295295
(name = "SIDECAR_HOSTNAME", fromEnvironment = "SIDECAR_HOSTNAME"),
296296
(name = "HTTP_PORT", fromEnvironment = "HTTP_PORT"),
297297
(name = "HTTPS_PORT", fromEnvironment = "HTTPS_PORT"),
298+
(name = "WS_PORT", fromEnvironment = "WS_PORT"),
299+
(name = "WSS_PORT", fromEnvironment = "WSS_PORT"),
298300
(name = "GEN_TEST_CONFIG", fromEnvironment = "GEN_TEST_CONFIG"),
299301
(name = "GEN_TEST_REPORT", fromEnvironment = "GEN_TEST_REPORT"),
300302
(name = "GEN_TEST_STATS", fromEnvironment = "GEN_TEST_STATS"),

justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ new-wpt-test test_name:
7777
echo 'wpt_test(name = "{{test_name}}", config = "{{test_name}}-test.ts", wpt_directory = "@wpt//:{{test_name}}@module")' >> src/wpt/BUILD.bazel
7878

7979
./tools/cross/format.py
80-
bazel test //src/wpt:{{test_name}} --test_env=GEN_TEST_CONFIG=1 --test_output=streamed
80+
bazel test //src/wpt:{{test_name}}@ --test_env=GEN_TEST_CONFIG=1 --test_output=streamed
8181

8282
# Specify the full Bazel target name for the test to be created.
8383
# e.g. just new-test //src/workerd/api/tests:v8-temporal-test

src/wpt/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,17 @@ wpt_test(
129129
config = "performance-timeline-test.ts",
130130
wpt_directory = "@wpt//:performance-timeline@module",
131131
)
132+
133+
wpt_test(
134+
name = "websockets",
135+
size = "large",
136+
config = "websockets-test.ts",
137+
start_server = True,
138+
target_compatible_with = select({
139+
# TODO(later): Provide a Windows version of the sidecar script we wrote to invoke wptserve.
140+
# Currently we only have a Unix shell script.
141+
"@platforms//os:windows": ["@platforms//:incompatible"],
142+
"//conditions:default": [],
143+
}),
144+
wpt_directory = "@wpt//:websockets@module",
145+
)

src/wpt/harness/assertions.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,24 @@ declare global {
103103
description?: string
104104
): void;
105105

106+
function assert_greater_than_equal(
107+
actual: number,
108+
expected: number,
109+
description?: string
110+
): void;
111+
112+
function assert_less_than(
113+
actual: number,
114+
expected: number,
115+
description?: string
116+
): void;
117+
118+
function assert_less_than_equal(
119+
actual: number,
120+
expected: number,
121+
description?: string
122+
): void;
123+
106124
function promise_rejects_exactly(
107125
test: Test,
108126
exception: typeof Error,
@@ -481,6 +499,43 @@ globalThis.assert_greater_than = (actual, expected, description): void => {
481499
ok(actual > expected, description);
482500
};
483501

502+
/**
503+
* Assert that ``actual`` is a number greater than or equal to ``expected``.
504+
*
505+
* @param actual - Test value.
506+
* @param expected - Number that ``actual`` must be greater than or equal to.
507+
* @param [description] - Description of the condition being tested.
508+
*/
509+
globalThis.assert_greater_than_equal = (
510+
actual,
511+
expected,
512+
description
513+
): void => {
514+
ok(actual >= expected, description);
515+
};
516+
517+
/**
518+
* Assert that ``actual`` is a number less than ``expected``.
519+
*
520+
* @param actual - Test value.
521+
* @param expected - Number that ``actual`` must be less than.
522+
* @param [description] - Description of the condition being tested.
523+
*/
524+
globalThis.assert_less_than = (actual, expected, description): void => {
525+
ok(actual < expected, description);
526+
};
527+
528+
/**
529+
* Assert that ``actual`` is a number less than or equal to ``expected``.
530+
*
531+
* @param actual - Test value.
532+
* @param expected - Number that ``actual`` must be less than or equal to.
533+
* @param [description] - Description of the condition being tested.
534+
*/
535+
globalThis.assert_less_than_equal = (actual, expected, description): void => {
536+
ok(actual <= expected, description);
537+
};
538+
484539
/**
485540
* Assert that a Promise is rejected with the provided value.
486541
*

src/wpt/harness/common.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ export type HostInfo = {
142142
HTTPS_REMOTE_ORIGIN: string;
143143
HTTP_PORT: string;
144144
HTTPS_PORT: string;
145+
WS_PORT: string;
146+
WSS_PORT: string;
145147
};
146148

147149
export function getHostInfo(): HostInfo {
@@ -163,6 +165,8 @@ export function getHostInfo(): HostInfo {
163165
HTTPS_REMOTE_ORIGIN: httpsUrl.origin,
164166
HTTP_PORT: httpUrl.port,
165167
HTTPS_PORT: httpsUrl.port,
168+
WS_PORT: (globalThis.state.env.WS_PORT as string) ?? '',
169+
WSS_PORT: (globalThis.state.env.WSS_PORT as string) ?? '',
166170
};
167171
}
168172

src/wpt/harness/harness.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,14 @@ function replaceInterpolation(code: string): string {
302302
const hostInfo = getHostInfo();
303303

304304
return code
305-
.replace('{{host}}', hostInfo.REMOTE_HOST)
306-
.replace('{{ports[http][0]}}', hostInfo.HTTP_PORT)
307-
.replace('{{ports[http][1]}}', hostInfo.HTTP_PORT)
308-
.replace('{{ports[https][0]}}', hostInfo.HTTPS_PORT);
305+
.replace(/\{\{host\}\}/g, hostInfo.REMOTE_HOST)
306+
.replace(/\{\{hosts\[alt\]\[www\]\}\}/g, hostInfo.REMOTE_HOST)
307+
.replace(/\{\{ports\[http\]\[0\]\}\}/g, hostInfo.HTTP_PORT)
308+
.replace(/\{\{ports\[http\]\[1\]\}\}/g, hostInfo.HTTP_PORT)
309+
.replace(/\{\{ports\[https\]\[0\]\}\}/g, hostInfo.HTTPS_PORT)
310+
.replace(/\{\{ports\[ws\]\[0\]\}\}/g, hostInfo.WS_PORT)
311+
.replace(/\{\{ports\[wss\]\[0\]\}\}/g, hostInfo.WSS_PORT)
312+
.replace(/\{\{ports\[h2\]\[0\]\}\}/g, hostInfo.HTTPS_PORT);
309313
}
310314

311315
function getCodeAtPath(

src/wpt/harness/test.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ declare global {
3636
name?: string,
3737
properties?: unknown
3838
): void;
39-
function async_test(func: TestFn, name?: string, properties?: unknown): void;
39+
function async_test(
40+
func: TestFn | string,
41+
name?: string,
42+
properties?: unknown
43+
): Test;
4044
function test(func: TestFn, name?: string, properties?: unknown): void;
4145
}
4246

@@ -293,15 +297,38 @@ class AsyncTest extends Test {
293297
}
294298
}
295299

296-
globalThis.async_test = (func, name, properties): void => {
297-
if (maybeAddSkippedTest(name ?? '')) {
298-
return;
300+
globalThis.async_test = (func, name, properties): Test => {
301+
// async_test can be called in two ways:
302+
// 1. async_test(func, name, properties) - func is a TestFn
303+
// 2. async_test(name, properties) - just creates a test with the given name
304+
let testName: string;
305+
let testFunc: TestFn | undefined;
306+
307+
if (typeof func === 'string') {
308+
// async_test(name, properties) signature
309+
testName = func;
310+
testFunc = undefined;
311+
// name parameter is actually properties in this case
312+
properties = name;
313+
} else {
314+
// async_test(func, name, properties) signature
315+
testName = name ?? '';
316+
testFunc = func;
299317
}
300318

301-
const testCase = new AsyncTest(name ?? '', properties);
319+
if (maybeAddSkippedTest(testName)) {
320+
// Return a dummy test object for skipped tests
321+
return new SkippedTest(testName, 'DISABLED');
322+
}
323+
324+
const testCase = new AsyncTest(testName, properties);
302325
globalThis.state.subtests.push(testCase);
303326

304-
testCase.step(func, testCase, testCase);
327+
if (testFunc) {
328+
testCase.step(testFunc, testCase, testCase);
329+
}
330+
331+
return testCase;
305332
};
306333

307334
/**

0 commit comments

Comments
 (0)