mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 10:58:56 +00:00
621 lines
17 KiB
JavaScript
621 lines
17 KiB
JavaScript
import { describe, it, expect } from "bun:test";
|
|
import { bunExe, bunEnv, gc, tls } 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";
|
|
const COMMON_CERT = { ...tls };
|
|
|
|
describe("WebSocket", () => {
|
|
it("should connect", async () => {
|
|
using 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;
|
|
Bun.gc(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;
|
|
Bun.gc(true);
|
|
});
|
|
|
|
it("should connect many times over https", async () => {
|
|
using server = Bun.serve({
|
|
port: 0,
|
|
tls: COMMON_CERT,
|
|
fetch(req, server) {
|
|
if (server.upgrade(req)) {
|
|
return;
|
|
}
|
|
return new Response("Upgrade failed :(", { status: 500 });
|
|
},
|
|
websocket: {
|
|
message(ws, message) {
|
|
// echo
|
|
ws.send(message);
|
|
},
|
|
open(ws) {},
|
|
},
|
|
});
|
|
{
|
|
for (let i = 0; i < 1000; i++) {
|
|
const ws = new WebSocket(server.url.href, { tls: { rejectUnauthorized: false } });
|
|
await new Promise((resolve, reject) => {
|
|
ws.onopen = resolve;
|
|
ws.onerror = reject;
|
|
});
|
|
var closed = new Promise((resolve, reject) => {
|
|
ws.onclose = resolve;
|
|
});
|
|
|
|
ws.close();
|
|
await closed;
|
|
}
|
|
Bun.gc(true);
|
|
}
|
|
});
|
|
|
|
it("rejectUnauthorized should reject self-sign certs when true/default", async () => {
|
|
using 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!");
|
|
},
|
|
},
|
|
});
|
|
|
|
{
|
|
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(1015);
|
|
expect(result.reason).toBe("TLS handshake failed");
|
|
}
|
|
|
|
{
|
|
// 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(1015);
|
|
expect(result.reason).toBe("TLS handshake failed");
|
|
}
|
|
}
|
|
});
|
|
|
|
it("rejectUnauthorized should NOT reject self-sign certs when false", async () => {
|
|
using 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!");
|
|
},
|
|
},
|
|
});
|
|
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
});
|
|
|
|
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",
|
|
};
|
|
|
|
using 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!");
|
|
},
|
|
},
|
|
});
|
|
|
|
{
|
|
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(1015);
|
|
expect(result.reason).toBe("TLS handshake failed");
|
|
}
|
|
}
|
|
});
|
|
|
|
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);
|
|
});
|
|
|
|
// If this test fails locally, check that ATT DNS error assist is disabled
|
|
// or, make sure that your DNS server is pointed to a DNS server that does not mitm your requests
|
|
it("should report failing websocket connection in onerror and onclose for DNS resolution error", async () => {
|
|
const url = `ws://aposdkpaosdkpasodk.com`;
|
|
const { promise, resolve, reject } = Promise.withResolvers();
|
|
const { promise: promise2, resolve: resolve2, reject: reject2 } = Promise.withResolvers();
|
|
|
|
const ws = new WebSocket(url, {});
|
|
ws.onopen = () => reject(new Error("should not be called"));
|
|
ws.onmessage = () => reject(new Error("should not be called"));
|
|
ws.onerror = () => {
|
|
resolve();
|
|
};
|
|
ws.onclose = () => resolve2();
|
|
await Promise.all([promise, promise2]);
|
|
});
|
|
});
|
|
|
|
// We want to test that the `onConnectError` callback gets called.
|
|
it("should report failing websocket connection in onerror and onclose for connection refused", async () => {
|
|
const url = `ws://localhost:65412`;
|
|
const { promise, resolve, reject } = Promise.withResolvers();
|
|
const { promise: promise2, resolve: resolve2, reject: reject2 } = Promise.withResolvers();
|
|
|
|
const ws = new WebSocket(url, {});
|
|
ws.onopen = () => reject(new Error("should not be called"));
|
|
ws.onmessage = () => reject(new Error("should not be called"));
|
|
ws.onerror = () => {
|
|
resolve();
|
|
};
|
|
ws.onclose = () => resolve2();
|
|
await Promise.all([promise, promise2]);
|
|
});
|
|
|
|
describe("websocket in subprocess", () => {
|
|
it("should exit", async () => {
|
|
let messageReceived = false;
|
|
using 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);
|
|
});
|
|
|
|
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();
|
|
|
|
expect(await subprocess.exited).toBe(143); // 128 + 15 (SIGTERM)
|
|
expect(subprocess.exitCode).toBe(null);
|
|
expect(subprocess.signalCode).toBe("SIGTERM");
|
|
});
|
|
|
|
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;
|
|
using 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(), join(import.meta.dir, "websocket-subprocess.ts"), server.url.href],
|
|
stderr: "pipe",
|
|
stdin: "pipe",
|
|
stdout: "pipe",
|
|
env: bunEnv,
|
|
});
|
|
|
|
expect(await subprocess.exited).toBe(0);
|
|
expect(messageReceived).toBe(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);
|
|
});
|
|
});
|