diff options
author | 2023-08-24 22:49:58 -0700 | |
---|---|---|
committer | 2023-08-24 22:49:58 -0700 | |
commit | f269432d90826ad3e5b66c7685a6e826e0fb05e2 (patch) | |
tree | 78a05d1ba0a9bb7079ba111db7f3ddbac9b021ed /test | |
parent | 73b3fb7b0fa7cc786e147ccf1247cd9883ad8e59 (diff) | |
download | bun-f269432d90826ad3e5b66c7685a6e826e0fb05e2.tar.gz bun-f269432d90826ad3e5b66c7685a6e826e0fb05e2.tar.zst bun-f269432d90826ad3e5b66c7685a6e826e0fb05e2.zip |
Listen on a unix domain socket with Bun.serve() (#4311)
* Update response.zig
* Comment this out for now
* Support unix domain socket in Bun.serve()
* Add test
* add types
* Update JSFetchHeaders.cpp
* comment this test out
* tls unix web socket serve options
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Co-authored-by: dave caruso <me@paperdave.net>
Diffstat (limited to 'test')
-rw-r--r-- | test/js/bun/http/serve.test.ts | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/test/js/bun/http/serve.test.ts b/test/js/bun/http/serve.test.ts index 3066b4b37..b6ef75b54 100644 --- a/test/js/bun/http/serve.test.ts +++ b/test/js/bun/http/serve.test.ts @@ -1,11 +1,12 @@ import { file, gc, Serve, serve, Server } from "bun"; import { afterEach, describe, it, expect, afterAll } from "bun:test"; import { readFileSync, writeFileSync } from "fs"; -import { resolve } from "path"; +import { join, resolve } from "path"; import { bunExe, bunEnv } from "harness"; import { renderToReadableStream } from "react-dom/server"; import app_jsx from "./app.jsx"; import { spawn } from "child_process"; +import { tmpdir } from "os"; type Handler = (req: Request) => Response; afterEach(() => gc(true)); @@ -225,7 +226,7 @@ describe("streaming", () => { return new Response( new ReadableStream({ pull(controller) { - throw new Error("Not a real error"); + throw new Error("TestPassed"); }, cancel(reason) {}, }), @@ -1117,3 +1118,50 @@ it("does propagate type for Blob", async () => { server.stop(true); }); + +it("unix socket connection in Bun.serve", async () => { + const unix = join(tmpdir(), "bun." + Date.now() + ((Math.random() * 32) | 0).toString(16) + ".sock"); + const server = Bun.serve({ + port: 0, + unix, + + async fetch(req) { + expect(req.headers.get("Content-Type")).toBeNull(); + return new Response(new Blob(["hey"], { type: "text/plain;charset=utf-8" })); + }, + }); + + const requestText = `GET / HTTP/1.1\r\nHost: localhost\r\n\r\n`; + const received: Buffer[] = []; + const { resolve, promise } = Promise.withResolvers(); + const connection = await Bun.connect({ + unix, + socket: { + data(socket, data) { + received.push(data); + resolve(); + }, + }, + }); + connection.write(requestText); + connection.flush(); + await promise; + expect(Buffer.concat(received).toString()).toEndWith("\r\n\r\nhey"); + connection.end(); + server.stop(true); +}); + +it("unix socket connection throws an error on a bad domain without crashing", async () => { + const unix = "/i/don/tevent/exist/because/the/directory/is/invalid/yes.sock"; + expect(() => { + const server = Bun.serve({ + port: 0, + unix, + + async fetch(req) { + expect(req.headers.get("Content-Type")).toBeNull(); + return new Response(new Blob(["hey"], { type: "text/plain;charset=utf-8" })); + }, + }); + }).toThrow(); +}); |