Files
bun.sh/test/js/web/websocket/websocket.test.js
Jarred Sumner c9fe57fa63 wip use wrapper for managing process (#8456)
* WIP sync close (shows ref count bug in stream)

* fix closing on PipeWriter and PipeReader

* remove old todos

* join

* Some shell changes

at least it compiles

* fix some compile errors

* fix ref/unref server on windows

* actually use the ref count in this places

* make windows compile again

* more tests passing

* Make shell compile again

* Slowly remove some `@panic("TODO SHELL")`

* Eliminate `@panic("TODO SHELL")` for BufferedWriter

* Holy cleansing of `@panic("TODO SHELL")`

at least it compiles now

* Okay now the shell compiles, but segfaults

* Fix compiler errors

* more stable stream and now Content-Range pass

* make windows compile again

* revert stuff until the fix is actually ready

* revert onDone thing

* Fix buffered writer for shell

* Fix buffered writer + shell/subproc.zig and windows build

* Fix for #8982 got lost in the merge

* Actually buffer subproc output

* Fix some stuff shell

* oops

* fix context deinit

* fix renderMissing

* shell: Fix array buffer

* more stable streams (#9053)

fix stream ref counting

* wip

* Remove `@panic("TODO")` on shell event loop tasks and Redirect  open flags got lost in merge

* Support redirects

* fixes

cc @cirospaciari

* Update ReadableStreamInternals.ts

* Fix spurious error

* Update stream.js

* leak

* Fix UAF

cc @cirospaciari

* Fix memory leaks

* HOLY FUCK big refactor

* misc cleanup

* shell: Fix a bunch of tests

* clean up

* gitignore: fix ending newline

* get windows compiling again

* tidy

* hide linker warn with icu

* closeIfPossible

* Better leak test

* Fix forgetting to decrement reference count

* Update stdio.zig

* Fix shell windows build

* Stupid unreachable

* Woops

* basic echo hi works on windows

* Fix flaky test on Windows

* Fix windows regression in Bun.main (#9156)

* Fix windows regression in Bun.main

* Handle invalid handles

* Fix flaky test

* Better launch config

* Fixup

* Make this test less flaky on Windows

* Fixup

* Cygwin

* Support signal codes in subprocess.kill(), resolve file path

* Treat null as ignore

* Ignore carriage returns

* Fixup

* shell: Fix IOWriter bug

* shell: Use custom `open()`/`openat()`

* windows shell subproc works

* zack commit

* I think I understand WindowsStreamingWriter

* fix thing

* why were we doing this in tests

* shell: Fix rm

* shell: Add rm -rf node_modules/ test

* shell: use `.runAsTest()` in some places to make it easier to determine which test failed

* [autofix.ci] apply automated fixes

* woopsie

* Various changes

* Fix

* shell: abstract output task logic

* shell: mkdir builtin

* fixup

* stuff

* shell: Make writing length of 0 in IOWriter immediately resolve

* shell: Implement `touch`

* shell: basic `cat` working

* Make it compile on windows

* shell: Fix IOReader bug

* [autofix.ci] apply automated fixes

* fix windows kill on subprocess/process

* fix dns tests to match behavior on windows (same as nodejs)

* fix windows ci

* again

* move `close_handle` to flags in `PipeWriter` and fix shell hanging

* Fix `ls` not giving non-zero exit code on error

* Handle edgecase in is_atty

* Fix writer.flush() when there's no data

* Fix some tests

* Disable uv_unref on uv_process_t on Windows, for now.

* fix writer.end

* fix stdout.write

* fix child-process on win32

* Make this test less flaky on Windows

* Add assertion

* Make these the same

* Make it pass on windows

* Don't commit

* Log the test name

* Make this test less flaky on windows

* Make this test less flaky on windows

* Print which test is taking awhile in the runner

* fixups

* Fixups

* Add some assertions

* Bring back test concurrency

* shell: bring back redirect stdin

* make it compile again cc @zackradisic

* initialize env map with capacity

* some fixes

* cleanup

* oops

* fix leak, fix done

* fix unconsumedPromises on events

* always run expect

* Update child_process.test.ts

* fix reading special files

* Fix a test

* Deflake this test

* Make these comparisons easier

* Won't really fix it but slightly cleaner

* Update serve.test.ts

* Make the checks for if the body is already used more resilient

* Move this to the harness

* Make this test not hang in development

* Fix this test

* Make the logs better

* zero init some things

* Make this test better

* Fix readSocket

* Parallelize this test

* Handle EPipe and avoid big data

* This was a mistake

* Fix a bunch of things

* Fix memory leak

* Avoid sigpipe + optimize + delete dead code

* Make this take less time

* Make it bigger

* Remove some redundant code

* Update process.zig

* Merge and hopefully don't breka things along teh way

* Silence build warning

* Uncomment on posix

* Skip test on windows

* windows

* Cleanup test

* Update

* Deflake

* always

* less flaky test

* [autofix.ci] apply automated fixes

* logs

* fix uaf on shell IOReader

* stuff to make it work with mini event loop

* fix 2 double free scenarios, support redirections on windows

* shell: Make `1>&2` and `2>&1` work with libuv

* yoops

* Partial fix

* Partial fix

* fix build

* fix build

* ok

* Make a couple shell tests pass

* More logging

* fix

* fix

* Fix build issue

* more tests pass

* Deflake

* Deflake

* Use Output.panic instead of garbled text

* Formatting

* Introduce `bun.sys.File`, use it for `Output.Source.StreamType`, fix nested Output.scoped() calls, use Win32 `ReadFile` API for reading when it's not a libuv file descriptor.

This lets us avoid the subtle usages of `unreachable` in std.os when writing to stdout/stderr.

Previously, we were initializing the libuv loop immediately at launch due to checking for the existence of a bun build --compile'd executable. When the file descriptor is not from libuv, it's just overhead to use libuv

cc @paperdave, please tell me if Iany of that is incorrect or if you think this is a bad idea.

* Fix closing undefined memory file descriptors in spawn

cc @zackradisic

* pause instead of close

* Fix poorly-written test

* We don't need big numbers for this test

* sad workaround

* fixup

* Clearer error handling for this test

* Fix incorrect test

@electroid when ReadableStream isn't closed, hanging is the correct behavior when consuming buffered data. We cannot know if the buffered data is finished if the stream never closes.

* Fix build

* Remove known failing on windows

* Deflake

* Mark no longer failing

* show all the failing tests

* Sort the list of tests

* fix argument handling

* dont show "posix_spawn" as an error code on windows

* make bun-upgrade.test.ts pass on windows

* fix bunx and bun create again sorry

* a

* fix invalidexe because we should not be running javascript files as if they were exes

* Concurrency in test runner + better logging

* Revert "fix invalidexe because we should not be running javascript files as if they were exes"

This reverts commit da47cf8247.

* WIP: Unix fixes (#9322)

* wip

* [autofix.ci] apply automated fixes

* wip 2

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>

* Update runner.node.mjs

* Update runner.node.mjs

* Document some environment variables

* shell: Make `Response` work with builtins

* Make it compile

* make pwd test pass

* [autofix.ci] apply automated fixes

* Fix printing garbage for source code previews

* Update javascript.zig

* Fix posix test failures

* Fix signal dispatch

cc @paperdave. Signals can be run from any thread. This causes an assertion failure when the receiving thread happens to not be the main thread. Easiest to reproduce on linux when you spawn 100 short-lived processes at once.

* windows

---------

Co-authored-by: cirospaciari <ciro.spaciari@gmail.com>
Co-authored-by: Zack Radisic <56137411+zackradisic@users.noreply.github.com>
Co-authored-by: Zack Radisic <zackradisic@Zacks-MBP-2.attlocal.net>
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Co-authored-by: Meghan Denny <meghan@bun.sh>
Co-authored-by: Zack Radisic <zack@theradisic.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: dave caruso <me@paperdave.net>
Co-authored-by: Dylan Conway <dylan.conway567@gmail.com>
2024-03-11 08:24:30 -07:00

600 lines
21 KiB
JavaScript

import { describe, it, expect } from "bun:test";
import { bunExe, bunEnv, gc } from "harness";
import { readFileSync } from "fs";
import { join, resolve } from "path";
import process from "process";
const TEST_WEBSOCKET_HOST = process.env.TEST_WEBSOCKET_HOST || "wss://ws.postman-echo.com/raw";
const isWindows = process.platform === "win32";
describe("WebSocket", () => {
it("should connect", async () => {
const server = Bun.serve({
port: 0,
fetch(req, server) {
if (server.upgrade(req)) {
server.stop();
return;
}
return new Response();
},
websocket: {
open(ws) {},
message(ws) {
ws.close();
},
},
});
const ws = new WebSocket(`ws://${server.hostname}:${server.port}`, {});
await new Promise(resolve => {
ws.onopen = resolve;
});
var closed = new Promise(resolve => {
ws.onclose = resolve;
});
ws.close();
await closed;
server.stop(true);
});
it("should connect over https", async () => {
const ws = new WebSocket(TEST_WEBSOCKET_HOST.replaceAll("wss:", "https:"));
await new Promise((resolve, reject) => {
ws.onopen = resolve;
ws.onerror = reject;
});
var closed = new Promise((resolve, reject) => {
ws.onclose = resolve;
});
ws.close();
await closed;
});
it("rejectUnauthorized should reject self-sign certs when true/default", async () => {
const COMMON_CERT = {
cert: "-----BEGIN CERTIFICATE-----\nMIIDXTCCAkWgAwIBAgIJAKLdQVPy90jjMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV\nBAYTAkFVMRMwEQYDVQQID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cOO5rFbNsHexStrx/h8jYnpdpIVlxACjh1xIyJ3lOCSAWfBWCS6KpgeO1Y484k\nEYhGjoUsKNQia8UWVt+uWnwjVSDhQjy5/pSH9xyFrUfDg8JnSlhsy0oC0C/PBjxn\nhxmADSLnNwKBgQD2A51USVMTKC9Q50BsgeU6+bmt9aNMPvHAnPf76d5q78l4IlKt\nwMs33QgOExuYirUZSgjRwknmrbUi9QckRbxwOSqVeMOwOWLm1GmYaXRf39u2CTI5\nV9gTMHJ5jnKd4gYDnaA99eiOcBhgS+9PbgKSAyuUlWwR2ciL/4uDzaVeDQKBgDym\nvRSeTRn99bSQMMZuuD5N6wkD/RxeCbEnpKrw2aZVN63eGCtkj0v9LCu4gptjseOu\n7+a4Qplqw3B/SXN5/otqPbEOKv8Shl/PT6RBv06PiFKZClkEU2T3iH27sws2EGru\nw3C3GaiVMxcVewdg1YOvh5vH8ZVlxApxIzuFlDvnAoGAN5w+gukxd5QnP/7hcLDZ\nF+vesAykJX71AuqFXB4Wh/qFY92CSm7ImexWA/L9z461+NKeJwb64Nc53z59oA10\n/3o2OcIe44kddZXQVP6KTZBd7ySVhbtOiK3/pCy+BQRsrC7d71W914DxNWadwZ+a\njtwwKjDzmPwdIXDSQarCx0U=\n-----END PRIVATE KEY-----",
passphrase: "1234",
};
const server = Bun.serve({
port: 0,
tls: COMMON_CERT,
fetch(req, server) {
// upgrade the request to a WebSocket
if (server.upgrade(req)) {
return; // do not return a Response
}
return new Response("Upgrade failed :(", { status: 500 });
},
websocket: {
message(ws, message) {
ws.send(message);
ws.close();
}, // a message is received
open(ws) {
// a socket is opened
ws.send("Hello from Bun!");
},
},
});
try {
function testClient(client) {
const { promise, resolve, reject } = Promise.withResolvers();
let messages = [];
client.onopen = () => {
client.send("Hello from client!");
};
client.onmessage = e => {
messages.push(e.data);
};
client.onerror = reject;
client.onclose = e => {
resolve({ result: e, messages });
};
return promise;
}
const url = `wss://127.0.0.1:${server.address.port}`;
{
// by default rejectUnauthorized is true
const client = WebSocket(url);
const { result, messages } = await testClient(client);
expect(["Hello from Bun!", "Hello from client!"]).not.toEqual(messages);
expect(result.code).toBe(1006);
expect(result.reason).toBe("Failed to connect");
}
{
// just in case we change the default to true and test
const client = WebSocket(url, { tls: { rejectUnauthorized: true } });
const { result, messages } = await testClient(client);
expect(["Hello from Bun!", "Hello from client!"]).not.toEqual(messages);
expect(result.code).toBe(1006);
expect(result.reason).toBe("Failed to connect");
}
} finally {
server.stop(true);
}
});
it("rejectUnauthorized should NOT reject self-sign certs when false", async () => {
const COMMON_CERT = {
cert: "-----BEGIN CERTIFICATE-----\nMIIDXTCCAkWgAwIBAgIJAKLdQVPy90jjMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV\nBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\naWRnaXRzIFB0eSBMdGQwHhcNMTkwMjAzMTQ0OTM1WhcNMjAwMjAzMTQ0OTM1WjBF\nMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50\nZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB\nCgKCAQEA7i7IIEdICTiSTVx+ma6xHxOtcbd6wGW3nkxlCkJ1UuV8NmY5ovMsGnGD\nhJJtUQ2j5ig5BcJUf3tezqCNW4tKnSOgSISfEAKvpn2BPvaFq3yx2Yjz0ruvcGKp\nDMZBXmB/AAtGyN/UFXzkrcfppmLHJTaBYGG6KnmU43gPkSDy4iw46CJFUOupc51A\nFIz7RsE7mbT1plCM8e75gfqaZSn2k+Wmy+8n1HGyYHhVISRVvPqkS7gVLSVEdTea\nUtKP1Vx/818/HDWk3oIvDVWI9CFH73elNxBkMH5zArSNIBTehdnehyAevjY4RaC/\nkK8rslO3e4EtJ9SnA4swOjCiqAIQEwIDAQABo1AwTjAdBgNVHQ4EFgQUv5rc9Smm\n9c4YnNf3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wdIXDSQarCx0U=\n-----END PRIVATE KEY-----",
passphrase: "1234",
};
const server = Bun.serve({
port: 0,
tls: COMMON_CERT,
fetch(req, server) {
// upgrade the request to a WebSocket
if (server.upgrade(req)) {
return; // do not return a Response
}
return new Response("Upgrade failed :(", { status: 500 });
},
websocket: {
message(ws, message) {
ws.send(message);
ws.close();
}, // a message is received
open(ws) {
// a socket is opened
ws.send("Hello from Bun!");
},
},
});
try {
function testClient(client) {
const { promise, resolve, reject } = Promise.withResolvers();
let messages = [];
client.onopen = () => {
client.send("Hello from client!");
};
client.onmessage = e => {
messages.push(e.data);
};
client.onerror = reject;
client.onclose = e => {
resolve({ result: e, messages });
};
return promise;
}
const url = `wss://127.0.0.1:${server.address.port}`;
{
// should allow self-signed certs when rejectUnauthorized is false
const client = WebSocket(url, { tls: { rejectUnauthorized: false } });
const { result, messages } = await testClient(client);
expect(["Hello from Bun!", "Hello from client!"]).toEqual(messages);
expect(result.code).toBe(1000);
}
} finally {
server.stop(true);
}
});
it("should not accept untrusted certificates", async () => {
const UNTRUSTED_CERT = {
key: readFileSync(join(import.meta.dir, "..", "..", "node", "http", "fixtures", "openssl.key")),
cert: readFileSync(join(import.meta.dir, "..", "..", "node", "http", "fixtures", "openssl.crt")),
passphrase: "123123123",
};
const server = Bun.serve({
port: 0,
tls: UNTRUSTED_CERT,
fetch(req, server) {
// upgrade the request to a WebSocket
if (server.upgrade(req)) {
return; // do not return a Response
}
return new Response("Upgrade failed :(", { status: 500 });
},
websocket: {
message(ws, message) {
ws.send(message);
ws.close();
}, // a message is received
open(ws) {
// a socket is opened
ws.send("Hello from Bun!");
},
},
});
try {
function testClient(client) {
const { promise, resolve, reject } = Promise.withResolvers();
let messages = [];
client.onopen = () => {
client.send("Hello from client!");
};
client.onmessage = e => {
messages.push(e.data);
};
client.onerror = reject;
client.onclose = e => {
resolve({ result: e, messages });
};
return promise;
}
const url = `wss://localhost:${server.address.port}`;
{
const client = WebSocket(url);
const { result, messages } = await testClient(client);
expect(["Hello from Bun!", "Hello from client!"]).not.toEqual(messages);
expect(result.code).toBe(1006);
expect(result.reason).toBe("Failed to connect");
}
} finally {
server.stop(true);
}
});
it("supports headers", done => {
const server = Bun.serve({
port: 0,
fetch(req, server) {
expect(req.headers.get("X-Hello")).toBe("World");
expect(req.headers.get("content-type")).toBe("lolwut");
server.stop();
done();
return new Response();
},
websocket: {
open(ws) {
ws.close();
},
},
});
const ws = new WebSocket(`ws://${server.hostname}:${server.port}`, {
headers: {
"X-Hello": "World",
"content-type": "lolwut",
},
});
});
it("should FAIL to connect over http when the status code is invalid", done => {
const server = Bun.serve({
port: 0,
fetch(req, server) {
server.stop();
return new Response();
},
websocket: {
open(ws) {},
message(ws) {
ws.close();
},
close() {},
},
});
var ws = new WebSocket(`http://${server.hostname}:${server.port}`, {});
ws.onopen = () => {
ws.send("Hello World!");
};
ws.onclose = e => {
expect(e.code).toBe(1002);
done();
};
});
it("should connect over http ", done => {
const server = Bun.serve({
port: 0,
fetch(req, server) {
server.upgrade(req);
server.stop();
return new Response();
},
websocket: {
open(ws) {},
message(ws) {
ws.close();
},
close() {},
},
});
var ws = new WebSocket(`http://${server.hostname}:${server.port}`, {});
ws.onopen = () => {
ws.send("Hello World!");
};
ws.onclose = () => {
done();
};
});
describe("nodebuffer", () => {
it("should support 'nodebuffer' binaryType", done => {
const server = Bun.serve({
port: 0,
fetch(req, server) {
if (server.upgrade(req)) {
return;
}
return new Response();
},
websocket: {
open(ws) {
ws.sendBinary(new Uint8Array([1, 2, 3]));
},
},
});
const ws = new WebSocket(`http://${server.hostname}:${server.port}`, {});
ws.binaryType = "nodebuffer";
expect(ws.binaryType).toBe("nodebuffer");
Bun.gc(true);
ws.onmessage = ({ data }) => {
ws.close();
expect(Buffer.isBuffer(data)).toBe(true);
expect(data).toEqual(new Uint8Array([1, 2, 3]));
server.stop(true);
Bun.gc(true);
done();
};
});
it("should support 'nodebuffer' binaryType when the handler is not immediately provided", done => {
var client;
const server = Bun.serve({
port: 0,
fetch(req, server) {
if (server.upgrade(req)) {
return;
}
return new Response();
},
websocket: {
open(ws) {
ws.sendBinary(new Uint8Array([1, 2, 3]));
client.onmessage = ({ data }) => {
client.close();
expect(Buffer.isBuffer(data)).toBe(true);
expect(data).toEqual(new Uint8Array([1, 2, 3]));
server.stop(true);
done();
};
},
},
});
client = new WebSocket(`http://${server.hostname}:${server.port}`, {});
client.binaryType = "nodebuffer";
expect(client.binaryType).toBe("nodebuffer");
});
});
it("should send and receive messages", async () => {
const ws = new WebSocket(TEST_WEBSOCKET_HOST);
await new Promise((resolve, reject) => {
ws.onopen = resolve;
ws.onerror = reject;
ws.onclose = () => {
reject("WebSocket closed");
};
});
const count = 10;
// 10 messages in burst
var promise = new Promise((resolve, reject) => {
var remain = count;
ws.onmessage = event => {
gc(true);
expect(event.data).toBe("Hello World!");
remain--;
if (remain <= 0) {
ws.onmessage = () => {};
resolve();
}
};
ws.onerror = reject;
});
for (let i = 0; i < count; i++) {
ws.send("Hello World!");
gc(true);
}
await promise;
var echo = 0;
// 10 messages one at a time
function waitForEcho() {
return new Promise((resolve, reject) => {
gc(true);
const msg = `Hello World! ${echo++}`;
ws.onmessage = event => {
expect(event.data).toBe(msg);
resolve();
};
ws.onerror = reject;
ws.onclose = reject;
ws.send(msg);
gc(true);
});
}
gc(true);
for (let i = 0; i < count; i++) await waitForEcho();
ws.onclose = () => {};
ws.onerror = () => {};
ws.close();
gc(true);
});
it("should report failing websocket construction to onerror/onclose", async () => {
let did_report_error = false;
let did_report_close = false;
try {
const url = `wss://some-random-domain.smth`;
await new Promise((resolve, reject) => {
const ws = new WebSocket(url, {});
let timeout = setTimeout(() => {
reject.call();
}, 500);
ws.onclose = () => {
did_report_close = true;
clearTimeout(timeout);
resolve.call();
};
ws.onerror = () => {
did_report_error = true;
};
});
} finally {
}
expect(did_report_error).toBe(true);
expect(did_report_close).toBe(true);
});
});
describe("websocket in subprocess", () => {
it("should exit", async () => {
let messageReceived = false;
const server = Bun.serve({
port: 0,
fetch(req, server) {
if (server.upgrade(req)) {
return;
}
return new Response("http response");
},
websocket: {
open(ws) {
ws.send("hello websocket");
},
message(ws) {
messageReceived = true;
ws.close();
},
close(ws) {},
},
});
const subprocess = Bun.spawn({
cmd: [bunExe(), import.meta.dir + "/websocket-subprocess.ts", `http://${server.hostname}:${server.port}`],
stderr: "pipe",
stdin: "pipe",
stdout: "pipe",
env: bunEnv,
});
expect(await subprocess.exited).toBe(0);
expect(messageReceived).toBe(true);
server.stop(true);
});
it("should exit after killed", async () => {
const subprocess = Bun.spawn({
cmd: [bunExe(), import.meta.dir + "/websocket-subprocess.ts", TEST_WEBSOCKET_HOST],
stderr: "pipe",
stdin: "pipe",
stdout: "pipe",
env: bunEnv,
});
subprocess.kill();
if (isWindows) {
expect(await subprocess.exited).toBe(1);
} else {
expect(await subprocess.exited).toBe(143);
}
});
it("should exit with invalid url", async () => {
const subprocess = Bun.spawn({
cmd: [bunExe(), import.meta.dir + "/websocket-subprocess.ts", "invalid url"],
stderr: "pipe",
stdin: "pipe",
stdout: "pipe",
env: bunEnv,
});
expect(await subprocess.exited).toBe(1);
});
it("should exit after timeout", async () => {
let messageReceived = false;
let start = 0;
const server = Bun.serve({
port: 0,
fetch(req, server) {
if (server.upgrade(req)) {
return;
}
return new Response("http response");
},
websocket: {
open(ws) {
start = performance.now();
ws.send("timeout");
},
message(ws, message) {
messageReceived = true;
expect(performance.now() - start >= 300).toBe(true);
ws.close();
},
close(ws) {},
},
});
const subprocess = Bun.spawn({
cmd: [bunExe(), import.meta.dir + "/websocket-subprocess.ts", `http://${server.hostname}:${server.port}`],
stderr: "pipe",
stdin: "pipe",
stdout: "pipe",
env: bunEnv,
});
expect(await subprocess.exited).toBe(0);
expect(messageReceived).toBe(true);
server.stop(true);
});
it("should exit after server stop and 0 messages", async () => {
const { promise, resolve } = Promise.withResolvers();
const server = Bun.serve({
port: 0,
fetch(req, server) {
if (server.upgrade(req)) {
return;
}
return new Response("http response");
},
websocket: {
open(ws) {
resolve();
},
message(ws, message) {},
close(ws) {},
},
});
const subprocess = Bun.spawn({
cmd: [bunExe(), import.meta.dir + "/websocket-subprocess.ts", `http://${server.hostname}:${server.port}`],
stderr: "inherit",
stdin: "inherit",
stdout: "inherit",
env: bunEnv,
});
await promise;
server.stop(true);
expect(await subprocess.exited).toBe(0);
});
});