Skip to content
File

Blob: src/workerd/api/tests/websocket-client-error-test.js

javascript64 lines
1// Copyright (c) 2025 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5// This test verifies that WebSocket protocol errors are properly converted to JavaScript errors
6// using the JsgifyWebSocketErrors handler.
7 
8export default {
9 async test(controller, env) {
10 const ws = new WebSocket(
11 `ws://${env.SIDECAR_HOSTNAME}:${env.BIG_MESSAGE_SERVER_PORT}/`
12 );
13 let receivedError = false;
14 
15 // Set up a promise to wait for errors
16 const errorPromise = new Promise((resolve, reject) => {
17 // Set a timeout
18 const timeoutId = setTimeout(() => {
19 reject({
20 source: 'timeout',
21 message: 'No error received within timeout',
22 });
23 }, 5000);
24 
25 // Handle errors
26 ws.addEventListener('error', (event) => {
27 clearTimeout(timeoutId);
28 receivedError = true;
29 
30 if (
31 event.message ===
32 'Uncaught Error: WebSocket protocol error; protocolError.statusCode = 1009; protocolError.description = Message is too large: 34603008 > 33554432'
33 ) {
34 resolve({
35 source: 'client-error',
36 message: event.message,
37 });
38 } else {
39 reject({
40 source: 'client-error-malformed',
41 message: event.message,
42 });
43 }
44 });
45 
46 ws.addEventListener('open', () => {});
47 // Handle close
48 ws.addEventListener('close', (event) => {
49 if (!(receivedError && event.code === 1009)) {
50 clearTimeout(timeoutId);
51 reject({
52 source: 'client-close',
53 code: event.code,
54 reason: event.reason,
55 });
56 }
57 });
58 });
59 
60 // Wait for an error or timeout
61 await errorPromise;
62 },
63};