process.test.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { expect } from "bun:test"
  2. import { Effect } from "effect"
  3. import { HttpServer } from "effect/unstable/http"
  4. import { it } from "../../core/test/lib/effect"
  5. import { ServerProcess } from "../src/process"
  6. it.live("allows browser preflight requests without credentials", () =>
  7. Effect.gen(function* () {
  8. const server = yield* ServerProcess.start<never, never>({
  9. hostname: "127.0.0.1",
  10. port: 0,
  11. password: "secret",
  12. app: { version: "test-version" },
  13. database: { path: ":memory:" },
  14. })
  15. const response = yield* Effect.promise(() =>
  16. fetch(new URL("/api/health", HttpServer.formatAddress(server.address)), {
  17. method: "OPTIONS",
  18. headers: {
  19. origin: "http://localhost:3000",
  20. "access-control-request-method": "GET",
  21. "access-control-request-headers": "authorization",
  22. },
  23. }),
  24. )
  25. expect(response.status).toBe(204)
  26. expect(response.headers.get("access-control-allow-origin")).toBe("http://localhost:3000")
  27. expect(response.headers.get("access-control-allow-headers")).toBe("authorization")
  28. const health = yield* Effect.promise(() =>
  29. fetch(new URL("/api/health", HttpServer.formatAddress(server.address)), {
  30. headers: {
  31. authorization: `Basic ${btoa("opencode:secret")}`,
  32. origin: "http://localhost:3000",
  33. },
  34. }),
  35. )
  36. expect(health.status).toBe(200)
  37. expect(health.headers.get("access-control-allow-origin")).toBe("http://localhost:3000")
  38. expect(yield* Effect.promise(() => health.json())).toMatchObject({ version: "test-version" })
  39. }),
  40. )